严浩翔圈外女友

导航

实验三

task1

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

 static变量特性:将保存上次结果

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)
{
    
    int i = 0;
    
    for( i;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{
        printSymbol(n-1, symbol);
        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)
 {
     
     if(n==0)
     return 0;
     
     else if (n==1)
     return 1;
     
     else{
     long long result;    
     
    result = 2 *fun(n-1) + 1;
    return result;    
         
    }
     
 }

 task4

#include <stdio.h>
#include <math.h>

int isPrime(int n);

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

 

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

 

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 m = -1,t = 1,i;
    double s=1;
    
    if(n==1)
    return 1;
    
    else
    {
        for(i=2;i<=n;i++)
        {
            t*=i;
            s+=(float)m/t;
            m=-m; 
        }
        return s;
    }
    
 } 

 

 

posted on 2021-11-29 21:24  严浩翔圈外女友  阅读(10)  评论(1编辑  收藏  举报