首页 > 其他分享 >实验3

实验3

时间:2024-10-27 17:42:14浏览次数:5  
标签:return int ans char 实验 printf 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 }

函数的功能是区分等级

形参类型是整型,返回值是ACSLL

有问题,在找到对应的答案之后还会继续向下运行,不会停止

任务二

 

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

将n中的数字相加求和

能实现相同的效果,一种是迭代的思想,一种是递归的思想

任务三

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

计算

是递归函数

计算x的n次方

 

任务四

 1 #include<stdio.h>
 2 
 3 int is_prime(int);
 4 
 5 int main(){
 6     int cnt=0,i;
 7     printf("100以内的孪生素数:\n");
 8     for(i=1;i<100;i++){
 9         if(is_prime(i)&&is_prime(i+2)){
10             cnt+=1;
11             printf("%d  %d\n",i,i+2);}
12     }
13     
14     printf("100以内的孪生素数共有%d个。",cnt);
15     
16     return 0;
17     
18     
19 }
20 
21 
22 int is_prime(int n){
23      int m,ans;
24     for(m=2;m<=n/2;m++)
25     {
26         if(n%m==0)
27         {
28         ans=0;
29         break;
30 }
31         else
32         ans=1;
33     }
34     return ans;
35 
36 }

任务五

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

 任务六

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

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

任务七

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

 

标签:return,int,ans,char,实验,printf,include
From: https://www.cnblogs.com/nnnm/p/18497755

相关文章

  • 实验3
    任务一源代码1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;6while(scanf("%d",&score)!=EOF){7grade=score_to_grade(score);8printf("分数:%d,等级:%c\n\n",score,grade......
  • 实验1
    Task1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usin......
  • 实验3
    #include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%c\n\......
  • 实验2(2)
    Task1://t.hpragmaonceinclude//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);~T();//移动构造函数//析构函数voidadjust(intratio);voiddisplay()const;private:int......
  • 实验干货|电流型霍尔传感器采样设计03-信号调理
    在前两篇博客中,将霍尔输出的电流信号转换成了有正有负的电压信号,但是DSP需要采集0~3V的电压信号,因此需要对信号缩放并抬升至全部为正的信号。常见的方法是,通过比例放大(缩小)电路对信号进行放缩,通过加法电路抬升基准电平。这里分为两步,首先设计基准电平。设计基准电平DSP的A......
  • 实验2
    Task1://t.hpragmaonceinclude//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);~T();//移动构造函数//析构函数voidadjust(intratio);voiddisplay()const;private:in......
  • SQL sever 索引实验
    1.用sql语言创建数据库DB_test,代码参考https://blog.csdn.net/m0_75112455/article/details/143257034?spm=1001.2014.3001.5502创建成功2.进行数据的导入(1)下载TPC-H文件(2)将tbl文件导入到数据库useDB_test;GOdroptablePART;CREATETABLEPART(P_PARTKEYintp......
  • 实验3
    任务11#include<stdio.h>2#include<math.h>3#include<time.h>4#include<stdlib.h>5charscore_to_grade(intscore);678intmain()9{10intscore;11chargrade;12while(scanf("%d",&s......
  • 实验3
    任务11#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf(&......
  • 实验3
    任务1原代码1#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf("......