南京信息工程大学实验3(耿子尧)


#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 = 0; i <= 10; i++) { line = rand() % 25; col = rand() % 80; printText(line, col, text); Sleep(1000); } return 0; } void printBlankLines(int n) { int i; for (i = 1; i <= n; i++) printf("\n"); } void printSpaces(int n) { int i; for (i = 1; i <= n; i++) printf(" "); } void printText(int line, int col, char text[]) { printBlankLines(line - 1); printSpaces(col - 1); printf("%s", text); }

随机生成数0-24和0-80的数赋值到line和col,是打印“hi may”时前打印col-1个空格,且每个“hi,may”间隔line-1行、且每生成一个”Hi may“后做1000ms的停留

2

#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! = %11d\n", i, fac(i));

    return 0;
}
long long fac(int n)
{
    static long long p = 1;

    p = p * n;

    return p;
}

添加“printf("p = %lld\n"p);”在17与19行间

static变量将保存上次运行的结果进入下一次运算

3

#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 a;
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    if (n != 0)
        return 2 * fun(n - 1) + 1;
}

4

#include<stdio.h>
void han (int n);
long long fun(int n);
int main()
{
    int n,p;
    char A='A',B='B',C='C';
    while (scanf("%d", &n) != EOF)
    {
        han(n,A,C,B);
        p = fun(n);
        printf("一共移动了%d次\n", p);
    }
    return 0;
}
void han(int n,char x,char y,char z)
{
    static int p=0;
    if (n == 1)
    {
        printf("第%d个盘子:%c-->%c\n", n, x, y);
    }
    if (n != 1)
    {
        han(n-1,x,z,y);
        printf("第%d个盘子:%c-->%c\n",n,x,y);
        han(n-1,z,y,x);
    }
}
long long fun(int n)
{
    long long a;
    if (n == 1)
        return 1;
    if (n != 0)
        return 2 * fun(n - 1) + 1;
}

5

#include<stdio.h>
int is_prime(int x);
int main()
{
    int j = 1,p,q,k;
    for (j = 0; j <= 20; j=j+2)
    {
        for (p = 1;p < j; p++)
        {
            k = is_prime(p);
            if (k == 1)
            {
                q = j - p;
                k = is_prime(q);
                if (k == 1)
                {
                    printf("%d=%d+%d\n", j, p, q);
                    break;
                }
            }
            
        }
    }
    return 0;
}
int is_prime(int x)
{
    int i;
    for (i = 2; i < x; i++)
        if (x % i == 0)break;
    if (i == x)
        return 1;
    else
        return 0;
}

6

#include<stdio.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: %1d\n\n", t);
        printf("enter a number:");
    }
    return 0;
}
long fun(long s)
{
    int m=1,k,s0=0;
    while (s != 0)
    {
        k = s % 10;
        if (k % 2 == 1)
        {
            s0 = s0 + k * m;
            m = 10 * m;
        }
        s = s / 10;
    }
    return s0;
}

 

posted @ 2022-04-24 20:37  耿子尧  阅读(20)  评论(1编辑  收藏  举报