1 / 21

习 题 课 ( 实验 )

习 题 课 ( 实验 ). 第三章. 附加题:小写字母转盘. #include&lt;stdio.h&gt; main() { char ch; printf(“ 请输入一个字符 :&quot;); ch=getchar(); ch=(ch-'a'+1)%26+'a'; printf(“<br> 其后面的字符是 : &quot;); putchar(ch); }. 5x+11 0 ≤ x &lt;20. sinx+ cosx 20 ≤ x&lt;40. e x -1 40 ≤ x&lt;60. y=.

jerold
Download Presentation

习 题 课 ( 实验 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 习 题 课(实验)

  2. 第三章 附加题:小写字母转盘 #include<stdio.h> main() { char ch; printf(“\请输入一个字符:"); ch=getchar(); ch=(ch-'a'+1)%26+'a'; printf(“\n其后面的字符是: "); putchar(ch); }

  3. 5x+11 0≤x <20 sinx+ cosx 20 ≤x<40 ex-1 40 ≤x<60 y= ln(x+1) 60 ≤x<80 x4其余值 选择结构 1、编一程序,按下式计算y的值,x的值由键盘输入。

  4. #include<stdio.h> #include<math.h> main() { float x,y; printf("Please input x:"); scanf("%f",&x); if(x>=0&&x<20) y=5*x+11; else if(x>=20&&x<40) y=sin(x)+cos(x); else if(x>=40&&x<60) y=exp(x); else if(x>=60&&x<80) y=log(x+1); else y=pow(x,4); printf("y=%f",y); }

  5. 2、按工资的高低纳税,已知不同工资s的税率p如下:2、按工资的高低纳税,已知不同工资s的税率p如下: s<1000 p=0% 1000≤s<2000 p=5% 2000≤s<3000 p=8% 3000≤s<5000 p=10% 5000≤s p=15% 编一程序,输入工资数,求纳税款和实得工资数。

  6. #include<stdio.h> main() { float salary,p,way; printf("Please input salary:"); scanf("%f",&salary); if(salary<1000) p=0; else if(salary<2000) p=0.05; else if(salary<3000) p=0.08; else if(salary<5000) p=0.1; else p=0.15; way=salary*(1-p); printf("p=%f,way=%f",p,way); }

  7. #include<stdio.h> main() { float salary,p,way; int t; printf("Please input salary:"); scanf("%f",&salary); t=salary/1000; switch(t) { case 0: p=0; break; case 1: p=0.05; break; case 2: p=0.08; break; case 3: case 4: p=0.1; break; default: p=0.15; break; } way=salary*(1-p); printf("p=%f,way=%f",p,way); }

  8. 4、编写一个“猜猜看”程序。 即给定一个样板数,比如123,让用户在 键盘 上输入整数,将其与样板数比较。 如果相等,则输出“正确”字样; 否则输出太大或太小的信息。

  9. main() { int num,guess; printf("Please input model-number:"); scanf("%d",&num); clrscr(); printf("mode-number:***"); printf("\nNow start:"); scanf("%d",&guess); while(guess!=num) { if(guess>num) printf("\nThe guess is high!"); if(guess<num) printf("\nThe guess is low!"); printf("\nContinue:"); scanf("%d",&guess); } printf("The guess is right!"); }

  10. (-1) n-1 … s= - - + + + n! 1 1 1 — — — 1! 2! 3! 1 — 4! #include "stdio.h" #include "math.h" main() { int i,j; float t=1,f=1.0,S=0; for(i=1;f>1e-6;i++) { t=1; for(j=1;j<=i;j++) t=t*j; f=1.0/t; S=S+f*pow(-1,i-1); } printf("S=1/1! - 1/2! +…=%f",S); } 循环结构 1、编程求 , 直到最后一项的绝对值小于10-6为止。 for(i=1;f>1e-6;i++) { t=t*j; f=1.0/t; S=S+f*pow(-1,i-1); }

  11. #include "stdio.h" main() { int num1,num2,num3,max,i; printf("\nPlease input three number(num1,num2,num3):"); scanf("%d,%d,%d",&num1,&num2,&num3); max=num1; if(max<num2) max=num2; if(max<num3) max=num3; for(i=max; ; i++) if(i%num1==0 && i%num2==0 && i%num3==0) break; printf("min_multiple=%d",i); } 2、编写程序,求任意输入的三个正整数的 最小公倍数并输出。

  12. #include "stdio.h" main() { int num1,num2,num3,sub,min_multiple,r; printf("\nPlease input three number(num1,num2,num3):"); scanf("%d,%d,%d",&num1,&num2,&num3); sub=num1*num2; r=num1%num2; while(r!=0) { num1=num2; num2=r; r=num1%num2; } min_multiple=sub/num2; sub=min_multiple*num3; r=min_multiple%num3; while(r!=0) { min_multiple=num3; num3=r; r=min_multiple%num3; } min_multiple=sub/num3; printf("min_multiple=%d",min_multiple); }

  13. #include <stdio.h> #include <math.h> main() { int i,j; for(i=101;i<=197;i+=2) { for(j=2;j<=i/2;j++) if(i%j==0) break; if(j>i/2) { for(j=2;j<=(i+2)/2;j++) if((i+2)%j==0) break; if(j>(i+2)/2) printf("%d and %d is twins prime!\n",i,i+2); } } getch(); } 3、相邻两个奇数都为素数的这样一对数叫双胞 胎素数,输出100到200之间的所有双胞胎素数。

  14. #include "stdio.h" main() { int coin1,coin2,coin5,count=0; printf("\nmethod:"); for(coin5=0;coin5<=20;coin5++) for(coin2=0;coin2<=50;coin2++) for(coin1=0;coin1<=100;coin1++) { if((coin1+coin2+coin5==50)&& (coin1+coin2*2+coin5*5==100)) { count++; printf("\n[%d]:%d-coin5,%d-coin2, %d-coin1",count,coin5,coin2,coin1); } } } 4、编写程序实现用一元人民币换成一分、两分、 五分的硬币共50枚。 设一分、两分、五分各为coin1,coin2,coin5 coin1+coin2+coin5=50 则应满足: coin1+coin2*2+coin5*5=100

  15. 根据: coin1+coin2+coin5=50 #include "stdio.h" main() { int coin1,coin2,coin5,count=0; printf("\nmethod:"); for(coin5=0;coin5<=20;coin5++) for(coin2=0;coin2<=50;coin2++) { coin1=50-coin2-coin5; if(coin1*1+coin2*2+coin5*5==100) { count++; printf("\n[%d]:%d-coin5,%d-coin2, %d-coin1",count,coin5,coin2,coin1); } } } coin1=50-coin2-coin5 得出:

  16. coin1+coin2+coin5=50 根据: coin1+coin2*2+coin5*5=100 #include "stdio.h" main() { int coin1,coin2,coin5,count=0; printf("\nmethod:"); for(coin5=0;coin5<=12;coin5++) { coin2=50-4*coin5; coin1=50-coin2-coin5; count++; printf("\n[%d]:%d-coin5,%d-coin2,%d-coin1", count,coin5,coin2,coin1); } } coin2=50-4*coin5 得出: coin5<=12

  17. main() { long num,i,sum,min,max; printf("Please input min and max(min,max):"); scanf("%ld,%ld",&min,&max); clrscr(); printf("---Rank (n%ld-%ld)--- ",min,max); for(num=min;num<max;num++) { sum=0; for(i=1;i<=num-1;i++) if(num%i==0) sum+=i; if(sum==num) { printf("\n%ld its factors are ",num); for(i=1;i<num-1;i++) if(num%i==0) printf("%ld,",i); } } } 7、一个数如果恰好等于它的因子之和,这个数就 称为“完数”.编程序找出1000之内的所有完数,并按 下面的格式输出其因子: 6 its factors are 1,2,3 分析:找出num(1000之内)循环 所有的因子 求余为0(1~num-1) 因子之和:循环,sum=sum+t(因子); if(sum==num) …

  18. main() { int i=0; float sum=0,score; printf("\nPlease input No.%d student's score:",i+1); scanf("%f",&score); while(score>=0) { i++; sum=sum+score; if (score<60) printf("\n*** %f is under 60.***\n",score); printf("Please input No.%d student's score:",i+1); scanf("%f",&score); } printf("\nThe average score is %f",sum/i); } 5.10 从键盘上输入若干个学生某门课的 成绩,计算出平均成绩,并输出低于60分 的学生成绩,当输入负数时结束输入。

  19. 附加题: 两个乒乓球队进行比赛,各出三人,甲队为A,B,C三人, 乙队为X,Y,Z三人,以抽签决定比赛名单.有人向队员打 听比赛的名单,A说他不和X比,C说他不和X,Z比.请编程 序找出三队赛手的名单.

  20. main() { char i,j,k; printf("The list is:"); for(i='X';i<='Z';i++) for(j='X';j<='Z';j++) if(i!=j) for(k='X';k<='Z';k++) if(i!=k&&j!=k) if(i!='X'&&k!='X'&&k!='Z') printf("A---%c,B---%c,C---%c\n",i,j,k); }

  21. 5.12 编写一个程序,输入从2001到2010年中的任何 一年,用for循环输出一个日历。注意对闰年的处理。 万年历

More Related