实验三

Task 1

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define N 80

void printText(int line, int col, char text[]);
void printSpaces(int n);
void printBlanklines(int n);

int main()
{
    int line, col, i;
    char text[N] = "hi,May~";

    srand(time(0));

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

    return 0;
}
void printSpaces(int n)
{
    int i;
    for (i = 1; i <= n; i++)
        printf(" ");
}
void printBlanklines(int n)
{
    int i;
    for (i = 1; i <= n; i++)
        printf("\n");
}
void printText(int line, int col, char text[])
{
    printBlanklines(line - 1);
    printSpaces(col - 1);
    printf("%s", text);
}

功能:每隔一秒在随机的行和列打印一个“hi,May~"

Task 2.1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.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;
    printf("p=%lld\n", p);
    p = p * n;

    return p;
}

 总结:"static long long p = 1"只起效一次,之后函数调用时采用上一次运算所得值运算。

Task 2.2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.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);

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

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

    return m;
}

理论计算p1=8,p2=17。运行结果符合计算。

Task 3

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
long long fun(int n);

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

    while (scanf("%d", &n) != EOF)
    {
        f = fun(n);
        printf("n =%d, f=%lld\n", n, f);

    }
    return 0;
}
long long fun(int n)
{
    long long k;
    if (n == 0)
    {
        return 0;
    }
    else
    {
        k = 2 * fun(n - 1) + 1;
    }
    return k;

}

Task 4

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
void hanoi(unsigned int n, char from, char temp, char to);
void moveplate(unsigned int n, char from, char to);
int main()
{
    unsigned int n;
    long long t;
    while (scanf("%u", &n) != EOF)
    {
        hanoi(n, 'A', 'B', 'C');
        t = pow(2, n) - 1;
        printf("\n一共移动了%d次.\n\n", t);
    }
    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)
{
    printf("第%u个盘子:%c-->%c\n", n, from, to);
}

Task 5

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int is_prime(int n);
int main()
{
    int i, j;
    for (i = 4; i <= 20; i += 2)
    {
        for (j = 2; j <= i / 2; j++)
        {
            if (is_prime(j) == 1 && is_prime(i - j) == 1)
            {
                printf("%d=%d+%d\n", i, j, (i - j));
            }
        }
    }

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

}

Task 6

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
long fun(long s);
int main()
{

    long s, t;
    printf("Enter a number:");
    while (scanf("%ld", &s) != EOF)
    {
        t = fun(s);
        printf("new number is: %ld\n\n", t);
        printf("Enter a number:");
    }
    return 0;
}
long fun(long s)
{
    int i, k, ans = 0;
    for (i = 0; s != 0; s = s / 10) {
        k = s % 10;
        if (k % 2 != 0)
        {
            ans = ans + k * pow(10, i);
            i++;
        }
    }
    return ans;
}

 

posted @ 2022-04-25 21:19  Zxc123jkl  阅读(10)  评论(1编辑  收藏  举报