任务一
源代码:
#include <stdio.h> char score_to_grade(int score); int main(){ int score; char garde; while(scanf("%d",&score) != EOF){ garde = score_to_grade(score); printf("分数:%d,等级:%c\n\n",score,garde); } return 0; } char score_to_grade(int score){ char ans; switch(score/10){ case 10: case 9: ans = 'A';break; case 8: ans = 'B';break; case 7: ans = 'C';break; case 6: ans = 'D';break; default: ans = 'E'; } return ans; }
运行结果:
问题1:将输入的分数进行等级划分,整型,字符型
问题2:缺少break终止语句导致case穿透,最终导致输出结果中含有后续情况的输出结果。且case6到10的输出结果不是字符型,导致程序运行错误。
任务二
源代码:
#include <stdio.h> 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:将输入的n各分位的数相加
问题2:可以实现同样的效果,第一个是将n/10放入while循环中,而第二个则是将n/10放入sum_digits函数中进行循环
任务三
源代码:
#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:是递归函数
数学公式模型:
任务四
源代码:
#include<stdio.h> #include<math.h> int is_prime(int x) { int i, k; k = sqrt(x); for(i=2;i<=k;i++) if(x%i==0) return 0; if(i>k) return 1; } int main(){ printf("100以内的孪生素数:\n"); int n=100; int m=0; int j; for(j=2;j<=n;j++){ if(is_prime(j)&&is_prime(j+2)){ m++; printf("%d %d\n",j,j+2); } } printf("100以内的孪生素数共有%d个\n",m); return 0; }
运行结果:
任务五
源代码:
#include<stdio.h> #include<stdlib.h> void hanoi(unsigned int n, char from,char temp, char to); void moveplate(unsigned int n ,char from, char to); int total=0; int main() { unsigned int n; while (scanf("%u",&n)!=EOF){ total = 0; hanoi(n, 'A', 'B', 'C'); printf("一共移动%d次",total); } return 0; } void hanoi(unsigned int n, char from,char temp, char to) { if(n==1) { moveplate(n, from, to); total++; } else { hanoi(n-1, from, to, temp); moveplate(n, from, to); hanoi(n-1,temp, from,to); total++; } } void moveplate(unsigned int n,char from, char to) { printf("%u:%c-->%c\n",n, from, to); }
运行结果;
任务六
迭代法:
#include <stdio.h> int func(int n, int m); int main(){ int n, m; int ans; while(scanf("%d%d", &n, &m) !=EOF){ ans = func(n, m); printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); } return 0; } func(int n, int m) { int i, j; int p=1, q=1; for(i=n-m+1;i<=n;i++){ p*=i;} for(j=1;j<=m;j++){ q*=j;} return p/q; }
递归法:
#include <stdio.h> int func(int n, int m); int main(){ int n, m; int ans; while(scanf("%d%d", &n, &m) !=EOF){ ans = func(n, m); printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); } return 0; } func(int n, int m) { if(m>n) return 0; else if(m==n||m==0) return 1; else return func(n-1, m)+func(n-1,m-1); }
任务七
源代码:
#include <stdio.h> #include <stdlib.h> void print_charman(int n); int main() { int n; printf("Enter n: "); scanf("%d", &n); print_charman(n); return 0; } void print_charman(int n){ int i,j,k; for(i=n;i>0;i--){ for(k=1;k<=n-i;k++){ printf(" \t"); } j=2*i-1; for(j;j>0;j--){ printf(" O \t"); } printf("\n"); for(k=1;k<=n-i;k++){ printf(" \t"); } j=2*i-1; for(j;j>0;j--){ printf("<H>\t"); } printf("\n"); for(k=1;k<=n-i;k++){ printf(" \t"); } j=2*i-1; for(j;j>0;j--){ printf("I I\t"); }printf("\n"); } }
运行结果:
输入3:
输入4:
标签:return,int,ans,char,实验,printf,include From: https://www.cnblogs.com/lzl060415/p/18497825