《C程序设计的抽象思维》1.9编程练习

本文地址:http://www.cnblogs.com/archimedes/p/programming-abstractions-in-c-1.html,转载请注明源地址。

1、温度转换:

#include<stdio.h>
int main()
{
    double C;
    while(~scanf("%lf", &C))
        printf("%lf\n", 9 * C * 1.0 / 5 + 32);
    return 0;
}   

2、长度转换:

#include<stdio.h>
int main()
{
    double YCI, YC, M;
    while(~scanf("%lf", &M)){
        printf("YCI:%lf\n", 1.0 / 0.0254);
        printf("YC:%lf\n", 1.0 / 12);
    }
    return 0;
}   

3、计算1+2+3+……+100

#include<stdio.h>
int main()
{
    int sum = 0;
    for(int i = 1; i <= 100; i++)
        sum += i;
    printf("%d\n", sum);
    return 0;
}   

4、计算序列值

#include<stdio.h>
int main()
{
    int N, i, sum;
    scanf("%d", &N);
    sum = 0;
    i = 1;
    while(i <= N)
        sum += 2 * (i++) - 1;
    printf("%d\n", sum);
    return 0;
}   

5、按照指定格式输入一个整数序列中的最大值

#include<stdio.h>
int main()
{
    int max, n, i;
    printf("This program finds the largest integer in a list.\n");
    printf("Enter 0 to sigal the end of the list.\n");
    printf("? ");
    max = -1;
    while(scanf("%d", &n) && n) {
        if(max < n) max = n;
        printf("? ");
    }
    printf("The largest value is %d\n", max);
    return 0;
}   

6、反转打印输入的整数

#include<stdio.h>
void print(int N)
{
    printf("The reverse number is ");
    while(N) {
        printf("%d", N % 10);
        N /= 10;
    }
    printf("\n");
}
int main()
{
    int N;
    printf("This program reverses the digits in an integer.\n");
    printf("Enter a positive integer: ");
    scanf("%d", &N);
    print(N);
    return 0;
}   

7、寻找完全数

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool IsPerfect(int n)
{
    int m, sum, t;
    sum = 1;
    m = (int)sqrt(n);
    for(int i = 2; i <= m; i++){ 
        if(n % i == 0) {
            if(i * i == n) {
                sum += m;
            } else {
                sum += (i + n / i);
            }
        }
    }
    if(sum == n)
        return true;
    else
        return false;
}
int main()
{
    for(int i = 2; i <= 9999; i++) {
        if(IsPerfect(i))
            printf("%d\n", i);
    }
    return 0;
}   

8、按照指定格式分解质因数

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define N 100000
int isprime[N];
bool prime(int n)
{
    int i;
    for(i = 2; i * i <= n; i++) {
        if(n % i == 0)
            return false;
    }
    return true;
}
void init()
{
    int i, k;
    k = 0;
    for(i = 2; i < 100000; i++) {
        if(prime(i))
            isprime[k++] = i;
    }
}
void solve()
{
    int i, n, flag, k, t;
    init();
    printf("Enter number to be factored: ");
    while(~scanf("%d", &n)) {
        k = flag = 0;
        if(prime(n)) {
            printf("%d\n", n);
        } else {
            t = n;
            while(isprime[k] < n) {
                while(t % isprime[k] == 0) {
                    if(!flag) {
                        flag = 1;
                        printf("%d", isprime[k]);
                    } else {
                        printf(" * %d", isprime[k]);
                    }
                    t /= isprime[k];
                }
                k++;
            }
            printf("\n");
        }
        printf("Enter number to be factored: ");
    }
}
int main()
{
    solve();
    return 0;
}   

9、浮点数按照规则转化为整数

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
void Round(float n)
{
    int m;
    m = floor(n);
    if(n > 0) {
        if(n - m < 0.5) {
            printf("%d\n", m);
        } else {
            printf("%d\n", m + 1);
        }
    } else {
        if(n - 0.5 > m) {
            printf("%d\n", m + 1);
        } else {
            printf("%d\n", m);
        }
    }
        
}
int main()
{
    float n;
    while(~scanf("%f", &n))
        Round(n);
    return 0;
}   

10、利用莱布利兹公式计算PI

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
double count(int n)
{
    int t, i;
    double ans = 0.0;
    t = 1;
    for(i = 1; i <= n; i++) {
        ans += t * 1.0 / (2 * i - 1);
        t *= -1;
    }
    return 4 * ans;
}
int main()
{
    printf("%f\n", count(10000));
    return 0;
}   

11、通过扇形的面积近似计算PI

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define N 100
void solve()
{
    int i;
    double w, r, ans, x;
    r = 2.0;
    w = r / N;
    ans = 0.0;
    for(i = 1; i <= N; i++) {
        x = w * (i - 0.5);
        ans += sqrt(r * r - x * x) * w;
    }
    printf("%f\n", ans);
}
int main()
{
    solve();
    return 0;
}   

 

posted @ 2014-05-26 10:14  wuyudong  阅读(1116)  评论(0编辑  收藏  举报
Top_arrow