实验三 函数

task 1-1

#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!=%11d\n",i,fac(i));
         
    return 0; 
}

long long fac(int n)
{
    static long long p=1;
    printf("p=%11d\n",p);
    p=p*n;
    return p;
}

 

 

task1-2

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

 task2

#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)
{
    if(n==1)
    printf("%c",symbol);
    else
    {
        printSymbol(n-1,symbol);
        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)
{
    int i;
    
    for(i=0;i<n;i++)
    printf("%c",symbol);
}

 

task3

 

#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 f = 1,s;
     for(i = 1;i<=n;++i)
     {
         f = f*2;
     }
     s = f - 1;
     return s;
 }

 task4

#include <stdio.h>
int isprime(int n);
int main()
{int a,i;
    for(i=101;i<201;i++){
        if(!isprime(i)){
        printf("%d ",i);
        a++;
        while(a%10==0&&a!=0){
        printf("\n");break; }}}
    printf("\n101~200之间一共有%d个非素数",a);
    return 0;
    }
int isprime(int n){
int i;
for (int i = 2; i < (n + 4) / 2; i++)
{if (n == 2)
return 1;
else if (n % i == 0){
return 0;
break;
}
if (i == (n + 4) / 2 - 1 && n % i != 0){
return 1;
}}}

 task5

 

#include<stdio.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 a number:");
    }
    
    return 0;
}


long fun(long s)
{
    int t,m=1,i;
    long p=0;
    while(s!=0)
    {
        t=s%10;
        s=s/10;
        if(t%2==1)
        {
            for(i=1;i<m;i++)
                t=t*10;
            p=t+p;
            m++;
        }
    }
    return p;
}

 task6

#include<stdio.h>
double fun(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)
{
     int i,k=1,j=1;
    double result=0;
    double a;
    for(i=1;i<=n;i++)
    {
        
        k=k*i;
        a=1.0/k;
        result=result+j*a;
        j*=-1;
    }
    return result;
}

 

posted @ 2021-11-29 23:20  猫街少女琳  阅读(5)  评论(1编辑  收藏  举报