实验3
task 1
#include <stdio.h> char score_to_grade(int score); int main() { int score; char grade; while(scanf("%d", &score) != EOF) { grade = score_to_grade(score); printf("分数: %d, 等级: %c\n\n", score, grade); } 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去除后 执行一个语句 输出对应等地时会接着输出之后的等第;双引号变为单引号 输出不了字符等第
task 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:可以有同等效果 第一种是迭代方式 第二种是递归方式
task 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:是递归函数
task 4
#include <stdio.h> int is_prime(int n); int main() { int count = 0; int n; printf("100 以内的孪生素数:\n"); for (n = 1; n< 100; n++) { if (is_prime(n) && is_prime(n + 2)) { printf("%d %d\n", n, n + 2); count++; } } printf("100 以内的孪生素数共有%d 个\n", count); return 0; } int is_prime(int n) { int i; if (n <= 1) return 0; for (i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; }
task 5
#include<stdio.h> #include<stdlib.h> int count=0; void hanoi(unsigned int n,char from,char temp,char to); void move(unsigned int n,char from,char to); int main(){ int n; while(scanf("%d",&n)!=EOF){ count=0; hanoi(n,'A','B','C'); printf("\n一共移动了%d次\n",count); } return 0; } void hanoi(unsigned int n,char from,char temp,char to) { if(n==1) move(n,from,to); else{ hanoi(n-1,from,to,temp); move(n,from,to); hanoi(n-1,temp,from,to); } } void move(unsigned int n,char from,char to) { printf("%d:%c-->%c\n",n,from,to); count++; }
task 6
#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; } int func(int n, int m) { int a,b, i; a = 1, b = 1; if (n < m) return 0; else if (n == m || m == 0) return 1; else { for (i = 1;i <= m;++i) a *= i; for (i = n; i >= n - m + 1;--i) b *= i; return b / a; } }
#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; } int func(int n, int m) { int a,b, i; a = 1, b = 1; if (n < m) return 0; else if (n == m || m == 0) return 1; else return func(n-1,m)+func(n-1,m-1); }
task 7
#include<stdio.h> int main() { int n; printf("Enter n:"); scanf_s("%d", &n); print_charman(n); return 0; } int print_charman(int n) { int i,j,a; for (i = n;i > 0;--i) { for (a = 0;a < n - i;a++) printf("\t"); for (j = 1;j <=(2*i-1);j++) { printf(" o \t"); } printf("\n"); for (a = 0;a < n - i;a++) printf("\t"); for (j = 1;j <= (2 * i - 1);j++) { printf("<H>\t"); } printf("\n"); for (a = 0;a < n - i;a++) printf("\t"); for (j = 1;j <= (2 * i - 1);j++) { printf("I I\t"); } printf("\n"); } }