C语言循环(DEVFORGE学编程社区)

1、级数和

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main()
 4 {
 5     int n = 5,j=1;
 6     double sum = 0;
 7     
 8     scanf("%d",&n);
 9     
10     while(n--)
11     {
12         sum += pow(-1,j-1)*pow(2,j)/((pow(2,j) + pow(-1,j))*(pow(2,j+1)+pow(-1,j+1)));
13         j++;
14     }
15     printf("%f",sum);
16     
17     return 0;
18 }

2、二分求根

 1 #include<stdio.h>
 2 #include<math.h>
 3 #define EPS 1e-6
 4 float Root(float x)
 5 {
 6     return 2*x*x*x - 4*x*x +3*x - 6;
 7 }
 8 int main()
 9 {
10     float min,max,mid;
11     scanf("%f%f",&min,&max);
12  
13     do{
14         mid=(max+min)/2.0;
15         if( Root(mid) > EPS) max = mid;
16         if( Root(mid) < EPS) min = mid;
17     }while(fabs( Root(mid) ) > EPS);
18  
19     printf("%.2f\n",mid);
20  
21     return 0;
22 }

3、你会存钱吗?

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main()
 4 {
 5     double maxMoney = 0;
 6     double temp;
 7     int a1, b1, c1, d1, e1;
 8     for (int a = 0; a <= 2; a++) 
 9     {
10         for (int b = 0; b <= (20-8*a)/5; b++) 
11         {
12             for (int c = 0; c <= (20-8*a-5*b)/3; c++) 
13             {
14                 for (int d = 0; d <= (20-8*a-5*b-3*c)/2; d++) 
15                 {
16                     int e = 20-8*a-5*b-3*c-2*d;
17                     temp = 2000*pow(1+0.0084*12*8, a)
18                         *pow(1+0.0075*12*5, b)
19                         *pow(1+0.0069*12*3, c)
20                         *pow(1+0.0066*12*2, d)
21                         *pow(1+0.0063*12*1, e);
22                     if (maxMoney < temp) {
23                         maxMoney = temp;
24                         a1 = a;
25                         b1 = b;
26                         c1 = c;
27                         d1 = d;
28                         e1 = e;
29                     }
30                 }
31             }
32         }
33     }
34     printf("%d %d %d %d %d\n",a1,b1,c1,d1,e1);
35     printf("%.2f\n",maxMoney);
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/GoldenEllipsis/p/11532654.html