您好,欢迎访问三七文档
//1.成绩判断#includestdio.hintmain(){//成绩intscore;printf(请输入你的成绩:\n);scanf(%d,&score);//判断if(score=0&&score60){printf(不及格\n);}elseif(60=score&&score80){printf(中等\n);}elseif(80=score&&score100){printf(优秀\n);}else{printf(输入错误!\n);}}//2.计算1到100的和#includestdio.hintmain(){intsum=0;//存结果变量inti;for(i=1;i=100;i++){sum=sum+i;}printf(sum=%d\n,sum);}//3.最大公约数#includestdio.h//求m,n的最大公约数intmain(){intm,n;inti,k;printf(请输入两个数:);scanf(%d%d,&m,&n);//三元运算符找较小的那个k=mn?m:n;//从较小的那个数倒着往前找for(i=k;i=1;i--){//这是公约数if((m%i==0)&&(n%i==0)){printf(最大公约数是%d\n,i);break;//跳出for循环}}}//4.最小公倍数#includestdio.h//求m,n的最小公倍数intmain(){intm,n;intmax,min;//m,n中较大,较小的那个intk;//max,2*max,3*max,.....printf(请输入两个数:);scanf(%d%d,&m,&n);//也可以交换m,n,保证m小n大max=mn?m:n;min=mn?m:n;k=max;//从max开始while(k%min!=0){k+=max;//每次倍增}printf(最小公倍数是%d\n,k);}//5.金字塔#includestdio.h//金字塔intmain(){inti;//外层intj;//内层for(i=1;i=10;i++){//当前是在第i行//先补空格10-i个for(j=1;j=10-i;j++){printf();}//再打2i-1个*for(j=1;j=2*i-1;j++){printf(*);}printf(\n);}}//6.九九乘法表#includestdio.h//打印九九乘法表intmain(){inti,j;for(i=1;i=9;i++)//外层一定是9行{for(j=1;j=i;j++)//内层第几行走几遍{printf(%d*%d=%d,i,j,i*j);}printf(\n);}}//7.百钱买百鸡#includestdio.h/**百钱买百鸡,类似1,2,5凑100银币问题*/intmain2(){inti,j;//公鸡,母鸡个数for(i=0;i=20;i++)//公鸡{for(j=0;j=33;j++)//母鸡{if((15*i+9*j+(100-i-j))==300){printf(公鸡%d,母鸡%d,小鸡%d\n,i,j,100-i-j);}}}}//1,2,5凑100银币问题intmain3(){intcount=0;//情况数inti,j;//5分个数,2分个数for(i=0;i=20;i++)//5分个数{for(j=0;j=50;j++)//2分个数{if((5*i+2*j)=100){count++;printf(%d:5分%d个,2分%d个,1分%d个\n,count,i,j,100-5*i-2*j);}}}}//8.一维数组的最大值、最小值、平均值#includestdio.h#defineN10//宏定义常量intmain(){inti;//下标索引intmax,min;doublesum=0;//累加和inta[N]={58,58,96,100,25,55,66,88,99,77};max=a[0];//假设第一个最大min=a[0];//假设第一个最小for(i=1;iN;i++){if(a[i]max)//比最大值还大max=a[i];//你才是最大if(a[i]min)//比最小值还小min=a[i];//你才是最小sum+=a[i];}printf(max=%d,min=%d\n,max,min);printf(average=%.2lf\n,sum/N);}//9.二维数组的最大值、最小值、平均值#includestdio.hintmain(){inti;//第几行intj;//第几列inta[3][4]={{1,2,3,4},{5,-6,7,8},{9,19,39,0}};intmax=a[0][0];//假设你最大intmin=a[0][0];//假设你最小doubleaverage;//平均值doublesum=0;//总和for(i=0;i3;i++)//必定3行{for(j=0;j4;j++)//必定4列{printf(%5d,a[i][j]);sum+=a[i][j];if(a[i][j]max)max=a[i][j];if(a[i][j]min)min=a[i][j];}printf(\n);}average=sum/(3*4);printf(max=%d,min=%d,avg=%.2lf\n,max,min,average);}//10.二维数组转置#includestdio.h//二维数组转置:行变列,列变行intmain(){inti;//第几行intj;//第几列inta[3][4]={{1,2,3,4},{5,-6,7,8},{9,19,39,0}};intb[4][3];for(i=0;i3;i++){for(j=0;j4;j++){printf(%5d,a[i][j]);}printf(\n);}//矩阵转置for(i=0;i3;i++){for(j=0;j4;j++){b[j][i]=a[i][j];}}for(i=0;i4;i++){for(j=0;j3;j++){printf(%5d,b[i][j]);}printf(\n);}}//11.冒泡排序#includestdio.h#defineN10//宏定义常量intmain(){inti;//下标索引intj;inttmp;//临时交换用inta[N]={58,58,96,100,25,55,66,88,99,77};//外层循环一定是N-1for(i=0;iN-1;i++){//两两交换,大的往后走for(j=0;jN-i-1;j++){//交换if(a[j]a[j+1]){tmp=a[j];a[j]=a[j+1];a[j+1]=tmp;}}}for(i=0;iN;i++){printf(%d,a[i]);;}printf(\n);}//12.结构冒泡排序#includestdio.h//结构定义,用户自定义类型typedefstructstudent{charsno[20];//学号charname[20];//姓名intage;//年龄chargender;//性别chartel[20];//电话};intmain(){inti;intj;doublesum=0;structstudenttmp;//两两交换临时用;//结构数组structstudentteam[5];for(i=0;i5;i++){printf(请输入第%d个队员的信息:\n,i+1);scanf(%s%s%d%c%s,team[i].sno,team[i].name,&team[i].age,&team[i].gender,team[i].tel);}//按年龄冒泡排序for(i=0;i5;i++){for(j=0;j5-i-1;j++){//两两交换if(team[j].ageteam[j+1].age){tmp=team[j];team[j]=team[j+1];team[j+1]=tmp;}}}//取值printf(%-12s%-10s%-5s%-5s%-15s\n,学号,姓名,年龄,性别,电话);for(i=0;i5;i++){printf(%-12s%-10s%-5d%-5c%-15s\n,team[i].sno,team[i].name,team[i].age,team[i].gender,team[i].tel);}}//13.结构数组找年龄最大值#includestdio.h//结构定义,用户自定义类型typedefstructstudent{charsno[20];//学号charname[20];//姓名intage;//年龄chargender;//性别chartel[20];//电话};intmain(){inti;structstudenttmp;//找最大临时用//结构数组structstudentteam[5];for(i=0;i5;i++){printf(请输入第%d个队员的信息:\n,i+1);scanf(%s%s%d%c%s,team[i].sno,team[i].name,&team[i].age,&team[i].gender,team[i].tel);}//取值printf(%-12s%-10s%-5s%-5s%-15s\n,学号,姓名,年龄,性别,电话);for(i=0;i5;i++){printf(%-12s%-10s%-5d%-5c%-15s\n,team[i].sno,team[i].name,team[i].age,team[i].gender,team[i].tel);}//找学号最大的那一个tmp=team[0];for(i=1;i5;i++){if(strcmp(team[i].sno,tmp.sno)0){tmp=team[i];}}printf(学号最大的队员如下:\n);printf(%-12s%-10s%-5d%-5c%-15s\n,tmp.sno,tmp.name,tmp.age,tmp.gender,tmp.tel);}//14.文件读写#includestdio.h#includestdlib.h//结构定义,用户自定义类型typedefstructstudent{charsno[20];//学号charname[20];//姓名intage;//年龄chargender;//性别chartel[20];//电话};//文件读写intmain(){structstudent*s,*p1;//个数未知FILE*fp;inti,n=0;charbuf[1024];//fgets缓冲区//打开文件fp=fopen(e:\\test.txt,r);while(fgets(buf,1024,fp)!=NULL)n++;fclose(fp);//指向一个可以存储n个student结构的内存空间s=(structstudent*)malloc(sizeof(structstudent)*n);p1=s;//不要动头位置s的值//打开文件fp=fopen(e:\\test.txt,r);for(i=0;in;i++){//从文件中读入一行fscanf(fp,%s%s%d%c%s,p1-sno,p1-name,&p1-age,&p1-gender,p1-tel);p1++;}fclose(fp);p1=s;for(i=0;i3;i++){printf(%s%s%d%c%s\n,p1-sno,p1-name,p1-age,p1-gender,p1-tel);}free(s);}//15.输入三角形三边长计算周长和面积#includestdio.h#includemath.hintmain(){doublearea,perimeter,s,a,b,c;printf
本文标题:c语言初学必背代码
链接地址:https://www.777doc.com/doc-7113491 .html