2、
#include<stdio.h> #include<math.h> int main(){ int sign = 1,count = 0; double pi = 0.0,n = 1.0,term = 1.0; while(fabs(term)>=pow(10,-6)){ pi = pi +term; n = n+2; sign = -sign; term = sign/n; count++; } pi = pi*4; printf("pi = %10.8f\n",pi); printf("The count is:%d\n",count); return 0; }
3、辗转相除法
#include<stdio.h> int main(){ int a,b,temp = 0; int x,y; scanf("%d%d",&a,&b); x = a; y = b; while(b){ temp = a%b; a = b; b = temp; } printf("maxdiv:%d\n",a); printf("minmul:%d\n",x*y/a); return 0; }
4、容易出现问题,判断语句中=一定是==
#include<stdio.h>
int main(){ int a = 0; int b = 0; int c = 0; int o = 0; char ch; scanf("%c",&ch); while(ch!='\n'){ if(ch>='A'&&ch<='z') a++; else if((int)ch==32) b++; else if (ch>='0'&&ch<='9') c++; else o++; scanf("%c",&ch); } printf("The number of letter is:%d\n",a); printf("The number of blank is:%d\n",b); printf("The number of figure is:%d\n",c); printf("The number of other is:%d\n",o); return 0; }
5、
#include<stdio.h> #include<math.h> int main(){ int n,a=0,sum=0; scanf("%d",&n); for(int i=0;i<n;i++){ a=a+pow(10,i)*2; sum+=a; } printf("sum:%d\n",sum); return 0; }
6、
#include<stdio.h> int main(){ int i,j=1,sum=0; for(i=1;i<=20;i++){ j*=i; sum+=j; } printf("sum is:%d\n",sum); return 0; }
7、
#include<stdio.h> #include<math.h> int main(){ int sum1=0,sum2=0; float sum3=0; for(int i=1;i<=100;i++) sum1+=i; for(int i=1;i<=50;i++) sum2+=pow(i,2); for(int i=1;i<=10;i++) sum3+=1.0*1/i; printf("sum is :%0.2f\n",sum1+sum2+sum3); return 0; }
8、水仙花数
#include<stdio.h> int main(){ int a,b,c; for(int i=100;i<1000;i++){ a = i%10; b = i/10%10; c = i/100; if(a*a*a+b*b*b+c*c*c==i) printf("it is a narcinum:%d\n",i); } return 0; }
9、问题,第一遍sum放在int定义后面,所以结果为空,在计算每个数是不是完数时,都应该把sum归零
#include<stdio.h> int main(){ int i,j,sum; for(i=2;i<=1000;i++){ sum = 0; for(j=1;j<i;j++){ if(i%j==0) sum+=j; } if(sum==i){ printf("%d its factors are:",i); for(j=1;j<i;j++){ if(i%j==0) printf("%d,",j); } printf("\n"); } } return 0; }
10、
#include<stdio.h> int main(){ float sum; int a = 2,b = 1,c; for(int i=1;i<=20;i++){ sum+=(float)a/b; c=a; a=a+b; b=c; } printf("the sum:%f\n",sum); return 0; }
11、
#include<stdio.h> #include<math.h> int main(){ float sum=100,high; for(int i=1;i<10;i++){ sum=sum+(1.0*100/pow(2,i))*2; } high = 0.1*100/pow(2,10); printf("sum:%f,high:%f\n",sum,high); return 0; }
12、正好返回来求,确定循环几次。
#include<stdio.h> int main(){ int sum = 1,i; for(i=1;i<=9;i++){ sum=(sum+1)*2; } printf("sum is :%d\n",sum); return 0; }
13、
#include<stdio.h> #include<math.h> int main(){ float a,m,n; printf("please input:"); scanf("%f",&a); m = a/2; while(1){ n = m; m = 0.5*(n+a/n); if(fabs(n-m)<pow(10,-5)) break; } printf("the result is:%f\n",m); return 0; }
14、熟悉牛顿迭代法定义
#include<stdio.h> #include<math.h> int main(){ float x,x1=1.5,f,f1; for(x=0;fabs(x1-x)>=pow(10,-5);){ x=x1; f=2*x*x*x-4*x*x+3*x-6; f1=6*x*x-8*x+3; x1=x-f/f1; printf("x1=%f\n",x1); } return 0; }
15、
#include<stdio.h> #include<math.h> int main(){ float a,a1,b,x=-10,y=10; a = 10; while(1){ a1 = a; a = (x+y)/2; b = 2*a*a*a-4*a*a+3*a-6; if(b>0) y=a; else x=a; if(fabs(a1-a)<=pow(10,-5)) break; } printf("the result is:%f\n",a); return 0; }
16、
标签:10,main,int,sum,C语言,谭浩强,课后,pi,include From: https://www.cnblogs.com/gunancheng/p/17398069.html