Java教程

实验3 函数

本文主要是介绍实验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()//随机在0~24中某行、0~79中某列开始输出字符串,共输出十次,每输出一次后停顿一秒。
{
    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);
}
#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;
}

#include<stdio.h>
int func(int, int);

int main()//理论认为p1=8,p2=17,与运行结果一致。
{
    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变量的特性:下一次的运算如果用到它,仍然代入上一次运行结束后它自己的值。

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

#include<stdio.h>
void hanoi(unsigned n, char from, char temp, char to);
void moveplate(unsigned n, char from, char to);
int m;
int main()
{
    unsigned n;
    
    while (scanf_s("%u", &n) != EOF)
    {
        m = 0;
        hanoi(n, 'A', 'B', 'C');
        printf("一共移动了%d次.\n", m);
    }
    return 0;
}

void hanoi(unsigned 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 n, char from, char to)
{
    extern int m;
    printf("第%u个盘子: %c --> %c\n", n, from, to);
    m++;
}

#include<stdio.h>
int is_prime(int n);
int is_prime(int n)
{
    int i;
    if (n == 0 || n == 1)
        return 0;
    else if (n == 2)
        return 1;
    else if (n > 2)
    {
        for (i = 2; i < n; i++)
        {
            if (n % i == 0)
                return 0;
            if (i == n - 1)
                return 1;
        }
    }
    
}

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

#include<stdio.h>
#define N 80
long fun(long s);

int main()
{
    long s, t;
    printf("Enter a number: ");
    while (scanf_s("%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 a[N], i, j = 0, k, t, m, b[N], n = 0, p;
    long q = 0;
    for (i = 0; s != 0; i++)
    {
        a[i] = s % 10;
        s = s / 10;
        j++;
    }
    for (k = 0; k < j/2; k++)
    {
        t = a[k];
        a[k] = a[j - 1 - k];
        a[j - 1 - k] = t;
    }
    for (m = 0; m < j; m++)
    {
        if (a[m] % 2 != 0)
            b[n++] = a[m];
    }
    for (p = 0; p < n; p++)
    {
        q = q * 10 + b[p];
    }
    return q;
}

 

这篇关于实验3 函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!