task.1 代码:
#include<stdio.h> #include<stdlib.h> #include<math.h> char score_to_grade(int x); 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'; break; } return ans; }
图片:
问题一:功能:将输入分数转化为相应等级返回,形参类型:int,返回值:char
问题二:有问题,当case进行结果时不会停止,导致ans值总以E返回
task.2
代码:
#include<stdio.h> int sum_digits(int x); int main() { int x,y; while(printf("Enter n:"),scanf("%d",&x)!=EOF) { y=sum_digits(x); printf("x=%d,y=%d\n",x,y); } return 0; } int sum_digits(int x) { int ans=0; while(x!=0) { ans+=x%10; x/=10; } return ans; }
图片
问题一:将输入数字每一分位上的数字相加
问题二:能,利用递归函数不断将数字每一分位数字剥离相加
task.3
代码:
#include<stdio.h> int power(intx, inty); int main() { int x, n, 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(x, 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:是,y=x^n
task.4
代码:
#include<stdio.h> #include<stdlib.h> int is_prime(int x); int main() { printf("100以内的素数:\n"); int x = 1, y = 3, ans1, ans2, s = 0; while (x <= 100) { ans1 = is_prime(x); ans2 = is_prime(y); if (ans1 == 1 && ans2 == 1) { printf("%d %d\n", x, y); s++; } x++; y++; } printf("100以内的孪生素数有:%d", s); return 0; } int is_prime(int x) { int n = 2; if (x == 1) return 0; while (n <= x / 2) { if (x % n != 0) n++; else { return 0; break; } } return 1; }
图片
task.5
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> void move(int n, char from, char to); void hanoi(int n, char from, char temp, char to); int s = 0; int main() { int n; while (scanf("%d", &n) != EOF) { hanoi(n, 'A', 'B', 'C'); printf("一共移动了%d次", s); } return 0; } void hanoi(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(int n, char from, char to) { printf("%d:%c->%c\n", n, from, to); s++; }
task.6
代码
#define _CRT_SECURE_NO_WARNINGS #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 s; int t = n - m + 1; for (s = 1; n >= t; n--) s *= n; for (; m >= 1; m--) s /= m; return s; } /*int func(int n, int m) { int s = 0; if (m > n) s = 0; else if (m == n) s = 1; else if (m > 0 && n > 1) s = func(n - 1, m) + func(n - 1, m - 1); else s = 1; return s; } */
图片
task.7
代码
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> void print_charman(int n); int a; int main() { int n; printf("Enter n: "); scanf("%d", &n); a = n; print_charman(n); return 0; } void print_charman(int n) { if (n > 0) { int s, z; for (s = 2 * n - 1; s > 0; s--) printf(" O \t"); printf("\n"); for (z = a - n ; z > 0; z--) printf("\t"); for (s = 2 * n - 1; s > 0; s--) printf("<H>\t"); printf("\n"); for (z = a - n; z > 0; z--) printf("\t"); for (s = 2 * n - 1; s > 0; s--) printf("I I\t"); printf("\n\n"); for (z=a-n+1; z > 0; z--) printf("\t"); print_charman(n - 1); } }
图片
标签:return,int,printf,char,实验,ans,include From: https://www.cnblogs.com/frogwithtrousers/p/18499749