实验三

 1 #include<stdio.h>
 2 long long fac(int n);
 3 int main()
 4 {
 5     int i,n;
 6     printf("Enter n:");
 7     scanf("%d",&n);
 8     
 9     for(i=1;i<=n;++i)
10         printf("%d! = %lld\n",i,fac(i));
11         
12     return 0;
13     
14 }
15 
16 long long fac(int n)
17 {
18     static long long p = 1;
19     
20     p = p * n;
21     return p;
22 }

实验三

 

 

#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()
{
    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);
           
    }
       printf("\n");
       
       return ;
}

实验三

 

 

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

实验三

 

 

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

实验三

 

 

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

实验三

 

 

#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,a,b;
    double sum;
    a=1;
    b=1;
    
    for(i=1;i<=n;i++)
    {
        b = b*i;
        sum = sum+1.0/b*a;
        a = a*(-1);
    }
    
    return sum;
}

实验三

 

 

task1  局部static变量的特性:在编译时赋初值,以后每次调用函数时不再重新赋值,而保留上次函数调用结束时的值。

task2  本实验中递归方法更好,不需要定义新变量

实验总结

根据实际情况选择迭代或递归

定义新函数后要加分号,调用时去掉分号

运算符的优先级可能造成结果错误

上一篇:ES6——let const Symbol 解构赋值


下一篇:实验3