1
1 #include<stdio.h> 2 char score(int s); 3 int main() 4 { 5 int s; 6 char a; 7 while (scanf("%d", &s) != EOF) 8 { 9 a = score(s); 10 printf("分数:%d", s); 11 printf("等级:%c\n", a); 12 } 13 return 0; 14 } 15 16 char score(int s) 17 { 18 char a; 19 switch (s / 10) 20 { 21 case 10: 22 case 9:a = 'A'; break; 23 case 8:a = 'B'; break; 24 case 7:a = 'C'; break; 25 case 6:a = 'D'; break; 26 default:a = 'E'; 27 } 28 return a; 29 }
问题1:将分数转化为等级字母,参数类型是整数,返回值类型是字符型
问题2:有问题,如果不加不break,则一直输出,如当输入100则输出ABCDE
2
1 #include<stdio.h> 2 int sumd(int n); 3 int main(){ 4 int n; 5 int ans = 0; 6 while (printf("Enter n:"), scanf("%d", &n) != EOF) 7 { 8 ans = sumd(n); 9 printf("n=%d,ans=%d\n\n", n, ans); 10 } 11 return 0; 12 } 13 int sumd(int n) 14 { 15 int ans = 0; 16 while (n != 0) 17 { 18 ans += n % 10; 19 n /= 10; 20 } 21 return ans; 22 }
问题1:将一个数各个位置数字求和
问题2:可以得到各个位数和,但也同时将n变成逆过来排列
1 #include <stdio.h> 2 int power(int x, int n); 3 int main() { 4 int x, n; 5 int ans; 6 while (printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) { 7 ans = power(x, n); 8 printf("n = %d, ans = %d\n\n", n, ans); 9 } 10 return 0; 11 } 12 int power(int x, int n) { 13 int t; 14 if (n == 0) 15 return 1; 16 else if (n % 2) 17 return x * power(x, n - 1); 18 else { 19 t = power(x, n / 2); 20 return t * t; 21 } 22 }
问题1:该函数计算一个数的x次方
问题2
4
1 #include<stdio.h> 2 int isprime(int n) 3 { 4 int i; 5 if (n == 1) 6 { 7 return 0; 8 } 9 for (i = 2; i*i <= n ; i++) 10 { 11 if (n % i == 0) 12 { 13 return 0; 14 } 15 } 16 return 1; 17 } 18 int main() 19 { 20 int i, s; 21 s = 0; 22 for (i = 1; i < 100; i++) 23 { 24 if (isprime(i) && isprime(i + 2)) 25 { 26 printf("%d %d\n", i, i + 2); 27 s++; 28 } 29 } 30 printf("一共%d个孪生素数", s); 31 return 0; 32 }
标签:case,10,return,int,ans,实验,printf From: https://www.cnblogs.com/zhj910/p/18510413