首页 > 其他分享 >实验3

实验3

时间:2024-10-29 16:30:48浏览次数:3  
标签:10 int printf char 实验 ans include

实验一:

 1 #include <stdio.h>
 2 
 3 char score_to_grade (int score);
 4 
 5 int main() {
 6 int score;
 7 char grade;
 8 
 9 while(scanf("%d", &score) != EOF) {
10 grade = score_to_grade(score);
11 printf("分数: %d, 等级: %c\n\n", score, grade);
12 }
13 
14 return 0;
15 }
16 
17 
18 char score_to_grade(int score) {
19 char ans;
20 
21 switch(score/10) {
22 case 10:
23 case 9:    ans = 'A'; break;
24 case 8:    ans = 'B'; break;
25 case 7:    ans = 'C'; break;
26 case 6:    ans = 'D'; break;
27 default:   ans = 'E';  
28 }
29 
30 return ans;
31 }

 

 

实验二:

 1 #include<stdio.h>
 2 
 3 int sum_digits(int n);
 4 
 5 int main() {
 6 int n;
 7 int ans;
 8 
 9 while(printf("Enter n: "), scanf("%d", &n) != EOF) {
10 ans = sum_digits(n);
11 printf("n = %d, ans = %d\n\n", n, ans);
12 }
13 
14 return 0;
15 }
16 
17 
18 int sum_digits(int n) {
19 int ans = 0;
20 while(n !=0) {
21 ans += n % 10;
22 n /= 10;
23 }
24 
25 return ans;
26 }

实验3 

power是计算x的n次放

power是递归函数

实验4:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<time.h>
 4 #include<stdlib.h>
 5 int is_prime(int n);
 6 int main()
 7 {
 8     int i,sum=0;
 9     printf("100以内的孪生素数:\n");
10     for(i=2;i<=98;++i)
11     {
12         if(is_prime(i)&&is_prime(i+2))
13         {
14             printf("%d %d\n",i,i+2);
15             sum+=1;
16         }
17         else
18         {
19             continue;
20          }
21 
22 
23     }
24        printf("100以内的孪生素数共有%d组\n",sum);
25 
26    return 0;
27 }
28 
29 
30  int is_prime(int n) {
31        if (n <= 1) {
32            return 0;
33        }
34        for (int i=2; i<=sqrt(1.0*n); i++) {
35            if (n % i == 0) {
36                return 0;
37            }
38        }
39        return 1;
40    }

实验五:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<time.h>
 4 #include<stdlib.h>
 5 unsigned int move_count = 0;
 6 void hanoi(unsigned int n, char from, char temp, char to);
 7 void moveplate(unsigned int n, char from, char to);
 8 int main()
 9 {
10  unsigned int n;
11 while(scanf("%u",&n)!=EOF)
12 {
13     move_count = 0;
14      hanoi(n, 'A','B','C');
15      printf("一共移动了: %u次\n", move_count);
16 getchar();
17 }
18 
19  return 0;
20 }
21 
22 
23 void hanoi(unsigned int n, char from, char temp, char to)
24 {
25 
26 if(n==1)
27 moveplate(n, from, to);
28 else
29 {
30 hanoi(n-1,from, to, temp);
31  moveplate(n, from, to);
32 hanoi(n-1,temp,from, to);
33 }
34 }
35 
36 
37 void moveplate(unsigned int n, char from, char to)
38 {
39     printf(" %u:%c-->%c\n", n, from, to);
40 
41     move_count++;
42 }

实验六:

 1 1 #include<stdio.h>
 2  2 #include<math.h>
 3  3 #include<time.h>
 4  4 #include<stdlib.h>
 5  5 int func(int n,int m);
 6  6 
 7  7 int main()
 8  8 {
 9  9     int n,m;
10 10     int ans;
11 11     while(scanf("%d%d",&n,&m)!=EOF)
12 12     {
13 13         ans=func(n,m);
14 14         printf("n=%d,m=%d,ans=%d\n\n",n,m,ans);
15 15         
16 16     }
17 17     
18 18     return 0;
19 19 }
20 20 
21 21 
22 22 int func(int n,int m)
23 23 {
24 24     25     if(m==0||m==n)
25 26     return 1;
26 27     else if(n<m)
27 28     return 0;
28 29     else 
29 30     return func( n-1, m)+func( n-1,m-1);
30 31      
31 32     
32 33     
33 34 }

实验7:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<time.h>
 4 #include<stdlib.h>
 5 void print_charman(int n);
 6 int k;
 7 int main()
 8 {
 9     int n;
10     printf("Enter n:");
11     scanf("%d",&n);
12     print_charman(n);
13 
14 
15 
16     return 0;
17 }
18 
19 
20 void print_charman(int n)
21 {
22     int i,j;
23      if (n <= 0)
24          return ;
25      for(i = 0;i<k;i++)
26          printf("\t");
27     for (j = 0;j<2*n-1;j++)
28              printf(" O      ");
29 
30 
31     printf("\n");
32 
33      for(i = 0;i<k;i++)
34          printf("\t");
35     for(i=1;i<=2*n-1;++i)
36     {
37         printf("<H>");
38             printf("\t");
39 
40     }
41         printf("\n");
42 
43      for(i = 0;i<k;i++)
44          printf("\t");
45     for(i=1;i<=2*n-1;++i)
46     {
47         printf("I I");
48             printf("\t");
49 
50 
51     }
52         ++k;
53         printf("\n");
54         print_charman(n-1);
55 
56 }

 

标签:10,int,printf,char,实验,ans,include
From: https://www.cnblogs.com/cxc666/p/18513768

相关文章

  • 记一个mysql 主从切换的实验记录
    一、前言msyql切换主库一般都是用高可用方案,例如MHA,MGR,高可用方案可以自动切主,但是MHA的failover功能无法把旧主自动挂到新主上作为从库使用,所以发生failover后,还需要手动把旧的主库作为备库挂到新的主库上。此时常规操作就是备份新主库,然后应用到旧主库上,然后再重新搭建主从。......
  • 实验2 类和对象 基础编程1
    实验任务1:源代码t.h:点击查看代码#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:  T(intx=0,inty=0); //普通构造函数  T(constT&t); //复制构造函数  T(T&&t);   //移动构造函数  ~T();     //析......
  • 实验3
    实验任务1:1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;67while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf("分数:%d,......
  • 实验三
    实验一:源代码:1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;67while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf(&quo......
  • 《DNK210使用指南 -CanMV版 V1.0》第三十四章 image图像滤波实验
    第三十四章image图像滤波实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)......
  • 实验3(2.0)
    任务1#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,g......
  • 实验3
    任务一#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%......
  • 实验3
    任务11#include<stdio.h>23charscore_to_grade(intscore);//函数声明45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);//函数调用11......
  • HNU-操作系统实验lab6-2022级
    实验目的任务调度是操作系统的核心功能之一。UniProton实现的是一个单进程支持多线程的操作系统。在UniProton中,一个任务表示一个线程。UniProton中的任务为抢占式调度机制,而非时间片轮转调度方式。高优先级的任务可打断低优先级任务,低优先级任务必须在高优先级任务挂起或......
  • 实验3
    task1#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",s......