saowu's Blog

C语言程序集(31-45)

C语言程序集(31-45)
2020-04-03 · 9 min read
C 日记

31.计算1-2+3-4+5-...+99-100=?


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

int main() {
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum += pow(-1, i - 1) * i;
    }
    printf("sum=%d", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=-50
Process finished with exit code 0

32.计算1/1+1/2+1/3+...1/100=?


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

int main() {
    float sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum += pow(i,-1);
    }
    printf("sum=%f", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=5.187378
Process finished with exit code 0

33.计算1/1+1/2+1/3+...+1/n=?,n=10^6


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

int main() {
    float sum = 0;
    for (int i = 1; i <= 1e6; i++) {
        sum += pow(i, -1);
    }
    printf("sum=%f", sum);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=14.357358
Process finished with exit code 0

34.输入两个整数m和n,求其最大公约数和最小公倍数


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

int main() {
    int m, n, r, p;
    printf("input m,n: ");
    scanf("%d,%d", &m, &n);
    p = m * n;
    do {
        r = m % n;
        m = n;
        n = r;
    } while (r != 0);
    printf("最大公约数:%d\n", m);
    printf("最小公倍数:%d", p / m);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
input m,n: 6,8
最大公约数:2
最小公倍数:24
Process finished with exit code 0

35.计算 2+22+222+2222+22222=?(此时n=5)


#include <stdio.h>

int main() {
    int n, s = 0, t = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        t = t * 10 + 2;
        s += t;
    }
    printf("%d\n", s);
    return 0;
} 
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
5
24690

Process finished with exit code 0



36.计算 2/1+3/2+5/3+8/5+... 求前20项


#include <stdio.h>

int main() {
    float m = 2, n = 1, t, s = 0;
    for (int i = 1; i <= 20; i++) {
        s += m / n;
        t = m;
        m += n;
        n = t;
    }
    printf("sum=%f\n", s);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=32.660263

Process finished with exit code 0

37.计算 1!+2!+3!+...+20!=?


#include <stdio.h>

int main() {
    float s = 0, f = 1;
    for (int i = 1; i <= 20; i++) {
        f *= i;
        s += f;
    }
    printf("sum=%f\n", s);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=2561327455189073920.000000

Process finished with exit code 0

38.计算

 \sum_{k=1}^{100}{k}+\sum_{k=1}^{50}{k^2}+\sum_{k=1}^{10}\frac{1}{k}=?
#include <stdio.h>

int main() {
    int s1 = 0, s2 = 0;
    float s3 = 0;
    for (int i = 1; i <= 100; i++)
        s1 += i;
    for (int i = 1; i <= 50; i++)
        s2 += i * i;
    for (int i = 1; i <= 10; i++)
        s3 += 1.0 / i;
    printf("%f", s1 + s2 + s3);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
47977.929688
Process finished with exit code 0

39.水仙花数:一个3位数,各位数字的立方和等于它本身(如:153),打印全部水仙花数

1).
    #include <stdio.h>
    #include <math.h>
    
    int main() {
        int x, y, z, s;
        for (int m = 100; m < 1000; m++) {
            x = m / 100;
            y = m / 10 % 10;
            z = m % 10;
            s = pow(x, 3) + pow(y, 3) + pow(z, 3);
            if (m == s)
                printf("%d is a water flower number.\n", m);
        }
        return 0;
    }
2).
    #include <stdio.h>
    #include <math.h>
    
    int main() {
        int x, s;
        for (int m = 1; m <= 9; m++)
            for (int i = 0; i <= 9; i++)
                for (int j = 0; j <= 9; j++) {
                    x = m * 100 + i * 10 + j;
                    s = pow(m, 3) + pow(i, 3) + pow(j, 3);
                    if (x == s)
                        printf("%d is a water flower number.\n", x);
                }
        return 0;
    }
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
153 is a water flower number.
370 is a water flower number.
371 is a water flower number.
407 is a water flower number.

Process finished with exit code 0

40.如果一个数恰好等于它的因子之和,这个数为“完数”(例如:6=1+2+3),找出1000以内的完数,按照:“6 its factors are 1 2 3” 格式打印


#include <stdio.h>
#include <math.h>
int main() {
    int s;
    for (int i = 1; i <= 1000; i++) {
        s = 0;
        for (int j = 1; j <= i / 2; j++)
            if (i % j == 0)
                s += j;
        if (s == i) {
            printf("%d it's factors are ", i);
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0)
                    printf("%d ", j);
            }
            printf("\n");
        }
    }
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
6 its factors are 1 2 3 
28 its factors are 1 2 4 7 14 
496 its factors are 1 2 4 8 16 31 62 124 248 

Process finished with exit code 0


41.一个球从100m的高度落下,每次落地后反跳回原高度的一半,再落下,再反弹。求它第10次落地时,共经过多少米,第10次反弹多高



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

int main() {
    float sum = 100, high = 50;
    for (int i = 0; i < 10; i++) {
        sum += 2 * high;
        high /= 2;
    }
    printf("sum=%.2f,high=%f\n", sum, high*2);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
sum=299.90,high=0.024414

Process finished with exit code 0

42.(谭浩强c语言课本P141)猴子吃桃


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

int main() {
    int x1, x2 = 1;
    for (int day = 9; day >= 1; day--) {
        x1 = 2 * (x2 + 1);
        x2 = x1;
    }
    printf("num=%d\n", x1);
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
num=1534

Process finished with exit code 0

43.每个月兔子的总和依次为1,1,2,3,5,8,13...这就是斐波那契数列

{
    f1=1(n=1);
    f2=1(n=2);
    fn=f(n-1)+f(n-2)(n>=3);
}

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

int main() {
    int f1 = 1, f2 = 1, f;
    printf("%5d %5d", f1, f2);
    for (int i = 3; i <= 20; i++) {
        f = f1 + f2;
        printf("%5d ", f);
        if (i % 5 == 0)
            printf("\n");
        f1 = f2;
        f2 = f;
    }
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
    1     1    2     3     5 
    8    13    21    34    55 
   89   144   233   377   610 
  987  1597  2584  4181  6765 

Process finished with exit code 0


44.判断一个数是否为素数


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

int main() {
    int m, i;
    scanf("%d", &m);
    for (i = 2; i <= m / 2; i++)
        if (m % i == 0)
            break;
    if (m/2 == i-1)
        printf("Yes");
    else
        printf("No");
    return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
5
Yes
Process finished with exit code 0

45.成绩平均值


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

int main() {
    int sum = 0, s[10];
    float ave;
    for (int i = 0; i <= 9; i++) {
        scanf("%d", &s[i]);
        sum += s[i];
    }
    ave = sum / 10.0;
    printf("ave=%f\n", ave);
    for (int j = 0; j < 10; j++) {
        printf("%d", s[j]);
    }
    return 0;
}

输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
1
2
3
4
5
6
7
8
9
0
ave=4.500000
1234567890
Process finished with exit code 0


Copyright © 2020 - 2024 saowu. All Right Reserved
Powered by Gridea