clm888

导航

实验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 = 1; i <= 10; ++i)
    {
        line = rand() % 25;
        col = rand() % 80;
        printText(line, col, text);
        Sleep(1000);
    }
    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);
}

程序功能:每间隔1秒在屏幕上打出hi,May~并且每次出现中间随机间隔0~24行,0~79列

#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;
    printf("p = %lld\n", p);
    p = p * n;
    return p;
}

 

 

#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变量的特性:函数退出时,变量始终存在,但不能被其他函数使用,当再次进入该函数时,将保存上次的结果。

#include <stdio.h>
long long fun(int n); 
int main()
{
  int n;
  long long f;
  while (scanf_s("%d", &n) != 0)
 {
    f = fun(n); 
    printf("n = %d, f = %lld\n", n, f);
 }
  return 0;
}
long long fun(int n)
{
    int s;
    if (n > 1)
        s = 2 * fun(n - 1) +1;
    else if (n == 1)
        s = 1;
    return s;
}

 

 

#include<stdio.h>
void hanoi(unsigned int n, char from, char temp, char to);
void moveplate(unsigned int n, char from, char to);
int count(int n);
int main()
{
    unsigned int n;
    while (scanf_s("%u", &n) != 0)
    {
        hanoi(n, 'A', 'B', 'C');
        printf("一共移动了%d次\n\n", count(n));
    }
    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);
}
int count(int n)
{
    int i = 1,s;
    if (n == 1)
        s = 1;
    else
        s = 2 * count(n - 1) + 1;
    return s;
}

 

 

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

int is_prime(int a);

int main()
{
    int n, i;
    for (n = 4; n <= 20; n += 2)
    {
        for (i = 2; i <= n ; i++)
        {
            if (is_prime(i) && is_prime(n - i))
            {
                printf("%d=%d+%d\n", n, i, n - i);
                break;
            }
        }
    }

    return 0;
}

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

    return 1;
}

 

 

#include <stdio.h>
#include<math.h>
long fun(long s);
int main()
{
    long s, t;
    printf("Enter a number: ");
    while (scanf_s("%ld", &s) != 0)
    {
        t = fun(s); 
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
    return 0;
}
long fun(long s)
{
    long i, j=0,k=0;
    while (s / 10 != 0||s==1)
    {
        i = s % 10;
        if (i % 2 != 0)
        {
            j = i * pow(10,k) + j;
            k++;
        }
        s /= 10;
    }
    return j;
}

 

posted on 2022-04-19 16:33  clmmmmm  阅读(13)  评论(0编辑  收藏  举报