#includeint main(){/*********Begin*********/ int a[10]; int i,j,t; printf("\n"); for (i=0;i<10;i++) scanf("%d",&a[i]); printf("\n"); for (j=0;j<9;j++) for(i=0;ia[i+1]) { t=a[i];a[i]=a[i+1];a[i+1]=t; } printf("\n"); for(i=9;i>=0;i--) printf("%d ",a[i]); printf("\n"); return 0;}
第2关查找整数
#includeint a[1009];int main(){int n;scanf("%d", &n);for(int i = 1; i <= n; i ++)scanf("%d", &a[i]);int pre;scanf("%d", &pre);for(int i = 1; i <= n; i ++){if(a[i] == pre){printf("%d", i);return 0;}}printf("-1");return 0;}
第3关计算数组中元素的最大值及其所在的行列下标值
#includeint a[1000][1000];int main(){printf("Input m, n:");int n, m;scanf("%d,%d", &n, &m);printf("Input %d*%d array:\n", n , m);for(int i = 1; i <= n; i ++)for(int j = 1; j <= m; j ++)scanf("%d", &a[i][j]);int MAX = -999999;int r, c;for(int i = 1; i <= n; i ++)for(int j = 1; j MAX){MAX = a[i][j];r = i;c = j;}printf("max=%d, row=%d, col=%d", MAX, r, c);return 0;}
第4关删除最大值
#includeint main(){int a[10];int MAX = -1;for(int i = 0; i < 10; i ++)scanf("%d", &a[i]);for(int i = 0; i MAX) MAX = a[i];}for(int i = 0; i < 10; i ++){if(a[i] == MAX) continue;printf("%d ", a[i]);}return 0;}
第5关杨辉三角
#includeint a[10][10];int main(){int num = 1;for(int i = 0; i < 10; i ++) a[i][0] = 1;for(int i = 1; i < 10; i ++){for(int j = 1; j < 10; j ++){if(j == num){num ++;a[i][j] = 1;break;}a[i][j] = a[i - 1][j - 1] + a[i - 1][j];}}num = 1;for(int i = 0; i < 10; i ++){for(int j = 0; j < 10; j ++){if(j == num){num ++;break;}if(j == num - 1) printf("%d", a[i][j]);else printf("%d ", a[i][j]);}printf("\n");}return 0;}
三种循环语句的使用
第1关分数序列和
#include int main( ){ int a, b, c, k, n = 5;double s;s = 0.0; a = 2; b = 1;for ( k = 1; k <= n; k++ ) {/*****以下一行有错误*****/s = s + (double)a / (double)b;/*****以下一行有错误*****/c=a;a=b+c;b=c;} printf( "s%d = %lf\n", n, s); return 0;}
#includeint main(void){/*********Begin*********/int N ,i,sum=1;scanf("%d",&N);for(i=1;i<=N;i++){if(i%5==0){sum=sum*i;}}printf("%d",sum);/*********End**********/return 0;}
#includeint main(void){/*********Begin*********/int n, a, b, c;scanf("%d",&n);a=n%10;//这个数的个位n/=10;b=n%10;//这个数的十位n/=10;c=n;//这个数的百位printf("%d %d %d",c,b,a);/*********End**********/return 0;}
#includeint main(void){int a,b;//Enter a and b:scanf("%d%d",&a,&b); printf("a=%d b=%d\n",a,b);/*********Begin*********/a=a^b;b=a^b;a=a^b;/*********End**********/printf("a=%db=%d\n",a,b); return 0;}
第3关用宏定义常量
#includeint main(void){/*********Begin*********/const int N=30;//定义常量用const,即定义题中价格为30.int x;scanf("%d",&x);printf("%d",x*N);/*********End**********/return 0;
#include int main() {int i = 0, j = 0;char s[20], t[20];scanf("%s", s);for (i = 0; i <= 20; i++) {if (i % 2 != 0 || s[i] % 2 == 0)//奇数位置的字符或ASCII码为偶数的字符t[j++] = s[i];}printf("%s", t);return 0;}
第4关:找最长字符串
/********** Begin **********/#include #include int main(){int N;char s[100][100];int i;int max_i, max_len = 0;scanf("%d",&N);for(i = 0; i < N; i ++){scanf("%s",s[i]);//输入}for(i = 0; i < N; i ++){int l = strlen(s[i]);if(max_len <l) {max_len = l;max_i = i;}}printf("%s\n", s[max_i]);//输出最长字符串return 0;}
第5关:单词排序
#include #include int main(){//she its can ibm1 bbc NBA nhk2 BOY jxf //c python java c++ android caffe keras pytorch php int N,i,k;char j,s[100][100],a[100][100];printf("排序前的字符串:\n");for(i=0;i<10;i++){scanf("%s",s[i]);//输入printf("%s",s[i]);if(i<9)printf("\n");fflush(stdout);}for(i=0;i<9;i++){k=i;for(j=i+1;j0)k=j;}if(k!=i){char temp[100];strcpy(temp, s[k]);strcpy(s[k], s[i]);strcpy(s[i], temp);}}printf("排序后的字符串:");for(i=0;i<10;i++){printf("%s\n",s[i]);} return 0;}
选择结构程序设计
第1关排序
#includeint main(void){ int a,b,c,temp1,temp2,temp3;/*********Begin*********/printf("请输入三个整数:");scanf("%d%d%d",&a,&b,&c); if(a>b){temp1=a;a=b;b=temp1;}if (a>c){temp2=a;a=c;c=temp2;}if (b>c){temp3=b;b=c;c=temp3;}printf("从小到大排列为:%d,%d,%d",a,b,c);/*********End**********/return 0;}
#includeint main(void){float x;/*********Begin*********/scanf("%f",&x);if (x>=90&&x<=100){printf("A");}if (x=80){printf("B");} if (x>=70&&x=60&&x<69){printf("D");}if (x=0){printf("E");}if (x100){printf("Score is error!");}/*********End**********/return 0;}
循环结构程序设计2
第1关C循环-求平均成绩
#includeint main(){int s;float x,sum,average;sum=0;scanf("%d",&s);if(s>0){for(int i=0;i<s;i++){scanf("%f",&x);sum=sum+x;}average=sum/s;printf("the number of students:the scores:average=%.2f",average);}elseprintf("the number of students:the scores:average=0.00");return 0;}
#include #include int main(){int a,b;scanf("%d",&a);if (a<0)b=-a;else b=a;printf("%d\n",b);return 0;}
第2关求m以内的奇数之和
#include int main(){int n,m, sum;scanf("%d",&m);n=1;sum=0;while(n<=m){sum=sum+n;n+=2;}printf("%d\n",sum);return 0;}
第3关排除此数还是输出此数
#include int main(){//获取参数方式 scanf//int x=0;//int y = 0;//scanf("%d", &x);//结果输出使用prinf//printf("%d",x); // 请在此添加你的代码/********** Begin *********/int x;scanf("%d",&x);if(x%3==0||x/100==3){printf("%d",x);}elseprintf("排除此数");return 0;/********** End **********/}
第4关医院收费.
#include int main(){float a,b,c,d,e,f;scanf("%f %f %f %f",&a,&b,&c,&e);d = a + b*c;f = e - d;printf("请输入药品费、床位费/天、住院天数:应付款:%.2f\n实付款:应找零:%.2f",d,f);return 0;}