实验三 函数

发布时间 2023-04-01 21:34:33作者: zhangchuyu
实验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); // 暂停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,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;
}

实验2不知道为啥运行总是报错如上,但是代码应该没有问题,输入5之后按理应该得到5的阶乘即120。

加入一行printf("p = %lld\n", p)后应该分别打印出1,1,2,6,24

实验2.2:因为static作用于局部变量时可以改变生命周期,使得第二次执行fac的时候不进行m = 0, i = 2的赋值,而是延续上一步计算出的m=8,所以理论上应该输出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){
    int i ;
    double f = 0.0;
    for (i = 0; i < n; i++)
        f = ((f + 1) * 2)-1;
    return f;
}

实验4:

#include <stdio.h>
int func(int n, int m);
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) {
    int i = 1;
    double f = 1.0;
    for (i = 1; i <= y; i++)
        f *= (x - i + 1) / i;
    return f;
}

实验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)
{
    int i;
    double m=1.0;
    if (y>= 0)
        for (i = 1; i <= y; i++)
            m = m * x;
    else
        for (i = 0; i > y; i--)
            m = m / x;
    return m;
    
}

实验6:

#include <stdio.h>
int a = 0;
void hanoi(unsigned int n, char from, char temp, char to);
void move(unsigned int n, char from, char to);
int main() {
    
    unsigned int n;
    while (scanf_s("%u", &n) != EOF) {

        hanoi(n, 'A', 'B', 'C');
        
        printf("共需要%d步", a);
    }
        return 0;
    
    

}
void hanoi(unsigned int n, char from, char temp, char to) {
    if (n == 1)
        move(n, from, to);
    else
    {
        hanoi(n - 1, from, to, temp);
        move(n, from, to);
        hanoi(n-1, temp,from, to);

    }
}

void move(unsigned int n, char from, char to) {
    
    a++;
    printf("%u:%c-->%c\n", n, from, to);
}

 

#include <stdio.h> 
#include <math.h>
int is_prime(int n);
int main()
{
    int i, k, a[15], j = 0, l;
    for (k = 2; k <= 20; k++)
    {
        if (is_prime(k))
            a[j++] = k;
    }
    for (i = 2; i <= 10; i++)
    {
        for (l = 1; l <= 10; l++)
        {

            if (is_prime(2 * i - a[l - 1]))
            {
                printf("%d = %d + %d\n", (2 * i), a[l - 1], (2 * i - a[l - 1])); break;
            }

        }
    }
    return 0;
}
int is_prime(int n)
{
    int i, flag = 1;
    for (i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0) { flag = 0; break; }
    }
    return flag;
}

实验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 x) {
    int t, n = 0, i = 0;
    while (x != 0)
    {
        t = x % 10;
        if (t % 2 != 0) {
            n = n + pow(10, i) * t;
            i++;
        }
        x = x / 10;
    }
    return n;

}