任务1:
源代码:
1 #include<stdio.h> 2 char score_to_grade(int score); 3 int main(){ 4 int score; 5 char grade; 6 7 while(scanf("%d",&score)!=EOF){ 8 grade=score_to_grade(score); 9 printf("分数:%d,等级:%c\n\n",score,grade); 10 } 11 return 0; 12 } 13 char score_to_grade(int score){ 14 char ans; 15 switch(score/10){ 16 case 10: 17 case 9:ans='A';break; 18 case 8:ans='B';break; 19 case 7:ans='C';break; 20 case 6:ans='D';break; 21 default: ans='E'; 22 } 23 return ans; 24 }
问题1:将分数以等第的形式输出;形参类型:整型;返回值类型:字符型
问题2:有1.字符用的是双引号,没有用单引号,不符合格式要求 2.case6-9后没有加上break;不,能及时终止
任务2:
源代码:
#include<stdio.h> int sum_digits(int n); int main(){ int n; int ans; while(printf("Enter n:"),scanf("%d",&n)!=EOF){ ans=sum_digits(n); printf("n = %d, ans = %d\n\n",n,ans); } return 0; } int sum_digits(int n){ int ans=0; while(n!=0){ ans+=n%10; n/=10; } return ans; }
问题1:将输入的数的各位上的数相加并输出
问题2:能,第一种运用了迭代的算法思维,第二种运用了递归的算法思维
任务3:
源代码:
#include<stdio.h> int power(int x,int n); int main(){ int x,n; int ans; while(printf("Enter x and n:"),scanf("%d%d",&x,&n)!=EOF){ ans=power(x,n); printf("n = %d,ans = %d\n\n",n,ans); } return 0; } int power(int x,int n){ int t; if(n==0) return 1; else if(n%2) return x*power(x,n-1); else{ t=power(x,n/2); return t*t; } }
问题1:计算x的n次幂
问题2:是
任务4:
源代码:
1 #include<stdio.h> 2 #include<math.h> 3 int is_prime(int n){ 4 int i; 5 if(n<2){ 6 return 0; 7 } 8 for(i=2;i<=sqrt(n);i++){ 9 if(n%i==0){ 10 return 0; 11 } 12 } 13 return 1; 14 } 15 int main(){ 16 int n; 17 int i=0; 18 printf("100以内的孪生素数:\n"); 19 for(n=0;n<=100;n++){ 20 if(is_prime(n)&&is_prime(n+2)){ 21 printf("%d %d\n",n,n+2); 22 i++; 23 } 24 } 25 printf("100以内的孪生素数共有%d个.",i); 26 return 0; 27 }
任务5:
源代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 unsigned int count=0; 4 void hanoi(unsigned int n,char from,char temp,char to); 5 void moveplate(unsigned int n,char from,char to); 6 int main(){ 7 unsigned int n; 8 while((scanf("%u",&n))!=EOF) 9 { 10 hanoi(n,'A','B','C'); 11 printf("\n一共移动了%d次.\n\n",count); 12 count=0; 13 } 14 system("pause"); 15 return 0; 16 } 17 void hanoi(unsigned int n,char from,char temp,char to) 18 { 19 count++; 20 if(n==1) 21 moveplate(n,from,to); 22 else 23 { 24 hanoi(n-1,from,to,temp); 25 moveplate(n,from,to); 26 hanoi(n-1,temp,from,to); 27 } 28 } 29 void moveplate(unsigned int n,char from,char to) 30 { 31 printf("%u:%c-->%c\n",n,from,to); 32 }
任务6:
源代码1(迭代):
1 #include <stdio.h> 2 int func(int n, int m); 3 int main(){ 4 int n, m; 5 int ans; 6 7 while(scanf("%d%d", &n, &m) != EOF) { 8 ans = func(n, m); 9 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 10 } 11 12 return 0; 13 } 14 int func(int n,int m){ 15 int i,j; 16 int up=1; 17 int down=1; 18 for(i=n;i>=(n-m+1);i--){ 19 up*=i; 20 } 21 for(j=1;j<=m;j++){ 22 down*=j; 23 } 24 return up/down; 25 }
源代码2(递归):
1 #include <stdio.h> 2 int func(int n, int m); 3 int main(){ 4 int n, m; 5 int ans; 6 7 while(scanf("%d%d", &n, &m) != EOF) { 8 ans = func(n, m); 9 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 10 } 11 12 return 0; 13 } 14 int func(int n,int m){ 15 if(m==0||m==n){ 16 return 1; 17 } 18 if(m>n){ 19 return 0; 20 } 21 return func(n-1,m-1)+func(n-1,m); 22 }
任务7:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 void print_charman(int n); 4 int main() { 5 int n; 6 7 printf("Enter n: "); 8 scanf("%d", &n); 9 print_charman(n); 10 11 return 0; 12 } 13 void print_charman(int n){ 14 int i,j,k,s; 15 s=n; 16 for(j=0;j<s;j++){ 17 for(k=0;k<j;k++){ 18 printf(" "); 19 } 20 for(i=0;i<(2*n-1);i++){ 21 printf(" O "); 22 } 23 printf("\n"); 24 for(k=0;k<j;k++){ 25 printf(" "); 26 } 27 for(i=0;i<(2*n-1);i++){ 28 printf("<H> "); 29 } 30 printf("\n"); 31 for(k=0;k<j;k++){ 32 printf(" "); 33 } 34 for(i=0;i<(2*n-1);i++){ 35 printf("I I "); 36 } 37 printf("\n"); 38 n--; 39 } 40 }
标签:return,int,printf,char,实验,ans,include From: https://www.cnblogs.com/wjz08/p/18497845