実験6制御構造総合プログラム設計

4327 ワード

6-1               (20  )
int reverse( int number ){
    int num=0;
    while(number!=0){
        num=num*10+number%10;
        number/=10;
    }
    return num;
}
6-2             (20  )
int prime( int p ){
    if(p==1){
        return 0;
    }
    for(int i=2;i

6-3               (20  )

//     
int factorsum( int number ) {
    int sum=0;
    for(int i=1; i<=number/2+1; i++) {
        if(number%i==0) {
            sum+=i;
        }
    }
    return sum;
}

void PrintPN( int m, int n ) {
    int flag=0;//        ,flag=0 
    for(int i=m; i<=n; i++) {
        if(i==factorsum(i))//         ,     
        {
            flag=1; //    ,flag=1 
            printf("%d = ",i);
            for(int j=1; j<=i/2+1; j++) {
                if(i%j==0) {
                    //       
                    if(j==1){
                        printf("%d",j); 
                    } else{
                        printf(" + %d",j);
                    }           
                }
            }
            //        ,   
            printf("
"); } } // flag=0, , no...... if(!flag){ printf("No perfect number"); } }
6-4             Fibonacci (20  )
int fib(int n) {
    if (n == 1 || n == 2) {
        return 1;
    }
    if (n > 2) {
        return fib(n - 1) + fib(n - 2);
    }
    //           ,   ,         
}
void PrintFN(int m, int n) {
    int i = 1, count = 0;
    while(fib(i)<=n){
        if (fib(i) >= m && fib(i) <= n) {
            count++;
            if(count==1){
                printf("%d", fib(i));
            }else{
                printf(" %d", fib(i));
            }
            
        }
        i++;
    }
    if (count == 0) {
        printf("No Fibonacci number");

    }
}
7-1    PI(15  )
#include 
#include 

 
double fun1(int num){
    double sum=1;
    for(int i=1;i<=num;i++){
        sum*=i;
    }
    return sum;
    //     
}
double fun2(int num){
    double sum=1;
    for(int i=1;i<=num;i++){
        sum*=2*i+1;
    }
    return sum;
    //     
}
int main()
{   double eps;
    scanf("%le", &eps);
    double PI=1,item=1,i=1;
    while(fabs(item)>eps){
        item=fun1(i)/fun2(i);
        PI+=item;
        i++;
    }
    printf("PI = %.5f",2*(PI));
    return 0;
}

7-2            (15  )
#include 

int main()
{   char ch,oldch=' ';
    int count=0;
    while((ch=getchar())!='
'){ if(ch!=' '&&oldch==' '){ count++; } oldch=ch; } printf("%d",count); return 0; }
/*
 ============================================================================
 Name        :      .c
 Author      :  lzh
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include 
#include 

int main(void) {
     int a, b, sum;
        char opt;
        scanf("%d", &a);
        sum = a;
        scanf("%c", &opt);
        int iErr = 1;

        while (opt != '=') {
            scanf("%d", &b);
            if (opt == '+')
                sum += b;
            else if (opt == '-')
                sum -= b;
            else if (opt == '*')
                sum *= b;
            else if (opt == '/') {
                if (b == 0) {
                    iErr = 0;
                    break;
                } else {
                    sum /= b;
                }
            } else {
                iErr = 0;
                break;
            }
            scanf("%c", &opt);
        }

        if (!iErr)
            printf("ERROR
"); else printf("%d
", sum); return EXIT_SUCCESS; }