C语言代码
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
函数求和 #include #include int Sum (int n); int main () { int n,r; scanf("%d",&n); r=Sum(n); printf("%d",r); } int Sum (int n) { int sum=0;; for (int i=0;i<=n;i++){ sum=sum+i; } return sum; } N 到M之间的和函数 #include #include int Sum (int n,int m); int main () { int n,r,m; scanf("%d %d",&n,&m); r=Sum(n,m); printf("%d",r); } int Sum (int n,int m) { int sum=0;; for (int i=n;i<=m;i++){ sum=sum+i; } return sum; } 比较两个数之间的最大的数 #include #include int MAX (int a,int b); int main () { int a,b,r; scanf("%d %d",&a,&b); r=MAX(a,b); printf("%d",r); } int MAX (int a,int b){ if (a>=b){ return a; }else { return b; } } 复数运算 #include float real,image; void complex_add(float real1,float real2,float image1,float image2); void complex_pro(float real1,float real2,float image1,float image2); int main () { float real1,real2; float image1,image2; scanf("%f %f",&real1,&image1); scanf("%f %f",&real2,&image2); complex_add(real1,image1,real2,image2); printf("%f %f", real,image); complex_pro(real1,image1,real2,image2); printf("%f %f", real,image); } void complex_add(float real1,float real2,float image1,float image2){ real=real1+real2; image=image1+image2; } void complex_pro(float real1,float real2,float image1,float image2){ real=real1*real2-image1*image2; image=real1*image2+real2*image1; } 阶乘函数 #include double fact (int n); int main (){ int n; double r; scanf("%d",&n); r=fact(n); printf("%f",r); } double fact (int n) { int i; double p; p=1; for(i=1;i<=n;i++){ p=p*i; } return p; } 静态变量 #include double fact_s (int n); int main (){ int n; double r; scanf("%d",&n); //r=fact_s(n); for(int i=1;i<=n;i++){ printf("%f",fact_s(i)); } } double fact_s (int n) { static double p=1; p=p*n; return (p); } 字符串 #include int main () { int count ,i; char str [80]; i=0; while((str[i]=getchar())!='\n'){ i++; str [i]='\0'; } count=0; for(i=0;str[i]!='\0';i++){ if(str[i]<='9'&&str[i]>='0') count++; } printf("%d",count); } 22 #include int main () { int i,number; char str[10]; i=0; while ((str[i]=getchar())!='\n'){ i++; str[i]='\0'; } number=0; for(i=0;str[i]!='\0';i++){ if(str[i]>='0'&&str[i]<='9') number=number*10+str[i]-'0'; } printf("%d\n",number); } //////////////////////////// #include struct student { int num; char name [10]; int computer,english,math; double average; }; int main () { int i,j,index,n; struct student students[50],temp; scanf ("%d", &n); for (i=0;i scanf ("%d", &students[i].num); scanf ("%s", &students[i].name); scanf ("%d", &students[i].math); scanf ("%d", &students[i].english); scanf ("%d", &students[i].computer); students[i].average=(students[i].math+students[
i].english+students[i].computer)/3.0; } for (i=0;i{ index=i; for (j=i+1;j if
(students[j].average>students[index].average) index=j; temp=students[index]; students[index]=students[i]; students[i]=temp; } for(i=0;i printf
("%d\t %s\t %.2lf\n",students[i].num,students[i].name,students[i].average); return 0; /*101 zhang 76 85 78 102 wang 83 72 60 */ }
本文来源:https://www.wddqw.com/doc/f83ceb40657d27284b73f242336c1eb91b37334e.html