• 97.50 KB
  • 2022-04-22 11:47:15 发布

C语言入门1.2.3习题答案(1-5).doc

  • 17页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'本答案为个人学习时候的学习记录,初学者,指令还有不完善的地方,多包涵P40-7#includevoidmain(){inta,b,c,d,e;intre;a=2,b=3;re=(--a==b++)?--a:b++;printf("%d",b);c=a++;d=b;printf("i=%d,j=%d,k=%dn",re,c,d);}P52-1#includevoidmain(){charch[20];gets(ch);puts(ch);puts(ch);puts(ch);}P52-2#includevoidmain(){inta,b;scanf("%d%d",&a,&b);printf("%dn%dn%dn%dn",a+b,a-b,a*b,a/b);}P52-3#includeintmain(){charch;printf("Input:"); scanf("%c",&ch);printf("ASCIIof"%c"is%dn",ch,ch);}P52-4#includevoidmain(){inta,b,c;printf("input=");scanf("%d%d%d",&a,&b,&c);printf("(%d+%d+%d)/3=%dn",a,b,c,(a+b+c)/3);}P58#includevoidmain(){inta,b,c,d,e;intre;charch;scanf("%d",&a);if(a>90)printf("A");elseif(a>=80)printf("B");elseif(a>=70)printf("C");elseif(a>=60)printf("d");elsech="E";\两种方法来进行选择printf("theleverofthestuentis%cn",ch);}P61//switch语句必须要有#includevoidmain(){inta,b,c,d,e;intre; charch;scanf("%d",&a);switch(a){case1:a++;//当没有break时程序会继续向下扫描,执行case2case2:a++;case3:a++;break;case4:a++;default:a=999;}printf("A=%dn",a);}P64#includevoidmain(){inta,b,c,d,e;intyear,run,ping;charch;printf("pleasinputoneyearn");scanf("%d",&year);if(year%4==0)if(year%100==0)if(year%400==0)printf("runniann");elseprintf("pingniann");elseprintf("runniann");elseprintf("pingniann");}----------------升级版-------------------------------#includevoidmain(){inta,year;scanf("%d",&year);if(year%4==0&&year%100!=0||year%400==0)a=1;elsea=2; printf("%d",a);}P66循环体#includevoidmain(){inta,sum;a=0;sum=0;while(a<=100){sum+=a++;}printf("%d",sum);}----------------------------------#includevoidmain(){inta=0,sum=0;do{sum+=a++;}while(a<=100);printf("%dn",sum);}------------------------------------#includevoidmain(){inta=0,sum=0;for(;a<=100;a++)sum+=a;printf("%dn",sum);}P70#includevoidmain(){inta,b;a=1,b=0; for(;a<=9;a++){for(b=1;b<=a;b++)printf("%dX%d=%dt",b,a,a*b);printf("n");}}P72BREAK语句#includevoidmain(){inti;intflag=0;for(i=1;i<=100;i++){if(i%9==0)flag++;if(flag==5)break;}printf("%d",i);}P78-3求三个数值平均值差的最小数值#include#includevoidmain(){inta,b,c;inta1,b1,c1;intre;printf("pleaseinputthreenumbers:n");scanf("%d%d%d",&a,&b,&c);re=(a+b+c)/3;a1=abs(a-re);//abs求平均值,需要用到b1=abs(b-re);c1=abs(c-re);if(a1>c1&&b1>c1)printf("min=%dn",c);if(b1>a1&&c1>a1)printf("min=%dn",a);else printf("min=%dn",b);}P78-4计价器#includevoidmain(){inta,b;intpay;printf("pleaseinputdanjian");scanf("%d",&b);printf("pleaseinputroadn");scanf("%d",&a);switch(a/250){case0:pay=a*b;break;case1:pay=a*b*0.98;break;case2:pay=a*b*0.97;break;case3:pay=a*b*0.96;break;case4:pay=a*b*0.95;break;case5:pay=a*b*0.95;break;default:pay=a*b*0.95;break;}printf("youshouldpayformoney:%dn",pay);}P78-5分段式税收#includevoidmain(){intmoney;inttax;printf("pleaseinputyourmoney:n");scanf("%d",&money);if(money>=10000)tax=(money-10000)*0.05+230;elseif(money>=5000&&money<10000)tax=(money-5000)*0.03+80;elseif(money>=1000&&money<5000)tax=(money-1000)*0.02;elsetax=0; printf("youshouldpayfortax:t%dn",tax);}P78-6找出指定数字并排列#includevoidmain(){intn;intflag=0;for(n=1;n<=1000;n++){while(n%7==5&&n%5==1&&n%3==2){printf("%dt",n);flag++;if(flag%8==0)printf("n");break;}}}P78反向输出整数#includevoidmain(){inta,b=0,c;intflag=10;printf("pleaseinpiutonenumbern");scanf("%d",&a);for(;a/flag!=0;){c=a%flag;c=c*10/flag;a=a-c;//为什么这句话偏移了也能成立?b=b*10+c;flag=flag*10;}c=a%flag;c=c*10/flag; a=a-c;b=b*10+c;printf("%dn",b);}#includevoidmain(){inta,b=0,c;intflag=10;printf("pleaseinpiutonenumbern");scanf("%d",&a);for(;a/flag!=0;){c=a%flag;a=a-c;//这才是正确的位置c=c*10/flag;b=b*10+c;flag=flag*10;}c=a%flag;a=a-c;c=c*10/flag;b=b*10+c;printf("%dn",b);}P78-8#includevoidmain(){inta,b,c,d=0;intflag=10;intre=2,r=2;printf("pleaseinputonenumbern");scanf("%d",&a);printf("这个整数的最小公倍数包括以下数字:");//好麻烦,,,,,,,,,,,不算了,过printf("n每个数字分别是:");for(;a/flag!=0;){c=a%flag; a=a-c;c=c*10/flag;printf("%dt",c);flag*=10;d=d+1;}c=a%flag;a=a-c;c=c*10/flag;printf("%dt",c);d=d+1;printf("n这个数字共有%d位n",d);}P78-9判别日期#includevoidmain(){inty,m,d;intdays;loop:printf("请输入一个今年的日期(格式yyyy/mm/dd):n");scanf("%d/%d/%d",&y,&m,&d);if(d>31||y!=2015){printf("您输入的日期有误请重新输入n");gotoloop;}switch(m){case1:days=d;break;case2:days=31+d;break;case3:days=59+d;break;case4:days=90+d;break;case5:days=120+d;break;case6:days=151+d;break;case7:days=181+d;break;case8:days=212+d;break;case9:days=243+d;break;case10:days=273+d;break;case11:days=303+d;break;case12:days=335+d;break;default:printf("您输入的日期有误请重新输入n");gotoloop;}printf("%d年%d月%d日为今年的第%d天n",y,m,d,days);} P79-12求100以内的所有素数(质数)#includevoidmain(){inta=3,b=2;intflag;for(;a<=100;a++){flag=1;while(a>b){if(a%b==0){flag=0;break;}b++;}b=2;if(flag==1)printf("%dt",a);}}P79-13输出指定图案//待优化,现在的指令非常累赘#includevoidmain(){inta,b,c;intflag1=1,flag2=1;a=5;b=1;for(;b<=5;){for(;flag1=0;){for(;flag1voidmain(){inta1=100,a2=1,b=10,c;intsum=0;printf("全部的三位数的水仙花数有:n");for(;a1<=1000;a1++){for(a2=a1,b=10,sum=0;a2*10/b!=0;b=b*10){c=a2%b;a2=a2-c;c=c*10/b;sum=sum+c*c*c;}if(sum==a1){printf("%dt",a1);}}printf("n");}P88数组赋值#includevoidmain(){intage[5];inti,a;for(i=0,a=20;i<5;++i,++a){age[i]=a; }for(i=0;i<5;++i){printf("%dt",age[i]);}}P91冒泡排序(大数在上面)#includevoidmain(){inta,b;intnum[10]={23,18,25,17,19,22,21,15,28,20};inttmp;for(a=1;a<=9;a++){for(b=1;b<=10-a;b++){if(num[b-1]>num[b]){tmp=num[b];num[b]=num[b-1];num[b-1]=tmp;}}}for(a=0;a<=9;a++){printf("%dt",num[a]);}printf("n");}P93简单选择排序#includevoidmain(){intnum[10]={23,18,25,17,19,22,21,15,28,20};inta,b;inttmp;intflag;for(a=1;a<=9;a++){for(b=1,flag=0;b<=10-a;b++){if(num[b]>num[flag]){ flag=b;}}tmp=num[b-1];num[b-1]=num[flag];num[flag]=tmp;}for(a=0;a<=9;a++){printf("%dt",num[a]);}}P110中文字符输入并计算长度#include#includevoidmain(){charstr[20];intnum;gets(str);num=strlen(str);printf("字符是:%sn",str);//注意符号分为中文符号和英文符号printf("长度为:%dn",num);}P111-1键盘输入十个整数,求平均值并输出#include#includevoidmain(){intnum[10];inta,b;intre;intsum=0;for(a=1;a<=10;a++){printf("请输入第%d个整数:n",a);scanf("%d",&num[a-1]);}for(a=1;a<=10;a++){sum=sum+num[a-1];}re=sum/10;printf("这十个数的平均值是:%dn",re);} P111-2键盘输入字符,不用函数strlen()来检查其字符长度并输出#include#includevoidmain(){chara[100];inti=0;printf("请输入一个字符串n");gets(a);while(a[i]!="")i++;printf("你输入的字符串的长度为%dn",i);}P111-5键盘输入字符串,判断是英文还是中文字符本程序不完善,勉强在一定范围内使用#include#includevoidmain(){charstr[100];inta=0;gets(str);while(str[a]!=""){if(str[a]<="z"&&str[a]>="a"||str[a]<="Z"&&str[a]>="A"){printf("%c是英文字符n",str[a]);}else{//这里无法解决汉语字符的双字节情况if(a%2!=0){printf("是汉语字符n");}//这里无法解决输出单个汉语字符}a++;}}P111-6键盘输入英文字符串,翻转大小写并输出#include#includevoidmain(){ charstr[20];inta;printf("请输入一个大小写混合的英文字符串n");gets(str);for(a=0;str[a]!="";a++){if(str[a]>="a"){str[a]=str[a]-32;}else{str[a]=str[a]+32;}}puts(str);printf("n");}P111-7键盘输入英文字符串,检查其是否为回文字符串#include#includevoidmain(){charstr[20];inta=0,b;intre=0;intflag;printf("请输入一个英文字符串n");gets(str);for(flag=0;str[flag]!=0;flag++){}printf("字符串长度为%d",flag);do{b=str[a]-str[flag-a-1];if(b!=0){re++;}a++;}while(a!=flag/2);if(re==0){printf("n%s是回文字符串n",str);}else{printf("n%s不是回文字符串n",str);}}P112-12键盘输入字符串,倒序输出#include#includevoidmain() {charstr[20];chartmp[20];intflag,a;printf("请输入一个字符串:");gets(str);flag=strlen(str);tmp[flag]="";for(a=0;flag>0;a++){flag--;tmp[a]=str[flag];}printf("字符串倒序输出为:");puts(tmp);}P112-13键盘输入字符串,加密输出#include#includevoidmain(){charstr[20];chartmp[20];intflag,a;printf("请输入一个字符串:");gets(str);flag=strlen(str);tmp[flag]="";for(;flag>0;){flag--;if(str[flag]>="a"){a=str[flag]-"a";tmp[flag]="z"-a;}else{a=str[flag]-"A";tmp[flag]="Z"-a;}}printf("字符串加密输出为:");puts(tmp);} P113-14不用strcat()函数,键盘输入两个字符串,连接输出#include#includevoidmain(){charstr1[40];charstr2[20];intflag1,flag2,a,b;printf("请输入第一个字符串:");gets(str1);printf("请输入第二个字符串:");gets(str2);flag1=strlen(str1);flag2=strlen(str2);a=flag1+flag2;str1[a]="";for(;a>flag1;){a--;flag2--;str1[a]=str2[flag2];}printf("字符串连接输出为:");puts(str1);}'