首页 > 其他分享 >实验3

实验3

时间:2023-11-05 15:15:00浏览次数:37  
标签:return int long char 实验 func include


#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void print_text(int line, int col, char text[]); // 函数声明 void print_spaces(int n); // 函数声明 void print_blank_lines(int n); // 函数声明 int main() { int line, col, i; char text[N] = "hi, November~"; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); // 暂停1000ms } return 0; } // 打印n个空格 void print_spaces(int n) { int i; for(i = 1; i <= n; ++i) printf(" "); } // 打印n行空白行 void print_blank_lines(int n) { int i; for(i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列打印一段文本 void print_text(int line, int col, char text[]) { print_blank_lines(line-1); // 打印(line-1)行空行 print_spaces(col-1); // 打印(col-1)列空格 printf("%s", text); // 在第line行、col列输出text中字符串 }

 

 

在屏幕上打出一串字符串“hi,November”并在随机行,随机列生成,每次生成的时间相隔一秒。执行n次后自动弹出。

 

 

任务2

#include <stdio.h>
#include<stdlib.h>
long long fac(int n); // 函数声明

int main() {
    int i, n;


    printf("Enter n: ");
    scanf("%d", &n);

    for (i = 1; i <= n; ++i)
        printf("%d! = %lld\n", i, fac(i));

    return 0;
}

// 函数定义
long long fac(int n) {
    static long long p = 1;

    p = p * n;
 
    system("pause");
    return p;
}

 

 

 实验2_2

 理论: 8,17.

#include<stdio.h>
#include<stdlib.h>
int func(int, int);

int main()
{
    int k = 4, m = 1, p1, p2;

    p1 = func(k, m);
    p2 = func(k, m);
    printf("%d,%d\n", p1, p2);
    system("pause");
    return 0;
}

int func(int a, int b)
{
    static int m = 0, i = 2;

    i += m + 1;/*p1ʱ3  p2ʱ12*/
    m = i + a + b;/*p1ʱ8 p2ʱ17*/
    
    return m;
}

    

任务3

源代码

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

运行结果

任务4

源代码

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

运行结果

任务5

源代码

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

运行结果

 任务六

源代码

复制代码
 1 #include<stdio.h>
 2 #include<math.h>
 3 long func(long s);
 4 int main(){
 5     long s,t;
 6     printf("Enter a number:");
 7     while(scanf("%ld",&s)!=EOF){
 8         t=func(s);
 9         printf("new number is: %d\n\n",t);
10         printf("Enter a number: ");
11     }
12     
13     return 0;
14 }
15 long func(long s){
16     int x,t=0;
17     while(s!=0){
18         x=s%10;
19         s/=10;
20         if(x%2!=0)
21         t=t*10+x;
22     }
23     while(t!=0){
24         x=t%10;
25         t/=10;
26         s=s*10+x;
27     }
28     return s;
29 }
复制代码

运行结果

 

标签:return,int,long,char,实验,func,include
From: https://www.cnblogs.com/p2kp2k/p/17798727.html

相关文章

  • 实验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......
  • 实验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;voidmov......
  • 实验三 类与数组、指针
    实验任务1task1.cpp源码:Point.hpp:1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13......
  • 实验3
    任务1源代码1#include<stdio.h>2#include<time.h>3#include<windows.h>4#include<stdlib.h>5#defineN806voidprint_line(intn);7voidprint_col(intn);8voidprint_text(intline,intcol,chartext[]);9intmain(){10......