#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
实验2
#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:能够实现同样效果
第一种是在定义的函数内插入循环,将输入值的每一位在每一次循环中加到总值中,运用的是迭代
第二种是在定义的函数内再一次插入定义的函数,运用的是递归
实验3
#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,然后输出x的n次方
问题2:是递归函数,递归模式:n%2==0,ans=x^2/n *x^2/n直到最后的n=1,ans=x^n*1
n%2!=0,x^n=x*x^n-1,n-1为偶数,重复上述步骤
实验4
#include<stdio.h> int is_prime(int n) { for(int i=2;i<n;i++) while(n%i==0) { return 0; break; } return 1; } int main() { int count=0; printf("100以内的孪生素数:\n"); for(int n=2;n+2<100;n++) { if((is_prime(n))&&(is_prime(n+2))) { count++; printf("%d %d\n",n,n+2); } } printf("100以内的孪生素数共有%d个",count); return 0; }
实验5
#include<stdio.h> #include<stdlib.h> int count=0; void hanoi(unsigned int n,char from,char temp,char to); void move(unsigned int n,char from,char to); int main() { int n; while(scanf("%d",&n)!=EOF) { count=0; hanoi(n,'A','B','C'); printf("\n一共移动了%d次\n",count); } return 0; } void hanoi(unsigned 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(unsigned int n,char from,char to) { printf("%d:%c-->%c\n",n,from,to); count++; }
实验6
迭代:
#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 c=1,d=1,i,k; if(m==0) return 1; else if(n==m) return 1; else if(n<m) return 0; for(i=n;i>n-m;i--) { c=c*i; } for(k=1;k<m+1;k++) { d=d*k; } return c/d; }
递归:
#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) { if(m==0) return 1; else return func(n-1,m)+func(n-1,m-1); } else return 0; }
实验7:
#include <stdio.h> #include <stdlib.h> void print_charman(int n); int main() { int n; printf("Enter n: "); scanf("%d", &n); print_charman(n); return 0; } void print_charman(int n) { int i,k=0,j; for(i=n;i>0;i--) { for(j=0;j<k;j++) printf("\t"); for(j=1;j<2*i;j++) printf(" o \t"); printf("\n"); for(j=0;j<k;j++) printf("\t"); for(j=1;j<2*i;j++) printf("<H>\t"); printf("\n"); for(j=0;j<k;j++) printf("\t"); for(j=1;j<2*i;j++) printf("I I\t"); printf("\n"); k++; } }
标签:return,int,ans,char,score,实验,printf From: https://www.cnblogs.com/hexu7/p/18497805