任务1
#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 符合一种情况后,程序并未终止,继续运行直至最后一条,因此无论输入任何值都,ans 的值都会返回为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 第一种实现方法利用的是迭代法而第二种利用的是递归法
实验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/2); else { t=power(x,n/2); return t*t; } }
问题1 计算x的n次幂
问题2
任务4
#include <stdio.h> #include <math.h> int is_prime(int n); int main(){ int n ,c=0 ; for (n=2;n<=100;n++){ if(is_prime(n)&&is_prime(n+2)){ printf("%d %d\n",n,n+2); c++; } } printf ("100以内的孪生素数共有%d对\n",c); return 0; } int is_prime(int n){ int m; for(m=2;m<=sqrt(1.0*n);m++) if(n%m==0) return 0; return 1; }
任务5
#include<stdio.h>
#include<stdlib.h>
int c=0;
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int main(){
unsigned int n;
while(scanf("%u",&n) !=EOF){
c=0;
hanoi(n,'A','B','C');
printf("一共移动了%d次\n",c);
}
return 0;
}
void hanoi(unsigned int n,char from,char temp,char to){
if(n==1) {
moveplate(n,from,to);
}
else{
hanoi(n-1,from,to,temp);
moveplate(n,from,to);
hanoi(n-1,temp,from,to);
}
}
void moveplate(unsigned int n,char from,char to){
printf("%u;%c-->%c\n",n,from,to);
++c;
}
任务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){ if(n<m) return 0; else if(n==m||m==0) return 1; else{ int a,b,x=1,y=1; for(a=0;a<m;a++){ x*=n-a; } for(b=0;b<m;b++){ y*=m-b; } return (x/y); } }
递归
#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; if(m==0) return 1; return func(n-1,m)+func(n-1,m-1); }
任务7
#include <stdio.h> #include <stdlib.h> void print_charman(int n); int main(){ int n; printf("Enter :"); scanf("%d",&n); print_charman(n); return 0; } void print_charman(int n){ int i,m,c,k=0; n=2*n-1; while(n>0){ m=n; for(c=0;c<k;c++){ printf(" "); } for(c=0;c<m;c++){ printf(" o "); printf(" "); } printf("\n"); for(c=0;c<k;c++){ printf(" "); } for(c=0;c<m;c++){ printf("<H>"); printf(" "); } printf("\n"); for(c=0;c<k;c++){ printf(" "); } for(c=0;c<m;c++){ printf("I I"); printf(" "); } printf("\n"); i=0; n=n-2; k++; } }
标签:return,int,ans,char,score,实验,printf,2.0 From: https://www.cnblogs.com/limo2024/p/18511860