选择结构必背经典程序(参考答案)1、输入3个整数,排序后输出。#include<stdio.h>voidmain(){intx,y,z,t;scanf("%d%d%d",&x,&y,&z);if(x>y){t=x;x=y;y=t;}/*交换x,y的值*/if(x>z){t=z;z=x;x=t;}/*交换x,z的值*/if(y>z){t=y;y=z;z=t;}/*交换z,y的值*/printf("smalltobig:%d%d%d\n",x,y,z);}2、求解一元二次方程ax2+bx+c=0。#include<stdio.h>#include<math.h>voidmain(){doublea,b,c,deta,x1,x2;printf("Entera,b,c:");scanf("%lf%lf%lf",&a,&b,&c);if(fabs(a)>1e-6){deta=b*b-4*a*c;if(deta>0){x1=-b/(2*a)+sqrt(deta)/(2*a);x2=-b/(2*a)-sqrt(deta)/(2*a);;printf("有两个不同的实根:x1=%.2f,x2=%.2f\n",x1,x2);}elseif(deta==0){x1=x2=-b/(2*a);printf("有两个相同的根:x1=x2=%.2f\n",x1);}else{x1=-b/(2*a);x2=sqrt(-deta)/(2*a);printf("有两个虚根:x1=%.2f+%.2f*i,x2=%.2f-%.2f*i\n",x1,x2,x1,x2);}}elseprintf("二次项系数为0,不是一元二次方程!");}3、已知银行整存整取存款不同期限的月息利率分别为:0.315%期限一年0.330%期限二年0.345%期限三年0.375%期限五年0.420%期限八年要求输入存钱的本金和期限,求到期时能从银行得到的利息与本金的合计。#include<stdio.h>voidmain(){doublecapital,total,interest;intdeadline;printf("Enteracapitalanddeadline:");scanf("%lf%d",&capital,&deadline);if(deadline>=8){interest=0.00420*capital*12*deadline;total=capital+interest;printf("Interest:%.3f,Total:%.3f\n",interest,total);}elseif(deadline>=5&&deadline<8){interest=0.00375*capital*12*deadline;total=capital+interest;printf("Interest:%.3f,Total:%.3f\n",interest,total);}elseif(deadline>=3&&deadline<5){interest=0.00345*capital*12*deadline;total=capital+interest;printf("Interest:%.3f,Total:%.3f\n",interest,total);}elseif(deadline==2){interest=0.00330*capital*12*deadline;total=capital+interest;printf("Interest:%.3f,Total:%.3f\n",interest,total);}elseif(deadline==1){interest=0.00315*capital*12*deadline;total=capital+interest;printf("Interest:%.3f,Total:%.3f\n",interest,total);}elseprintf("scoreinputerror!\n");}4、给一个不多于5位的正整数,要求:①求出它是几位数;②分别打印出每一位数字;③按逆序打印出各位数字,例如原数是321,应输出123。#include<stdio.h>voidmain(){longa,b,c,d,e,x;scanf("%ld",&x);a=x/10000;/*分解出万位*/b=x%10000/1000;/*分解出千位*/c=x%1000/100;/*分解出百位*/d=x%100/10;/*分解出十位*/e=x%10;/*分解出个位*/if(a!=0)printf("thereare5,%ld%ld%ld%ld%ld\n",e,d,c,b,a);elseif(b!=0)printf("thereare4,%ld%ld%ld%ld\n",e,d,c,b);elseif(c!=0)printf("thereare3,%ld%ld%ld\n",e,d,c);elseif(d!=0)printf("thereare2,%ld%ld\n",e,d);elseif(e!=0)printf("thereare1,%ld\n",e);}5、计算器程序。用户输入运算数和四则运算符,输出计算结果。#include<stdio.h>voidmain(){floata,b;charc;printf("inputexpression:a+(-,*,/)b\n");scanf("%f%c%f",&a,&c,&b);switch(c){case'+':printf("%f\n",a+b);break;case'-':printf("%f\n",a-b);break;case'*':printf("%f\n",a*b);break;case'/':printf("%f\n",a/b);break;default:printf("inputerror\n");}}6、输入学生成绩,输出等级,规则如下:90~100A80~89B70~79C60~69D不及格E#include<stdio.h>voidmain(){intscore;chargrade;printf("Enterascore:");scanf("%d",&score);if(score<=100&&score>=0){switch(score/10){case10:case9:grade='A';break;case8:grade='B';break;case7:grade='C';break;case6:grade='D';break;default:grade='E';break;}printf("Score:%d,Gread:%c\n",score,grade);}elseprintf("scoreinputerror!\n");}循环结构必背经典程序(参考答案)1、“百钱买百鸡问题”母鸡五钱一只,公鸡三钱一只,小鸡一钱三只,现有百钱欲买百鸡,共有多少种买法?#include<stdio.h>voidmain(){intn1,n2,n3,n;printf("所有可能的组...