task1
#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~”字符

task2-1

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

 

 task2-2

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

 

 局部static变量的特性:静态局部变量在程序执行到该对象的声明处时被首次初始化,以后的函数调用不再进行初始化

task3

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

 

 task4

#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,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);
}

 

 task5

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int is_prime(int n);
int main()
{
    int n,p,q,flagp,flagq;              
    for(n=4;n<=20;n+=2)
    {
        p = 1;
        do
        {
            p++;
            q=n-p;
            flagp=is_prime(p);
            flagq=is_prime(q);
        } 
        while(flagq*flagp == 0);
        printf("%d =%d +%d\n", n, p, q);
    }
    return 0;
}
int is_prime(int n)
{
    int i;
    for(i=2;i<n;i++)
        if(n%i==0)
            break;
    if(i<n)
        return 0;
    else
        return 1;
}

 

 task6

#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: %ld\n\n", t); 
        printf("Enter a number: "); 
    }
    return 0; 
}
long fun(long s)
{
    int d;
    long t=0;
    long s1=1;
    while(s>0)
    { 
        d=s%10;
        if(d%2!=0)
        {
            t=d*s1+t;
            s1*=10;
        }
        else
        {
            t=t;
        }
        s/=10;
    }
    return t;
}

 

posted on 2022-04-20 17:40  wangyfan  阅读(11)  评论(3编辑  收藏  举报