如何实现这样的程序
请教用什么软件如何编程可以实现下列要求: 输入一个数a
x1+x2+x3=a
y1=0.1 (0<x1<100)
y1=0.2 (100<x1)
y2=0.1 (0<x2<100)
y2=0.2 (100<x2)
y3=0.1 (0<x3<100)
y3=0.2 (100<x3)
求:x1,x2,x3为何值时x1*y1+x2*y2+x3*y3的和最小?
int main(){ int i,j,k; float a,b,c; int m[3]={200,200,200}; float min=60; for(i=1;i<=200;i++){ if(i>0 && i<=100) a=0.1; else a=0.2; for(j=1;j<=200;j++){ if(j>0&&j<=100) b=0.1; else b=0.2; for(k=1;k<=200;k++){ if(k>0 && k<=100) c=0.1; else c=0.2; if(1.0*i*a+1.0*j*b+1.0*k*c<min){ m[0]=i; m[1]=j; m[2]=k; min=1.0*i*a+1.0*j*b+1.0*k*c; } } } } printf("x1=%d\nx2=%d\nx3=%d\n",m[0],m[1],m[2]); getch(); }
/*----------------------------------------------------------------------------- 输入一个数a x1+x2+x3=a y1=0.1 (0<x1<100) y1=0.2 (100<x1) y2=0.1 (0<x2<100) y2=0.2 (100<x2) y3=0.1 (0<x3<100) y3=0.2 (100<x3) 求:x1,x2,x3为何值时x1*y1+x2*y2+x3*y3的和最小? =================================================================== 设 a 及 x1,x2,x3 均为 int 型。 -----------------------------------------------------------------------------*/ #include<stdio.h> int main(void) { int a,x1,x2,x3,num1,num2,num3; float y1,y2,y3,num; printf("input a :\n"); scanf("%d",&a); num=a; for(x1=1;x1<=a-2;++x1) for(x2=1;x2<=a-2;++x2) for(x3=1;x3<=a-2;++x3) if(a==(x1+x2+x3)) { y1=x1<100?0.1:0.2; y2=x2<100?0.1:0.2; y3=x3<100?0.1:0.2; if(num>x1*y1+x2*y2+x3*y3) { num=x1*y1+x2*y2+x3*y3; num1=x1;num2=x2;num3=x3; } } printf("x1=%d x2=%d x3=%d\n",num1,num2,num3); return 0; }