首页 > 其他分享 >实验三

实验三

时间:2023-11-05 16:33:06浏览次数:29  
标签:10 int long char 实验 func include

1.test1

 1 #include <stdlib.h>
 2 #include <time.h>
 3 #include <windows.h>
 4 #define N 80
 5 #include<stdio.h>
 6 
 7 void print_text(int line,int col,char text[]);
 8 void print_spaces(int n);
 9 void print_blank_lines(int n);
10 
11 int main(){
12     int line,col,i;
13     char text[N]="hi,november~";
14     
15     srand(time(0));
16     
17     for(i=1;i<=10;++i){
18         line=rand()%25;
19         col=rand()%80;
20         print_text(line,col,text);
21         Sleep(1000);//暂停1000ms 
22     }
23     return 0;
24 }
25 
26 //打印空格 
27 void print_spaces(int n){
28     int i;
29     
30     for(i=1;i<=n;++i)
31     printf(" ");
32 }
33 
34 //打印n行空白行
35 void print_blank_lines(int n){
36     int i;
37     
38     for(i=1;i<=n;++i)
39     printf("\n");
40 } 
41 
42 //在第line行第col列打印一段文本
43 void print_text(int line,int col,char text[]) {
44     print_blank_lines(line-1);    
45     print_spaces(col-1);
46     printf("%s",text);  //在第line行,第col列输出text中的字符串 
47 }

2.test2

 1 #include<stdio.h>
 2 long long fac(int n);
 3 
 4 int main(){
 5     int i,n;
 6     
 7     printf("Enter n: ");
 8     scanf("%d",&n);
 9     
10     for(i=1;i<=n;++i){
11         printf("%d!=%11d\n",i,fac(i));
12     }
13     
14     return 0;
15 }
16 
17 long long fac(int n){
18     static long long p=1;
19     
20     p=p*n;
21     return p;
22 }

3.test3

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

4.test4

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int func(int n,int m);
 4 
 5 
 6 int main()
 7 {
 8     int n,m;
 9     
10     while(scanf("%d%d",&n,&m)!=EOF)
11     printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m));
12     
13     return 0;
14 }
15 //迭代 
16 int func(int n,int m){
17     int up=1,down=1;
18     int q;
19     int i=1,j=1;
20     if(n<m){
21         q=0;
22     }
23     else if(n>=m){
24     
25     for(i=n-m+1;i<=n;++i){
26         up*=i;
27     }
28     for(j=1;j<=m;++j){
29         down*=j;
30     }
31 }
32     q=up/down;
33     return q;
34 }
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int func(int n,int m);
 4 
 5 int main()
 6 {
 7     int n,m;
 8     
 9     while(scanf("%d%d",&n,&m)!=EOF)
10     printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m));
11     
12     return 0;
13 }
14 //递归
15 int func(int n,int m){
16     int f;
17     if(n<m) {
18         f=0;
19     }
20     
21     else if(m==1) {
22         f=n;
23     }
24      
25     else if(m==0) {
26         f=1;
27     }
28     
29     else{
30     f=func(n-1,m)+func(n-1,m-1);
31 
32     }
33     return f;
34 
35     
36         
37 } 

 5.test5

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

 6.test6

 1 #include<stdio.h>
 2 #include<math.h>
 3 long func(long s);
 4 
 5 int main(){
 6 
 7     long s, t;
 8 
 9     printf("Enter a number:");
10     while(scanf("%ld",&s)!=EOF){
11         t = func(s);
12         printf("new numberm is:%ld\n\n",t);
13         printf("Enter a number:");
14 
15 
16     }
17 
18     return 0;
19 }
20 long func(long s){
21     int n,m;
22     long long k=0,d=0;
23 
24     while(s>0){
25         m=s%10;
26         if(m%2==0)
27             printf(" ");
28         else
29             k=k*10+m;
30         s=s/10;
31 
32     }
33     while(k>0){
34         n=k%10;
35         d=d*10+n;
36         k=k/10;
37 
38 
39 
40     }
41     return d;
42 
43 }

 

标签:10,int,long,char,实验,func,include
From: https://www.cnblogs.com/hhhyx/p/17808782.html

相关文章

  • 实验3
    任务1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);7voidprint_spaces(intn);8voidprint_blank_lines(intn);......
  • 实验3 C语言函数应用编程
    一、实验目的二、实验准备三、实验内容1.实验任务1源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(in......
  • 实验3
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明int......
  • 实验3 C语言函数应用编程
    实验任务1:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,col,......
  • 实验3 C语言函数应用编程
    1、实验1实验1运行结果  实现了每隔一秒随机弹出“hi,November~”。2、实验2实验2-1源代码1#include<stdio.h>2longlongfac(intn);34intmain()5{6inti,n;78printf("Entern:");9scanf("%d",&n);1011for......
  • 实验三
    task1.c源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_bl......
  • 实验3 类与数组、指针
    实验任务1point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmo......
  • 实验三-类与数组、指针
    point.hpp1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()const;1......
  • 实验2 类和对象_基础编程2
    实验任务1demo1.dev方法一#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;//类T的声明classT{public:T(intx=0,inty=0);//带有默认形值的构造函数T(constT&t);//复制构造函数T(T&&......
  • 实验3
    实验1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn);10......