实验四

试验一思考:不能将方程的根设计成返回值,因为返回值不止一个。

试验二:

#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;
    p = p * n;
    return p;
}

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

静态局部变量会保留上一次函数调用的值

试验三

#include <stdio.h>
#define N 1000
int fun(int n, int m, int bb[N]) {
    int i, j, k = 0, flag;

    for (j = n+1; j < m; j++) {
        flag = 1;
        for (i = 2; i < j; i++)
            if (j % 2 == 0) {
                flag = 0;
                break;
            }
        if (flag)
            bb[k++] = j;
    }
    return k;
}

int main() {
    int n = 0, m = 0, i, k, bb[N];

    scanf_s("%d", &n);
    scanf_s("%d", &m);

    for (i = 0; i < m - n; i++)
        bb[i] = 0;

    k = fun(n, m, bb);

    for (i = 0; i < k; i++)
        printf("%4d", bb[i]);

    return 0;
}

试验任务四

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

试验任务五

#include <stdio.h>

void draw(int n, char symbol);  // 函数声明 

#include <stdio.h> 
int main() {
    int n, symbol;

    while (scanf_s("%d %c", &n, &symbol) != EOF) {
        draw(n, symbol);  // 函数调用 

        printf("\n");
    }

    return 0;
}
void draw(int n, char symbol) {
    for (int i = 1; i <= n; i++) {
        for (int k = 0; k < n - i; k++) {
            printf(" ");
        }
        for (int j = 0; j < 2 * i - 1; j++) {
            printf("%c", symbol);
        }
        printf("\n");
    }

}

总结:

(坑):实验三中的j应该从n+1开始到m-1结束,不然输入的n和m若是素数也会输出,而题目让求的是n和m之间的素数 0.0

posted @ 2020-11-29 20:01  吔瓜群众  阅读(85)  评论(0编辑  收藏  举报