实验三

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

 

#include<stdio.h>

void printSymbol(int n, char symbol);

int main()
{
    int n;
    char symbol;
    
    while(scanf("%d %c", &n, &symbol)!=EOF)
    {
        printSymbol(n, symbol);
        printf("\n");
    }
    
    return 0;
}

void printSymbol(int n,char symbol)
{
    int i;
    for(i = 1;i <= n;++i)
        printf("%c",symbol);    
        
}

 

#include<stdio.h>

void printSymbol(int n, char symbol);

int main()
{
    int n;
    char symbol;
    
    while(scanf("%d %c", &n, &symbol)!=EOF)
    {
        printSymbol(n, symbol);
        printf("\n");
    }
    
    return 0;
}

void printSymbol(int n, char symbol)
{
    char ans;
    if(n == 1)
    printf("%c", symbol);
    else
    {
    printf("%c", symbol);
    return printSymbol(n-1,symbol);
    }
}

 

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

 

#include<stdio.h>
#include<math.h>
int isPrime(int n);

int main()
{
    int i, sum;
    sum = 79;
    for(i = 101;i <= 200;++i)
    {
        if(isPrime(i))
            printf("");
        else
            printf("%4d",i);
    }
    printf("\n");
    printf("101~200之间一共有%d个非素数",sum);
    return 0;
}

int isPrime(int n)
{
    int k;
    for(k = 2;k <= sqrt(n);k++)
        if(n%k==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("%ld", &s)!=EOF)
    {
        t = fun(s);
        printf("new number is:%ld\n\n", t);
        printf("Enter anumber:");
    }
    
    return 0;
}

long fun(long a)
{
    long s = 0;
    int b[100] = {0};
    int i = 0, c = 0,n = 0;
    
    while(a != 0)
    {
        b[i] = a % 10;
        a = a / 10;
        i++;
        n++;
    }
    for(i = 0;i <= n - 1;i++)
    {
        if(b[i] % 2 != 0)
        {
            s = s + b[i] * pow(10,c);
            c++;
        }
    }
    
    return s;
}

 

#include<stdio.h>
double fun(int n);
int fac(int n);

int main()
{
    int n;
    double s;
    
    printf("Enter n(1~10):");
    while(scanf("%d", &n)!=EOF)
    {
        s = fun(n);
        printf("n = %d,s = %f\n\n",n ,s);
        printf("Enter n(1~10):");    
    }
    
    return 0;
}

double fun(int n)//计算目标函数 
{
    double ans = 0;
    
    for(int i = 1;i <= n;i++)
    {
        ans = ans + 1/(double)fac(i); 
    }
    return ans;
}

int fac(int n)//计算阶乘 
{
    if (n == 1)
        return 1;
    else
        return -1 * n * fac(n - 1);
        
}

 

 

posted @ 2021-11-25 20:02  凌空haki  阅读(4)  评论(0编辑  收藏  举报