首页 > 其他分享 >实验3

实验3

时间:2023-04-01 11:57:19浏览次数:31  
标签:return int long 实验 func ans include

实验任务1

代码:

#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,April~";

    srand(time(0));

    for (i = 1; i <= 10; ++i) {
        line = rand() % 25;
        col = rand() % 80;
        print_text(line, col, text);
        Sleep(1000);
    }

    return 0;
}

void print_spaces(int n) {
    int i;

    for (i = 1; i <= n; ++i)
        printf(" ");
}

void print_blank_lines(int n) {
    int i;
    for (i = 1; i <= n; ++i)
        printf("\n");
}

void print_text(int line, int col, char text[]) {
    print_blank_lines(line - 1);
    print_spaces(col - 1);
    printf("%s", text);
}

截图:

 

 

结论:

不断循环:随机生成0~25的数字作为line,随机生成0~80的数字作为col,打印line-1行空行,打印col-1列空格后,在第line行,第col列输出hi,April~的字符串。即随机分布hi,April~

实验任务2

代码:

#include<stdio.h>
long long fac(int n);

int main() {
    int i, n;

    printf("Enter n:");
    scanf_s("%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;

    return p;
}
#include<stdio.h>
long long fac(int n);

int main() {
    int i, n;

    printf("Enter n:");
    scanf_s("%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;
    printf("p=%lld\n", p);
    p = p * n;

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

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

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

    i += m + 1;
    m = i + a + b;

    return m;
}

 

截图:

 

 

 

 

 

 结论:

局部static变量的特性是:定义局部变量并将其初始化,所以在程序运行时静态局部变量就存在且执行完毕,程序调用函数并执行的时候static的这条定义并不起作用,所以只在第一次调用时变量取初始化的值,之后的调用函数并执行的过程中使用的变量的值都是由前一次执行函数所得的值继承而来。也就是上面程序第一次执行的结果是8而第二次执行的结果是17。

实验任务3

代码:

#include<stdio.h>
long long func(int n);

int main() {
    int n;
    long long f;

    while (scanf_s("%d", &n) != EOF) {
        f = func(n);
        printf("n=%d,f=%lld\n", n, f);
    }

    return 0;
}

long long func(int n) {
    long long f=0;
    long long m=1;
    for (int i = 1; i <= n; i++) {
        m *= 2;
        f =m-1;
    }
    
    return f;
}

 

截图:

 实验任务4

代码:

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

int main() {
    int n, m;

    while (scanf_s("%d%d", &n, &m) != EOF)
        printf("n=%d,m=%d,ans=%d\n", n, m, func(n, m));

    return 0;
}

int func(int x,int y) {
    
    double ans;
    if (y == 0 || y == x)
        ans = 1;
    else if (y == 1)
        ans = x;
    else if (y > x)
        ans = 0;
    else
        ans = (func(x - 1, y) + func(x - 1, y - 1));
    return ans;
}

 

截图:

 

 

实验任务5

代码:

#include<stdio.h>
double mypow(int x, int y);

int main() {
    int x, y;
    double ans;

    while (scanf_s("%d%d", &x, &y) != EOF) {
        ans = mypow(x, y);
        printf("%d的%d次方:%g\n\n", x, y, ans);
    }
    return 0;
}

double mypow(int x, int y) {
    double ans=1;
    if (y >= 0) {
        for (int i = 1; i <= y; i++)
            ans *= x;
    }
    else {
        for (int j = 1; j <= -y; j++)
            ans = ans / x;
    }
    return ans;
}
#include<stdio.h>
double mypow(int x, int y);

int main() {
    int x, y;
    double ans;

    while (scanf_s("%d%d", &x, &y) != EOF) {
        ans = mypow(x, y);
        printf("%d的%d次方:%g\n\n", x, y, ans);
    }
    return 0;
}

double mypow(int x, int y) {
    double ans = 1,t=1;
    if (y == 0)
        ans = 1;
    else {
        if (y > 0) {
            for (int i = 1; i <= y - 1; i++) {
                t *= x;
            }
            ans = t * x;
        }
        else {
            for (int j = 1; j <= -y; j++) {
                t *= x;
            }
            ans = 1 / t;
        }
    }
    return ans;
}

 

截图:

 

 

 

 

 

 

实验任务6

代码:

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

int main() {
    unsigned int n;
    
    while (scanf_s("%u", &n) != EOF) {
        tot = 0;

        hanoi(n, 'A', 'B', 'C');
        
        printf("一共移动了%d次\n", tot);
    }

    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) {
    tot++;
    printf("%u:%c-->%c\n", n, from, to);
}

 

截图:

 

 

实验任务7

代码:

#include<stdio.h>
int is_prime(int n);
int main() {
    int i=4;
    while( i<=20){
        int x, y;
        if (i == 4)
            printf("4=2+2\n");
        else{
            for (x = 3; x <= i; x += 2)
        {
            y = i - x;
            if (is_prime(x) && is_prime(y))
            {
                printf("%d=%d+%d\n", i,x,y);
                break;
            }
        }
        }
        
        i += 2;
    }
    return 0;
}

int is_prime(int n) {
    int i;
    if (n <= 2)
        return 0;
    else
    {
        for (i = 2; i <= n - 1; i++)
        {
            if (n % i == 0)
                break;
        }
        if (i >= n)
            return 1;
        else
            return 0;

    }
}

 

截图:

 

 

实验任务8

代码:

#include<stdio.h>
#include<math.h>
long func(long s);

int main() {

    long s, t;
    
    printf("Enter a number:");
    while (scanf_s("%ld", &s) != EOF) {
        t = func(s);
        printf("new number is :%ld\n\n", t);
        printf("Enter a number:");
    }

    return 0;
}

long func(long s) {
    long t = 0;
    int x, sum = 0;
    while (s) {
        x = s % 10;
        if (x % 2 == 1) {
            sum += 1;
            t += x * pow(10, sum-1);
        }
        s /= 10;
    }
    return t;
}

 

截图:

 

标签:return,int,long,实验,func,ans,include
From: https://www.cnblogs.com/gueal/p/17273756.html

相关文章

  • 【实验】Bilibili各档清晰度
    想试试B站的视频清晰度是怎么分的,1080P的高码率和普通1080P有没有什么区别,以下是测试情况:测试视频:https://www.bilibili.com/bangumi/play/ss25672?from_spmid=666.22.0.0分档:1080P-高码率1080P720P480P360P自动注:视频均处于2倍速播放状态......
  • 202031607213-李蕊 实验一 软件工程准备--构建之法与博客首秀
    实验一软件工程准备一、实验介绍项目内容班级博客链接https://edu.cnblogs.com/campus/xbsf/2020CSSE本次作业要求链接https://edu.cnblogs.com/campus/xbsf/2020CSSE/homework/12938我的课程学习目标(1)学习博客园软件开发者学习社区使用技巧和经验。(2)了解Gi......
  • 202031607211-米乐 实验一 软件工程准备—初步认识软件工程
    实验一软件工程准备项目内容班级博客链接2023年春软件工程(2020级计算机科学与技术)本次作业要求链接实验一软件工程准备我的课程学习目标学会使用博客园进行学习、了解Github的基本操作、阅读《现代软件工程——构建之法》本次作业帮我实现的学习目标能够......
  • 实验3
    Task1<实验结论>#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn)......
  • 实验3
    实验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,......
  • 202031607330-杨雯丽 实验一 软件工程准备—认识软件工程
    实验一:软件工程准备项目内容班级课程博客链接20级卓越班此次作业链接实验一软件工程准备我的课程学习目标(1)学习博客园软件开发者学习社区使用技巧和经验。(2)了解Github的基本操作。本次作业在哪些方面帮我实现学习目标(1)了解与掌握了博客园的基本使用(2)熟练了......
  • 202031607334-贾小萌 实验一 软件工程准备 初步认识软件工程
    项目内容班级博客链接20级卓越班本次作业要求链接实验一软件工程准备我的课程学习目标学习博客园软件开发者学习社区使用技巧和经验;了解Github基本操作本次作业在哪方面帮我实现学习目标初步了解博客园软件和Github的基本操作;初步认识软件工程实验内容......
  • 202031705119-张倩 实验一 软件工程准备——初步认识软件工程
    一.博文开头项目内容班级博客链接2023春软件工程(2020级计算机科学与技术)本次作业要求链接实验一软件工程准备我的课程学习目标1.学会使用博客园的基本功能2.学会使用Github的基本功能3.阅读《现代软件工程——构建之法》并解决提出的问题本次作业在哪些......
  • 实验二
    task11x="nbaFIFA"2print(x.upper())3print(x.lower())4print(x.swapcase())5print()6x="abc"7print(x.center(10,'*'))8print(x.ljust(10,'*'))9print(x.rjust(10,'*'))10x="123&quo......
  • 实验三
    实验任务一程序源码:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmai......