实验三

任务一
#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); // 暂停1000ms
}
return 0;
}
// 打印n个空格
void printSpaces(int n)
{
int i;
for(i=1; i<=n; ++i)
printf(" ");
}
// 打印n行空白行
void printBlankLines(int n)
{
int i;
for(i=1; i<=n; ++i)
printf("\n");
}
// 在第line行第col列打印一段文本
void printText(int line, int col, char text[])
{
printBlankLines(line-1); // 打印n-1行空行
printSpaces(col-1); // 打印n-1列空格
printf("%s", text);
}
 从一个到一行,再随机出现,利用sleep函数设置时间 

任务二

// 练习:局部static变量特性
#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;
}
不一致 

任务三

#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)
{
    int i;
    long long x=0,p=1;
    if(n==0) x=1;
    else 
    {
        for(i=1;i<=n;i++)
        {
            p=p*2;
        }
        x=p-1;
    }
    return x;
}

任务四

#include <stdio.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;
    extern int x;
    while(scanf("%u",&n)!=EOF) /*盘子个数*/ 
        {
        hanoi(n,'A','B','C');
        printf("次数为%d\n",x);
        printf("\n");
        x=0;
        }
    
    return 0;
} 
int x=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);/*n-1个盘子从A以C为中转移到B*/ 
        moveplate(n,from,to);/*将盘子n从A移到C*/ 
        hanoi(n-1,temp,from,to);/*将n-1个盘子从B以A为中转移到C*/ 
        
    }
}
void moveplate(unsigned int n,char from,char to)
{
    printf("%u:%c-->%c\n",n,from,to);
    x++;
}

任务五

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

任务六

#include<stdio.h>
#include<stdio.h>
#include<math.h>

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

 

posted @ 2022-04-23 18:20  &H  阅读(26)  评论(3)    收藏  举报