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:会无限的运行其中符合条件的一个
实验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;
}
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){
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;
}
}
int power(int x,int n);
int main(){
int x,n;
int ans;
while(printf("Enter x and n:"),scanf("%d%d"),&x,&n){
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:是,用x乘上一项直至n为0终止
实验4:
代码:
#include<stdio.h>
int is_prime(int x);
int main(){
int i,n=100;
for(i=2;i<=n;++i)
if(is_prime(i)==1&&is_prime(i+2)==1){
printf("%d\t%d\n",i,i+2);}
return 0;
}
int is_prime(int x){
int i;
for(i=2;i<x;++i){
if(x%i==0){
return 0;}
}
return 1;
}
int is_prime(int x);
int main(){
int i,n=100;
for(i=2;i<=n;++i)
if(is_prime(i)==1&&is_prime(i+2)==1){
printf("%d\t%d\n",i,i+2);}
return 0;
}
int is_prime(int x){
int i;
for(i=2;i<x;++i){
if(x%i==0){
return 0;}
}
return 1;
}
图:
实验五
代码:
#include<stdio.h>
#include<stdlib.h>
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int count(int n);
int main(){
unsigned int n;
while(scanf("%u",&n)!=EOF)
hanoi(n,'A','B','C');
count(n);
system("pause");
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);
}
int count(int n){
if(n<=1){
return n;
return count(n-1);
return 2*count(n-1)+1;}
printf("%u",count(n));}
#include<stdlib.h>
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int count(int n);
int main(){
unsigned int n;
while(scanf("%u",&n)!=EOF)
hanoi(n,'A','B','C');
count(n);
system("pause");
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);
}
int count(int n){
if(n<=1){
return n;
return count(n-1);
return 2*count(n-1)+1;}
printf("%u",count(n));}
图:
实验六
代码:
#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 i,a=1,b=1,c=1,ans;
if(n>=m){
for(i=1;i<=n;++i){
a*=i;}
for(i=1;i<=m;++i){
b*=i;}
for(i=1;i<=n-m;++i){
c*=i;}
ans=a/b/c;
}
return ans;
}
图:
递归不会用
实验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,m;
m=n;
while(n>0){
for(i=0;i<2*n-1;++i){
printf(" o \t");}
printf("\n");
for(i=0;i<m-n;++i){
printf("\t");}
for(i=0;i<2*n-1;++i){
printf("<H>\t");}
printf("\n");
for(i=0;i<m-n;++i){
printf("\t");}
for(i=0;i<2*n-1;++i){
printf("I I\t");}
printf("\n");
n=n-1;
for(i=0;i<m-n;++i){
printf("\t");}
}
}
图: