您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > 计算机二级需掌握的常用各种程序(由简到难)
二级C编程要点Linghanzh1第一、二章:C语言设计的初步知识☆基本语句#includestdio.hmain(){printf(hello);}☆求三个数的平均值#includestdio.hmain(){doublei,j,k,e;i=3.5;j=5.9999;k=2.6;e=(i+j+k)/3;printf(e=%f,e);}☆求矩形面积的程序#includestdio.hmain(){inta,b,c;a=3;b=5;c=a+b;printf(c=%d\n,c);}☆典型的求两数之积#includestdio.hmain(){inta,b,c;scanf(%d%d,a,b);c=a*b;printf(c=%d\n,c);}☆求圆面积的程序#includestdio.hmain(){doubler,s;r=3.5;s=3.1416*r*r;printf(s=%f\n,s);}☆求圆球的体积#includestdio.hmain(){doublev,r;r=2.3333;v=4*3.14159*r*r*r/3;printf(v=%f\n,v);}二级C编程要点Linghanzh2★交换算法(空瓶开头,首尾相连)#includestdio.hmain(){inta,b,t;a=3;b=5;t=a;a=b;b=t;printf(a=%d,b=%d\n,a,b);}★自加自减运算#includestdio.hmain(){inti,j;i=3;j=++i;/*i=i+1;j=i;*/printf(i=%d,j=%d\n,i,j);}#includestdio.hmain(){inti,j;i=3;j=--i;/*i=i-1;j=i;*/printf(i=%d,j=%d\n,i,j);}#includestdio.hmain(){inti,j;i=3;j=i++;/*j=i;i=i+1;*/printf(i=%d,j=%d\n,i,j);}#includestdio.hmain(){inti,j;i=3;j=i--;/*j=i;i=i-1;*/printf(i=%d,j=%d\n,i,j);}二级C编程要点Linghanzh3第三章顺序结构P343.17编写程序,把560分钟换算成用小时和分钟表示,然后进行输出。#includestdio.hmain(){inta,b;a=560/60;b=560%60;printf(%d:%d\n,a,b);}3.18编写程序,输入两个整数,求出他们的商和余数并进行输出。#includestdio.hmain(){inta,b,s,y;scanf(%d%d,&a,&b);s=a/b,y=a%b;printf(s=%d,y=%d\n,s,y);}3.19编写程序,读入三个双精度数,求它们的平均值,并保留此平均值小数点后一位小数,对小数点后第二位数进行四舍五入,最后输出结果。#includestdio.hmain(){doublea,b,c,d;scanf(%lf%lf%lf,&a,&b,&c);d=(a+b+c)/3.0;printf(d=%.1f\n,d);}3.20编写程序,读入三个整数给a,b,c,然后交换他们中的数,把a中原来的值给b,把b中原来的值给c,把c中原来的值给a,然后输出啊,a,b,c.#includestdio.hmain(){inta,b,c,t;printf(inputa,b,c:\n);scanf(%d%d%d,&a,&b,&c);t=a;a=c;c=b;b=t;printf(a=%d,b=%d,c=%d,a,b,c);}二级C编程要点Linghanzh4第四章选择结构一.If语句⒈单分支结构①输入两整数,按从小到大顺序排列后输出#includestdio.hmain(){inta,b,t;scanf(%d%d,&a,&b);if(ab){t=a;a=b;b=t;}printf(%5d%5d\n,a,b);}②输入三个整数,按从小到大顺序排列后输出#includestdio.hmain(){inta,b,c,t;scanf(%d%d%d,&a,&b,&c);if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}printf(%5d%5d%5d\n,a,b,c);}⒉双分支结构①4.23编写一个程序,输入一个整数,打印出它是计数还是偶数。#includestdio.hmain(){inta;scanf(%d,&a);if(a%2!=0)printf(jishu);elseprintf(oushu);}②★输入一个年份,判定是否为闰年。#includestdio.hmain(){inty;scanf(%d,&y);if(y%4==0&&y%100!=0||y%400==0)printf(Yes\n);elseprintf(No\n);}二级C编程要点Linghanzh5⒊多分支结构①输入成绩,输出等级。A:100-85,B:84-70,C:69-60,D:59-0。方法一:嵌套(在语法上为一条语句)#includestdio.hmain(){ints;scanf(%d,&s);if(s=85)printf(A\n);elseif(s=70)printf(B\n);elseif(s=60)printf(C\n);elseprintf(D\n);}方法二:不嵌套的if语句#includestdio.hmain(){ints;scanf(%d,&s);if(s=85&&s=100)printf(A\n);if(s=70&&s=84)printf(B\n);if(s=60&&s=74)printf(C\n);if(s60)printf(D\n);}⒋条件运算符①输入两个整数,输出较大者。#includestdio.hmain(){inta,b,max;scanf(%d%d,&a,&b);max=ab?a:b;printf(max=%d\n,max);}二级C编程要点Linghanzh6②输入三个数,输出最大者。#includestdio.hmain(){inta,b,c,max;scanf(%d%d%d,&a,&b,&c);max=ab?a:b;if(cmax)max=c;printf(max=%d\n,max);}4.22(较难)编写程序,输入一个学生的生日(年:y0,月:m0,日:d0),并输入当前日期(年:y1,月:m1,日:d1),输出该生的实际年龄。#includestdio.hmain(){inty0,m0,d0,y1,m1,d1,age;printf(Birthday:);scanf(%d%d%d,&y0,&m0,&d0);printf(Today:);scanf(%d%d%d,&y1,&m1,&d1);if(m1m0||m1==m0&&d1d0)age=y1-y0;elseage=y1-y0-1;printf(age=%d\n,age);}二.最简单的while语句:#includestdio.hmain(){ints=0,i=1;while(i=5){s+=i;i++;}printf(s=%d,i=%d\n,s,i);}/*1=5s=0+1=1i=22=5s=1+2=3i=33=5s=3=3=6i=44=5s=4=6=10i=55=5s=10+5=15i=6*/三.Switch语句:编程一般不用。二级C编程要点Linghanzh7第五章循环结构⒈★有规律的整数数列求和①1+2+3+…+100.#includestdio.hmain(){ints=0,i;for(i=1;i=100;i++)s+=i;printf(s=%d\n,s);}②n!.#includestdio.hmain(){ints=1,i,n;scanf(%d,&n);for(i=1;i=n;i++)s*=i;printf(s=%d\n,s);}③1-3+5-7+…-99+101.#includestdio.hmain(){ints=0,i,f=1;for(i=1;i=101;i+=2){s+=f*i;f=-f;}printf(s=%d\n,s);}⒉有规律的分数数列之和(数得清用for语句,数不清用while语句)★已知:1119171513114,①利用其前20个数求π的近似值#includestdio.hmain(){/*1°赋初值,2°循环,3°输出。*/doubles=0.0;inti,f=1,m=1;for(i=1;i=200;i++){s+=f*1.0/m;f=-f;m+=2;}printf(PI=%f\n,4*s);}②利用该式求π的值,直至最后一项的绝对值小于610。#includestdio.h#includemath.hmain(){doubles=0.0,t=1.0;intf=1,m=1;while(fabs(t)=1e-6){s+=t;f=-f;m+=2;t=f*1.0/m;}printf(PI=%f\n,s*4);}二级C编程要点Linghanzh8★已知!51!41!31!21!111e,计算前50项,求e的值。#includestdio.h#includemath.hmain(){doubles=1.0,m=1.0;inti;for(i=1;i=49;i++){s+=1.0/m;m*=(i+1);}printf(e=%f\n,s);}求e的值,直至最后一项的值小于610。#includestdio.h#includemath.hmain(){doubles=0.0,m=1.0,t=1.0;inti=1;while(t=1e-6){s+=t;m*=i;i++;t=1.0/m;}printf(e=%f\n,s);}⒊混合型:★编写程序,输出从公元2000年至公元3000年所有闰年的年号,每输出十个换一行。#includestdio.hmain(){inty,n;for(y=2000;y=3000;y++)if(y%4==0&&y%100!=0||y%400==0){printf(%5d,y);n++;if(n%10==0)printf(\n);}}二级C编程要点Linghanzh9⒋★求素数算法:输入一个整数,判定是否为素数。#includestdio.hmain(){intn,i;scanf(%d,n);for(i=2;i=n-1;i++)if(n%i==0)break;if(i=n)printf(yes\n);elseprintf(no\n);}输出2—100间所有的素数。#includestdio.hmain(){intn,i;for(i=2;i=100;i++){for(i=2;i=n-1;i++)if(n%i==0)break;elseprintf(%d,n);}}5.20编写程序,打印出以下图形:*************************#includestdio.hmain(){inti,j,k;for(i=1;i=4;i++){for(j=1;j=4-i;j++)printf();for(k=1;k=2*i-1;k++)printf(*);printf(\n);}for(i=3;i=1;i--){for(j=1;j=4-i;j++)printf();for(k=1;k=2*i-1;k++)printf(*);printf(\n);}}二级C编程要点Linghanzh10第六章字符型数据6.19编写程序,输入一行字符(用回车结束),输出每个字符,以及与之对应的ASCⅡ码值,每行输出三对。#i
本文标题:计算机二级需掌握的常用各种程序(由简到难)
链接地址:https://www.777doc.com/doc-3176795 .html