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
",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
",a1,b1,c1,d1,e1);
35     printf("%.2f
",maxMoney);
36     return 0;
37 }

 4、VOL大学乒乓球比赛

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 int main()
 5 {
 6     char teamA[] = { 'A','B','C' }; // 把字母映射成012
 7     char teamB[] = { 'X','Y','Z' }; // 把字母映射成012
 8     for (int i = 0; i < 3; i++) // 枚举A的对手
 9         for (int j = 0; j < 3; j++) // 枚举B的对手
10             for (int k = 0; k < 3; k++) // 枚举C的对手
11             {
12                 if (i != 0 && k != 0 && k != 2 // A不与X/C不与X,Z
13                     && i != j && j != k && k != i) // 保证ABC的对手是不同的
14                 {
15                     // 输出方案
16                     printf("%c=%c
",teamA[0],teamB[i]);
17                     printf("%c=%c
",teamA[1],teamB[j]);
18                     printf("%c=%c
",teamA[2] ,teamB[k]);
19                 }
20             }
21     return 0;
22 }

5、整数位数

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 int main()
 5 {
 6     double d;
 7     scanf("%lf",&d);
 8     int cnt = 0;
 9     int n = fabs(d);
10     while(n)
11     {
12         n/=10;
13         cnt++;
14     }
15     printf("%d",cnt);    
16     return 0;
17 }
 1 #include<stdio.h>
 2 #include<math.h>
 3 #define N 100
 4 int main()
 5 {
 6     char str[N] = "";
 7     char *p = str;
 8     int cnt = 0;
 9     gets(str);
10     while(*p=='-'||*p=='0')
11         p++;
12     while(*p!='.')
13     {
14         if(*p>='0'&&*p<='9')
15             cnt++;
16         p++;
17     }
18     printf("%d",cnt);    
19     return 0;
20 }

 6、两个整数之间所有的素数

 1 #include<stdio.h>
 2 #include<math.h>
 3 int isPrime(int n)
 4 {
 5     for(int i=2; i<n; ++i)
 6         if(n%i == 0)
 7             return 0;
 8     return 1;
 9 }
10 int main()
11 {
12     int a,b;
13     scanf("%d%d",&a,&b);
14     for(int i=a; i<=b; ++i)
15     {
16         if(isPrime(i))
17             printf("%d ",i);
18     }    
19     return 0;
20 }

7、自然数立方的乐趣

 1 #include<stdio.h>
 2 #include<math.h>
 3 #define N 100
 4 int main()
 5 {
 6     int a,i=1,t=i,sum=0,index=0;
 7     int arr[N] = {0};
 8     scanf("%d",&a);
 9     
10     while(sum != a*a*a)
11     {    
12         arr[index++] = i;         
13         sum += i;    
14         i+=2;
15         if(sum > a*a*a)
16         {
17             index = 0;
18             i = t+2;
19             t = i;
20             sum = 0;    
21         }
22     }
23     
24     printf("%d*%d*%d=%d=",a,a,a,a*a*a);
25     for(int j=0; j<index; ++j)
26     {
27         if(j)
28             printf("+");
29         printf("%d",arr[j]);
30     }
31     
32     return 0;
33 }

8、迭代求根

 1 #include<stdio.h>
 2 #include<math.h>
 3 #define EPS 1e-5
 4 int main()
 5 {
 6     double a,x=2,t;
 7     scanf("%lf",&a);
 8     x = a;
 9     
10     do{    
11         t = x;
12         x=(x+a/x)/2;
13     }while(t-x>EPS);
14     
15     printf("%.5f",x);
16     
17     return 0;
18 }

9、多项式的猜想

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 int main()
 5 {
 6     int a1,a2,a3,sum,n;
 7     int m[]={100,1000,10000},index = 0;
 8     
 9     while(index<3)
10     {
11         n=2,a1=1,a2=1,sum =2;//赋初值
12         while(sum<m[index])
13         {
14             a3=a2+2*a1;        
15             sum += a3; //累加
16             
17             if(sum>=m[index])//不符合条件break
18                 break;
19             
20             n++;      //符合条件n++
21             a2=a1,a1=a3; //迭代            
22         }
23         printf("%d
",n);//打印
24         index++; 
25     }
26     
27     return 0;
28 }

10、区间内的素数

 1 #include<stdio.h>
 2  
 3 int isPrime(int n)
 4 {
 5     for(int i=2; i<n; ++i)
 6         if(n%i==0)
 7             return 0;
 8     return 1;
 9 }
10  
11 int main()
12 {
13     int cnt=0, sign=1, sum=0;
14      
15     for(int j=800; j>=500; --j)
16     {
17         if(isPrime(j))
18         {
19             cnt++;
20             sum += j*sign;
21             sign = -sign;
22         }       
23     }
24      
25     printf("%d %d",cnt,sum);
26      
27     return 0;
28 }
原文地址:https://www.cnblogs.com/GoldenEllipsis/p/11532654.html