>

9编制程序练习

- 编辑:金沙国际平台登录 -

9编制程序练习

本文地址:

1、温度转变:

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

2、长度转变:

#include<stdio.h>int main(){    double YCI, YC, M;    while(~scanf("%lf", &M)){        printf("YCI:%lfn", 1.0 / 0.0254);        printf("YC:%lfn", 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("%dn", 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 *  - 1;    printf("%dn", 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 %dn", max);    return 0;}   

6、反转打字与印刷输入的整数

#include<stdio.h>void print(int N){    printf("The reverse number is ");    while {        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;    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;    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            printf("%dn", i);    }    return 0;}   

8、根据钦命格式分解质因数

#include<stdio.h>#include<math.h>#include<stdbool.h>#define N 100000int 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            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 {            printf("%dn", 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;    if(n > 0) {        if(n - m < 0.5) {            printf("%dn", m);        } else {            printf("%dn", m + 1);        }    } else {        if(n - 0.5 > m) {            printf("%dn", m + 1);        } else {            printf("%dn", m);        }    }        }int main(){    float n;    while(~scanf("%f", &n))        Round;    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("%fn", count(10000));    return 0;}   

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

#include<stdio.h>#include<math.h>#include<stdbool.h>#define N 100void 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("%fn", ans);}int main(){    solve();    return 0;}   

本文由编程发布,转载请注明来源:9编制程序练习