实验3 函数

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

 

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

 

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

 Task 2

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

 Task 3

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

 Task 4

#include<stdio.h>
#include<math.h>
int isPrime(int n);
int main()
{
    int i,k=0;
    for(i=101;i<=200;i++)
    {
        if(isPrime(i))
        {
            printf("%4d",i);
            k++;
        }   
    }
    printf("\n");
    printf("101~200之间一共有 %d个非素数",k);
    return 0;
} 
int isPrime(int n)
{
    int j;
    for(j=2;j<=sqrt(n);j++)
        if(n%j==0)
            return 1;
        return 0;
}

 Task 5

#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 a,b=0,c=1;
    while(s>0){
        a=s%10;
        if(a%2!=0)
        {   
            a*=c;
            b+=a;
            c*=10;
        }
        s/=10;    
    }
    return b;    
}

 Task 6

#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 j,D=1,sign=1;
        double sum=0,team;
            for(j=1;j<=n;j++)   
               {
                D=D*j;
                team=1.0/D;    
                sum+=sign*team;
                sign=-sign;
               }
    return sum;
}

 

posted @ 2021-11-24 16:50  谢能骏  阅读(15)  评论(1编辑  收藏  举报