任务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会导致所有输入的输出结果都为最后一种情况
任务2:
源代码:
#include<stdio.h> int sumdigits(int n); int main() { int n; int ans; while(printf("Enter n:"),scanf("%d",&n) !=EOF) { ans=sumdigits(n); printf("n=%d,ans=%d \n\n",n,ans); } return 0; } int sumdigits(int n) { int ans =0; while(n!=0) { ans+=n%10; n/=10; } return ans; }
问题1:该函数的作用是将一个整数所有数位上的数取出并相加
问题2:能实现。第一种是从个位开始一个一个提取数字并消去这个数位,第二种是将数位上提取出来的的数字累加并再次调用函数消去数位。
任务3:
源代码
#define _CRT_SECURE_NO_WARNINGS #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:power的功能是计算x的n次方
问题2:该函数求次方的方法就是一步一步减小指数的大小。比如将一个四次方转化成两个二次方相乘,再将二次方转化成两个一次方相乘,遇到奇数次方则将这个数与其减一次方相乘以达到次方为偶数。
任务4:
源代码:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<iostream> using namespace std; int is_prime(int n) { int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { count++; } } if (count == 2) { return 1; } else { return 0; } } int main() { int count = 0; cout << "一百以内的孪生素数:" << endl; for (int i = 1; i <= 100; i++) { switch (is_prime(i)) { case 1: if (is_prime(i) == is_prime(i + 2)) { cout << i << " " << i + 2 << endl; count++; break; } else { break; } case 0: break; } } cout << "100以内的孪生素数一共有" << count << "个" << endl; return 0; }
实验5:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<iostream> using namespace std; void hanoi_move(int n, char A, char B, char C) { int count = 0; if (n > 1) { hanoi_move(n - 1, A, C, B); printf("%c -> %c\n", A, C); hanoi_move(n - 1, B, A, C); count++; } else { printf("%c -> %c\n", A, C); count++; } } int main() { int n; while (scanf("%d", &n) != EOF) { hanoi_move(n, 'A', 'B', 'C'); } return 0; }
这里count计数器插入在哪里都不管用,求解
实验6:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<iostream> using namespace std; int func(int n, int m) { if (m == 0 || m == n) { return 1; } else if(n<m) { return 0; } else { int x = n - m; int j, k, all; j = 1; k = 1; for (int i = n; i >= x + 1; i--) { j = j * i; } for (int ss = m; ss >= 1; ss--) { k = k * ss; } all = j / k; return all; } } // 函数声明 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; }
实验7:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include<iostream> using namespace std; void print_charman(int n) { int ma = n * 2 - 1; for (int i = n; i >=1; i--) { for (int j = 3; j > 0; j--) { for (int m = ma; m > 0; m--) { cout << " O " << " "; }cout << endl; for (int m = ma; m > 0; m--) { cout << "<H>" << " "; }cout << endl; for (int m = ma; m > 0; m--) { cout << "I I" << " "; }cout << endl; ma = ma - 2; if (ma == -1) { break; } } } } int main() { int n; printf("Enter n: "); scanf("%d", &n); print_charman(n); // 函数调用 return 0; }
空格不会整了求解