1 #include <stdio.h> 2 3 char score_to_grade(int score); 4 5 int main() { 6 int score; 7 char grade; 8 9 while(scanf("%d", &score) != EOF) { 10 grade = score_to_grade(score); 11 printf("分数: %d, 等级: %c\n\n", score, grade); 12 } 13 14 return 0; 15 } 16 17 18 char score_to_grade(int score) { 19 char ans; 20 21 switch(score/10) { 22 case 10: 23 case 9: ans = 'A'; break; 24 case 8: ans = 'B'; break; 25 case 7: ans = 'C'; break; 26 case 6: ans = 'D'; break; 27 default: ans = 'E'; 28 } 29 30 return ans; 31 }
函数的功能是区分等级
形参类型是整型,返回值是ACSLL
有问题,在找到对应的答案之后还会继续向下运行,不会停止
任务二
#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; }
将n中的数字相加求和
能实现相同的效果,一种是迭代的思想,一种是递归的思想
任务三
1 #include <stdio.h> 2 3 int power(int x, int n); 4 5 int main() { 6 int x, n; 7 int ans; 8 9 while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) { 10 ans = power(x, n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 14 return 0; 15 } 16 17 18 int power(int x, int n) { 19 int t; 20 21 if(n == 0) 22 return 1; 23 else if(n % 2) 24 return x * power(x, n-1); 25 else { 26 t = power(x, n/2); 27 return t*t; 28 } 29 }
计算
是递归函数
计算x的n次方
任务四
1 #include<stdio.h> 2 3 int is_prime(int); 4 5 int main(){ 6 int cnt=0,i; 7 printf("100以内的孪生素数:\n"); 8 for(i=1;i<100;i++){ 9 if(is_prime(i)&&is_prime(i+2)){ 10 cnt+=1; 11 printf("%d %d\n",i,i+2);} 12 } 13 14 printf("100以内的孪生素数共有%d个。",cnt); 15 16 return 0; 17 18 19 } 20 21 22 int is_prime(int n){ 23 int m,ans; 24 for(m=2;m<=n/2;m++) 25 { 26 if(n%m==0) 27 { 28 ans=0; 29 break; 30 } 31 else 32 ans=1; 33 } 34 return ans; 35 36 }
任务五
1 #include<stdio.h> 2 #include<stdlib.h> 3 void hanoi(unsigned int n,char from,char temo,char to); 4 void moveplate(unsigned int n,char from,char to); 5 int main(){ 6 unsigned int n; 7 while(scanf("%u",&n)!=EOF){ 8 hanoi(n,'A','B','C'); 9 extern int count; 10 printf("\n一共移动了%d.\n\n",count); 11 count=0; 12 } 13 14 return 0; 15 } 16 void hanoi(unsigned int n,char from,char temp,char to) 17 { 18 if(n==1) 19 moveplate(n,from,to); 20 else 21 { 22 hanoi(n-1,from,to,temp); 23 moveplate(n,from,to); 24 hanoi(n-1,temp,from,to); 25 } 26 } 27 int count=0; 28 void moveplate(unsigned int n,char from,char to) 29 { 30 printf("%u:%c-->%c\n",n,from,to);\ 31 count+=1; 32 }
任务六
1 #include<stdio.h> 2 int func(int n,int m); 3 4 int main(){ 5 int m,n; 6 int ans; 7 8 while(scanf("%d%d",&n,&m)!=EOF){ 9 ans=func(n,m); 10 printf("n=%d,m=%d,ans=%d\n\n",n,m,ans); 11 } 12 return 0; 13 14 } 15 16 int func(int n,int m){ 17 int i,j=1,p=1,ans; 18 for(i=n;i>=n-m+1;i--) 19 j*=i; 20 for(i=m;i>=1;i--) 21 p*=i; 22 ans=j/p; 23 return ans; 24 }
1 #include<stdio.h> 2 int func(int n,int m); 3 4 int main(){ 5 int m,n; 6 int ans; 7 8 while(scanf("%d%d",&n,&m)!=EOF){ 9 ans=func(n,m); 10 printf("n=%d,m=%d,ans=%d\n\n",n,m,ans); 11 } 12 return 0; 13 14 } 15 16 int func(int n,int m){ 17 int ans=1; 18 if(m==0||m==n) 19 return 0; 20 else if(m>n) 21 return 0; 22 else 23 return ans=func(n-1,m)+func(n-1,m-1); 24 }
任务七
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 char print_charman(int n); 5 6 int main() { 7 int n; 8 9 printf("Enter n: "); 10 scanf("%d", &n); 11 print_charman(n); 12 13 return 0; 14 } 15 16 char print_charman(int n) 17 { 18 int p,m,t; 19 for(p=n;p>0;--p){ 20 for(t=0;t<n-p;++t) 21 { 22 printf("\t"); 23 } 24 for(m=2*p-1;m>0;--m) 25 { 26 printf(" O \t"); 27 28 } 29 printf("\n"); 30 for(t=0;t<n-p;++t) 31 { 32 printf("\t"); 33 } 34 for(m=2*p-1;m>0;--m) 35 { 36 printf("<H>\t"); 37 38 } 39 printf("\n"); 40 for(t=0;t<n-p;++t) 41 { 42 printf("\t"); 43 } 44 for(m=2*p-1;m>0;--m) 45 { 46 printf("I I\t"); 47 48 } 49 printf("\n"); 50 51 } 52 }
标签:return,int,ans,char,实验,printf,include From: https://www.cnblogs.com/nnnm/p/18497755