#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:判断完之后不会终止,而是执行下一行,最终永远输出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; }
问题1:将输入数的每一位数字相加
问题2:能,一种是迭代计算答案,一种是递归计算答案
#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:是
#include<stdio.h> int is_prime(int n){ int i; for(i=2;n>=i;i++){ if(n%i==0&&n>i) return 0; else if(n%i==0&&n==i) return 1; } } int main(){ printf("100以内孪生素数:\n"); int num=2,a=0; while(num<100){ if(is_prime(num)==1&&is_prime(num+2)==1){ printf("%d %d\n",num,num+2); a++; } num++; } printf("100以内孪生素数共%d个",a); return 0; }
#include<stdio.h> int m=0; int hanoi(int n,char from,char by,char to){ if (n==2){ printf("1:%c-->%c\n",from,by); printf("2:%c-->%c\n",from,to); printf("1:%c-->%c\n",by,to); m+=3; return 0; } hanoi(n-1,from,to,by); printf("%d:%c-->%c\n",n,from,to); hanoi(n-1,by,from,to); m++; return 0; } int main(){ int a; while(scanf("%d",&a)!=EOF){ char from='A',to='C',by='B'; hanoi(a,from,by,to); printf("一共移动了%d次\n",m); m=0; } return 0; }
#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 up=n,down=m,i=m,j=n,l=n-m; if(n<m) return 0; else if(l==0||m==0) return 1; while(i!=1){ down=down*(i-1); i--; } while(j!=1){ up=up*(j-1); j--; } while(l!=0){ down=down*l; l--; } return up/down; }
#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){ if(n<m) return 0; else if(n==m||m==0) return 1; else if(m==1) return n; else return func(n-1,m)+func(n-1,m-1); }
#include <stdio.h> #include <stdlib.h> void print_1(int a,int b){ int i=1,j=b; while(j-1>0){ printf(" "); --j; } while(i<=(a*2-1-(b-1)*2)){ i++; printf(" O "); } printf("\n"); i=1;j=b; while(j-1>0){ printf(" "); --j; } while(i<=(a*2-1-(b-1)*2)){ i++; printf("<H> "); } printf("\n"); i=1;j=b; while(j-1>0){ printf(" "); --j; } while(i<=(a*2-1-(b-1)*2)){ i++; printf("I I "); } printf("\n"); } void print_charman(int n){ int i=1; while(i<=n){ print_1(n,i); i++; } } int main() { int n; printf("Enter n: "); scanf("%d", &n); print_charman(n); return 0; }
标签:return,函数,int,编程,C语言,while,score,ans,printf From: https://www.cnblogs.com/jhyzhanchuan/p/18511845