实验三

实验一

源代码

#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;
}

运行结果

 问题回答

无break,所以程序不终止,继续进行default的程序输出

取整10,得到该数字的十位数

实验二

源代码

#include<stdio.h>

int sum_dights(int n);
int main(){
    int n;
    int ans;
    while(printf("Enter n: "),scanf("%d",&n) != EOF){
        ans = sum_dights(n);
        printf("n = %d, ans = %d\n\n",n, ans);
    }
    return 0;
    
}
int sum_dights(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.是递归函数,因为他用函数一直推到一

3.n = 0, 输出1

n为偶数,n除以2后先算x的二分之n次方,再相乘·

n为奇数,先算x的(n-1)次方

实验四

源代码

#include<stdio.h>
#include<math.h>
int main(){
	int n;
	int is_prime( int n);
	int num = 0;
	printf("100以内的孪生素数:\n");
	for(int n = 1;n < 100; n++){
	if(is_prime(n) && is_prime(n + 2)){
		printf("%d %d\n",n ,n + 2);
		num = num + 1;
	}
	}
	printf("100以内的孪生素数共有%d个\n",num);
	return 0;
}
int is_prime(int n){
	if(n <= 1){
		return 0;
		
	}
	int i = 2;
	for( i <= sqrt(n); i++);
{
	if(n % 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 sum = 0;
int main(){
    unsigned int n;
    while(scanf("%d",&n) != EOF){ 
        hanoi(n,'A','B','C');
        printf("一共移动了%d次\n",sum);
        sum = 0;
        }
    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);
     sum = sum + 1;
}

 

运行结果

 

实验六

源代码迭代

#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 a,b=1,c=1;
   if (m > n)
   return 0;
   for(a = 0;a < m; a++)
    {
    	b = b * n--;
    	b = b / c++;}
    	
    	
	
    return b;
}

  

源代码递归

#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(m==0||m==n)
    return 1;
    else if(m>n)
    return 0;
    else
    return func(n-1,m)+func(n-1,m-1);
    
}

  

运行结果

 

实验七

源代码

运行结果

 

#include<stdio.h>
#include<stdlib.h>

void print_charman(int n);
 
int main(){
	int n,i;
	printf("Enter n:");
	scanf("%d",&n);
	print_charman(n);
	
	return 0;
}
void print_charman(int n){
	int i,j;
	for(j = 0; j < n;j++){    
for(i=1;i<=j;i++)
    printf("   \t");
for (i=1;i<=2*(n-j)-1;i++)
    printf("O \t ");
printf("\n");
for(i=1;i<=j;i++)
    printf("   \t");
for (i=1;i<=2*(n-j)-1;i++)
    printf("<H>\t");
printf("\n");    
for(i=1;i<=j;i++)
    printf("   \t");
    for (i=1;i<=2*(n-j)-1;i++)
    printf("I I\t");
printf("\n");
}
}

  

 

 

posted @ 2024-10-30 15:23  liusihan  阅读(3)  评论(0编辑  收藏  举报