实验任务一
源代码:
#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; }
运行结果:
答案一:设计一个独立的可复用函数,根据输入的数字来评判成绩等级
答案二:有,在后续情况函数中没有【break】的情况下,最后的结果会遍历,导致所有的结果都是E
实验任务二:
源代码:
#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; }
运行结果:
答案一:将所输入数字的每一位加起来
答案二:能替换。前一个是迭代思维,后一个是递归思维
实验任务三:
源代码:
`#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 tt;
}
}`
运行结果:
答案一:算出给定数字x的n平方
答案二:
数学公式模型为
power(x, 0)= 1;
当n为奇数时,power(x, n)= x power(x, n -1);
当n为偶数时,power(x, n)= power(x, n/2)* power(x, n/2)。
实验任务四:
源代码:
`#include <stdio.h>
include <math.h>
int is_prime(int num){
if (num <= 1) return 0;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int count = 0;
printf("100以内的孪生素数: \n");
for (int i = 2; i < 100; i++) {
if (is_prime(i)&&is_prime(i+2)) {
printf("%d %d\n", i, i+2);
count++;
}
}
printf("100以内的孪生素数共有 %d个\n", count);
return 0;
}`
运行结果:
实验任务五:
源代码:
`#include <stdio.h>
include <stdlib.h>
void hanoi(unsigned int n, char from, char temp, char to, int *count);
void moveplate(unsigned int n, char from, char to, int *count);
int main() {
unsigned int n;
while (scanf("%u", &n)!= EOF) {
int count = 0;
hanoi(n, 'A', 'B', 'C', &count);
printf("一共移动了%d次。\n", count);
}
system("pause");
return 0;
}
void hanoi(unsigned int n, char from, char temp, char to, int *count) {
if (n == 1) {
moveplate(n, from, to, count);
} else {
hanoi(n - 1, from, to, temp, count);
moveplate(n, from, to, count);
hanoi(n - 1, temp, from, to, count);
}
}
void moveplate(unsigned int n, char from, char to, int count) {
printf("%u:%c-->%c\n", n, from, to);
(count)++;
}`
运行结果:
实验任务六:
迭代法源代码:
#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 x=1; if(n<m) x=0; if(m==0) x=1; if(n>=m&&m!=0){ int a=1,b=1,c=1; for (int i = 2; i <= n; i++) { a*= i; } for (int j = 2; j <= m; j++) { b*= j; } for (int k = 2; k <= n-m; k++) { c*= k; } x=(a/b)/c; } return x; }
运行结果:
递归法源代码:
`#include <stdio.h>
int func_recursive(int n, int m) {
if (m > n || m < 0) return 0;
if (m == 0 || m == n) return 1;
return func_recursive(n - 1, m - 1) + func_recursive(n - 1, m);
}
int main() {
int n, m;
int ans;
while (scanf("%d%d", &n, &m)!= EOF) {
ans = func_recursive(n, m);
printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
}
return 0;
}`
运行结果:
实验任务七:
源代码:
`#include <stdio.h>
include <stdlib.h>
int print_charman(int n);
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
print_charman(n);
return 0;
}
int print_charman(int n){
for(int t=0;t<n;t++){
int i=(n-t)2-1;
int j=t;
for(j;j>0;j--){
printf(" ");
}
for(i;i>0;i--){
printf(" O ");
}
printf("\n");
j=t;
for(j;j>0;j--){
printf(" ");
}
i=(n-t)2-1;
for(i;i>0;i--){
printf("
}
printf("\n");
j=t;
for(j;j>0;j--){
printf(" ");
}
i=(n-t)*2-1;
for(i;i>0;i--){
printf("I I ");
}
printf("\n");
}
return 0;
}`
运行结果: