• C语言教材答案


    C语言教材答案

    可能会错,有问题联系QQ3364445435

    第五章

    [collapse status="false" title="5.5"]

    #include <stdio.h>
    #include <stdlib.h>
    int main() {
        char c1 = 'C', c2 = 'h', c3 = 'i', c4 = 'n', c5 = 'a';
        printf("%c%c%c%c%c", c1 + 4, c2 + 4, c3 + 4, c4 + 4, c5 + 4);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="5.6"]

    /*
    题目:小明和小红和你正在玩数字游戏,小明要随机说出一个自然数,而小红会随机说出一个整数,你的任务是去计算这两个数的和。
    自然数符合unsigned int范围,整数符合int范围。
    */
    #include <stdio.h>
    int main() {
        unsigned int a;
        int b;
        scanf("%d%d", &a, &b);
        long ans = a + b;
        printf("%ld", ans);
        return 0;
    }
    
    
    

    [/collapse]

    [collapse status="false" title="5.7"]

    #include <stdio.h>
    int main() {
        short s = -32768;
        printf("%d
    ", s - 2);
        s = s - 2;
        printf("%d", s);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="5.9"]

    #include <stdio.h>
    int main() {
        int a = 4, b = 6;
        printf("%x
    ", a * b);
        return 0;
    }
    

    [/collapse]

    第六章

    [collapse status="false" title="6.1"]

    #include <stdio.h>
    int main() {
        double a, h;
        scanf("%lf%lf", &a, &h);
        double ans = 1.00 / 2 * a * h;
        printf("%f
    ", ans);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.2"]

    #include <stdio.h>
    int main() {
        int a;
        scanf("%d", &a);
        char ch1 = a / 100 + '0';
        char ch2 = a / 10 % 10 + '0';
        char ch3 = a % 10 + '0';
        printf("%c%c%c
    ", ch3, ch2, ch1);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.3"]

    #include <stdio.h>
    int main() {
        double a, b, c;
        scanf("%lf%lf%lf", &a, &b, &c);
        double maxx, minn;
        a > b ? maxx = a : b > c ? maxx = b : maxx = c;
        a < b ? minn = a : b < c ? minn = b : minn = c;
        printf("%f
    %f", maxx, minn);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.4"]

    /*
    题目:给出两个整数a,b,求((a + 1) * (b - 1) + (a + 2) / (b - 2)) % 2的结果
    */
    #include <stdio.h>
    int main() {
        int a, b;
        scanf("%d%d", &a, &b);
        int tmp1 = (++a) * (--b);
        int tmp2 = (a + 1) / (b - 1);
        int ans = (tmp1 + tmp2) % 2;
        printf("%d
    ", ans);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.5"]

    #include <stdio.h>
    int main() {
        int a, b;
        scanf("%d%d", &a, &b);
        int c, d, e, f, g, h;
        g = e = c = a;
        h = f = d = b;
        printf("a++:%d
    ", a ++ );
        printf("b--:%d
    ", b -- );
        a -- , b ++ ;
        printf("++a:%d
    ", ++ a);
        printf("--b:%d
    ", -- b);
        printf("a++ + b++:%d
    ", c++ + d++);
        printf("++a + ++b:%d
    ", ++e + ++f);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.6"]

    (1) num % 6 == 0;
    (2) x > -2 && x < 2 && y > -2 && y < 2 && (x * x) + (y * y) > 1
    (3) a + b > c && a + c > b && b + c > a 
    

    [/collapse]

    [collapse status="false" title="6.7"]

    #include <stdio.h>
    #include <math.h>
    int main() {
        int a, b, c, maxx = 0, flag;
        double ans;
        scanf("%d%d%d", &a, &b, &c);
        a > maxx ? maxx = a, flag = 1:1;
        b > maxx ? maxx = b, flag = 2:1;
        c > maxx ? maxx = c, flag = 3:1;
        flag == 1 ? ans = sqrt(1 - (1.00 * (b * b + c * c - a * a) / (2 * b * c)) * (1.00 * (b * b + c * c - a * a) / (2 * b * c))):1;
        flag == 2 ? ans = sqrt(1 - (1.00 * (c * c + a * a - b * b) / (2 * a * c)) * (1.00 * (c * c + a * a - b * b) / (2 * a * c))):1;
        flag == 3 ? ans = sqrt(1 - (1.00 * (b * b + a * a - c * c) / (2 * a * b)) * (1.00 * (b * b + a * a - c * c) / (2 * a * b))):1;
        printf("%f", ans);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.8"]

    #include <stdio.h>
    int main() {
        // printf("输入时间:");
        int a, b;
        scanf("%d%d", &a, &b);
        double time = a + b / 60.0;
        double tmp1 = 4 * time * time;
        double tmp2 = time + 2;
        double ans = tmp1 / tmp2 - 20;
        printf("%.2f
    ", ans);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.9"]

    #include <stdio.h>
    int main() {
        int strx, endx;
        int strh, strm, strs;
        int endh, endm, ends;
        int x, timh, timm, tims;
        double ans;
        printf("所有输入数据为整数
    ");
        printf("开始里程?
    ");
        scanf("%d", &strx);
        printf("开始时间(时,分,秒)
    ");
        scanf("%d%d%d", &strh, &strm, &strs);
        printf("结束里程?
    ");
        scanf("%d", &endx);
        printf("结束时间(时,分,秒)
    ");
        scanf("%d%d%d", &endh, &endm, &ends);
        x = endx - strx;
        printf("汽车行驶了%d公里
    ", x);
        ends - strs >= 0 ? tims = ends - strs : endm -= 1, tims = ends + 60 - strs;
        endm - strm >= 0 ? timm = endm - strm : endh -= 1, timm = endm + 60 - strm;
        timh = endh - strh;
        printf("消耗时间%d小时%d分%d秒
    ", timh, timm, tims);
        ans = 1.00 * x / (timh + 1.0 * timm / 60 + 1.0 * tims / 3600);
        printf("平均速度:%.4f公里/小时", ans);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="6.10"]

    #include <stdio.h>
    #define dj 10800
    #define tc 1000
    int main() {
        int mon, num;
        double ans = 0;
        printf("请输入基本工资、销售台数
    ");
        scanf("%d%d", &mon, &num);
        ans = mon;
        ans += tc * num;
        dj * num > 100000 ? ans += 0.02 * dj * num : 1;
        printf("%f", ans);
        return 0;
    }
    

    [/collapse]

    第七章

    [collapse status="false" title="7.1"]

    #include <stdio.h>
    #include <math.h>
    
    int main() {
        int a, b, c, tmp;
        scanf("%d %d %d", &a, &b, &c);
        if (a > b) {
            if (b > c) {
                printf("%d %d %d", c, b, a);
            }
            else {
                if (a > c) {
                    printf("%d %d %d", b, c, a);
                }
                else {
                    printf("%d %d %d", b, a, c);
                }
            }
        }
        else {
            if (c < a) {
                printf("%d %d %d", c, a, b);
            }
            else {
                if (b > c) {
                    printf("%d %d %d", a, c, b);
                }
                else {
                    printf("%d %d %d", a, b, c);
                }
            }
        }
        
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="7.2"]

    #include <stdio.h>
    int main() {
        int w;
        double h, ans;
        scanf("%lf%d", &h, &w);
        ans = 1.00 * w / (h * h);
        if (ans < 18) {
            printf("偏瘦
    ");
        }
        else if (ans <= 25) {
            printf("正常体重
    ");
        }
        else if (ans < 27){
            printf("超重体重
    ");
        }
        else {
            printf("肥胖
    ");
        }
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="7.5"]

    #include <stdio.h>
    int main() {
        char ch;
        scanf("%c", &ch);
        if (ch >= '0' && ch <= '9') {
            printf("数字
    ");
        }
        else if (ch >= 'A' && ch <= 'Z') {
            printf("大写字母
    ");
        }
        else if (ch >= 'a' && ch <= 'z') {
            printf("小写字母
    ");
        }
        else {
            printf("其他字符
    ");
        }
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="7.7"]

    //整数:
    // #include <stdio.h>
    // int main() {
    //     int a, b;
    //     int ans;
    //     char ch;
    //     scanf("%d %c %d", &a, &ch, &b);
    //     if (ch == '+') {
    //         ans = a + b;
    //     }
    //     if (ch == '-') {
    //         ans = a - b;
    //     }
    //     if (ch == '*') {
    //         ans = a * b;
    //     }
    //     if (ch == '/') {
    //         ans = a / b;
    //     }
    //     printf("%d", ans);
    //     return 0;
    // }
    
    
    
    /*浮点数:
    #include <stdio.h>
    int main() {
        double a, b;
        double ans;
        char ch;
        scanf("%lf %c %lf", &a, &ch, &b);
        if (ch == '+') {
            ans = 1.0 * a + b;
        }
        if (ch == '-') {
            ans = 1.0 * a - b;
        }
        if (ch == '*') {
            ans = 1.0 * a * b;
        }
        if (ch == '/') {
            ans = 1.0 * a / b;
        }
        printf("%f", ans);
        return 0;
    }
    */
    
    //多次:
    // #include <stdio.h>
    // int main() {
    //     int a, b;
    //     double ans = 0;
    //     char ch;
    //     scanf("%d", &a);
    //     ans += a;
    //     while (1) {
    //         getchar();
    //         do {
    //             scanf("%c", &ch);
    //         } while(ch == ' ' || ch == '
    ');
    //         scanf("%d", &b);
    //         if (ch == '+') {
    //             ans += b;
    //         }
    //         else if (ch == '-') {
    //             ans -= b;
    //         }
    //         else if (ch == '*') {
    //             ans *= b;
    //         }
    //         else if (ch == '/') {
    //             ans /= 1.0 * b;
    //         }
    //         printf("%f", ans);
    //     }
    //     return 0;
    // }
    
    

    [/collapse]

    [collapse status="false" title="7.8"]

    #include <stdio.h>
    int main() {
        int year, month, day, maxx;
        bool run = 0;
        scanf("%d%d%d", &year, &month, &day);
        if (month > 12 || month < 1) {
            printf("无效
    ");
            return 0;
        }
        switch (month)
        {
        case 1 : case 3 : case 5: case 7 : case 8 : case 10 : case 12 :
            maxx = 31; break;
        case 2 :
            maxx = 28; break;
        default:
            maxx = 30; break;
        }
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            if (month == 2) {
                maxx += 1;
            }
        }
        if (day > maxx || day < 1) {
            printf("无效
    ");
            return 0;
        }
        printf("有效
    ");
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="7.10"]

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        int n;
        scanf("%d", &n);
        int a = n / 1000, b = n % 1000 / 100, c = n % 100 / 10, d = n % 10;
        if (a != 0) {
            switch(a) {
                case 1 : printf("壹");break;   case 2 : printf("贰");break;   case 3 : printf("叁");break; 
                case 4 : printf("肆");break;   case 5 : printf("伍");break;   case 6 : printf("陆");break; 
                case 7 : printf("柒");break;   case 8 : printf("捌");break;   case 9 : printf("玖");break;
            }
            printf("千");
        }
        switch(b) {
            case 1 : printf("壹");break;   case 2 : printf("贰");break;   case 3 : printf("叁");break; 
            case 4 : printf("肆");break;   case 5 : printf("伍");break;   case 6 : printf("陆");break; 
            case 7 : printf("柒");break;   case 8 : printf("捌");break;   case 9 : printf("玖");break;
            case 0 : printf("零");break;
        }
        if (b != 0) {
            printf("百");
        }
        switch(c) {
            case 1 : printf("壹");break;   case 2 : printf("贰");break;   case 3 : printf("叁");break; 
            case 4 : printf("肆");break;   case 5 : printf("伍");break;   case 6 : printf("陆");break; 
            case 7 : printf("柒");break;   case 8 : printf("捌");break;   case 9 : printf("玖");break;
            case 0 : printf("零");break;
        }
        if (c != 0) {
            printf("拾");
        }
        switch(d) {
            case 1 : printf("壹");break;   case 2 : printf("贰");break;   case 3 : printf("叁");break; 
            case 4 : printf("肆");break;   case 5 : printf("伍");break;   case 6 : printf("陆");break; 
            case 7 : printf("柒");break;   case 8 : printf("捌");break;   case 9 : printf("玖");break;
        }
        return 0;
        
    }
    

    [/collapse]

    第八章

    [collapse status="false" title="8.1"]

    /*(1)*/
    //for循环
    
    // #include <stdio.h>
    // int main() {
    //     int n, ans = 1;
    //     scanf("%d", &n);
    //     for (int i = n; i >= 1; i -- ) {
    //         ans *= i;
    //     }
    //     printf("%d
    ", ans);
    //     return 0;
    // }
    
    //while循环
    
    // #include <stdio.h>
    // int main() {
    //     int n, ans = 1;
    //     scanf("%d", &n);
    //     while (n > 0) {
    //         ans *= n;
    //         n --;
    //     }
    //     printf("%d
    ", ans);
    //     return 0;
    // }
    
    //do-while循环
    
    // #include <bits/stdc++.h>
    // int main() {
    //     int n, ans = 1;
    //     scanf("%d", &n);
    //     do {
    //         ans *= n;
    //     }while (-- n);
    //     printf("%d
    ", ans);
    //     return 0;
    // }
    
    /*(2)*/
    //for循环
    
    // #include <stdio.h>
    // int main() {
    //     int m, now = 1, ans = 0;
    //     scanf("%d", &m);
    //     for (int i = 1; i <= m; i ++ ) {
    //         now *= i;
    //         ans += now;
    //     }
    //     printf("%d
    ", ans);
    //     return 0;
    // }
    
    //while循环
    
    // #include <stdio.h>
    // int main() {
    //     int m, now = 1, ans = 0;
    //     scanf("%d", &m);
    //     int i = m;
    //     while(i -- ) {
    //         now *= m - i;
    //         ans += now;
    //     }
    //     printf("%d", ans);
    //     return 0;
    // }
    
    
    
    
    /*(3)*/
    //for循环
    
    // #include <stdio.h>
    // int main() {
    //     int now = 1;
    //     double ans = 1.0;
    //     for (int i = 1; i <= 10; i ++ ) {
    //         now *= i;
    //         ans += 1.0 / now;
    //     }
    //     printf("%f
    ", ans);
    //     return 0;
    // }
    
    //while循环
    
    // #include <stdio.h>
    // int main() {
    //     int now = 1, maxx = 10;
    //     double ans = 1.0;
    //     while (maxx -- ) {
    //         now *= 10 - maxx;
    //         ans += 1.0 / now;
    //     }
    //     printf("%f
    ", ans);
    //     return 0;
    // }
    
    /*(4)*/
    //for循环
    
    // #include <stdio.h>
    // int main() {
    //     int x, now = 1;
    //     double ans = 1.0;
    //     scanf("%d", &x);
    //     for (int i = 1; i <= 10; i ++ ) {
    //         now *= i;
    //         ans += 1.0 * x / now;
    //     }
    //     printf("%f
    ", ans);
    //     return 0;
    // }
    
    //while循环
    
    // #include <stdio.h>
    // int main() {
    //     int now = 1, maxx = 10;
    //     int x;
    //     double ans = 1.0;
    //     scanf("%d", &x);
    //     while (maxx -- ) {
    //         now *= 10 - maxx;
    //         ans += 1.0 * x / now;
    //     }
    //     printf("%f
    ", ans);
    //     return 0;
    // }
    
    
    

    [/collapse]

    [collapse status="false" title="8.2"]

    #include <stdio.h>
    #define pi 3.1415926
    int main() {
    
    	//公式1
    	double ans1 = 0, ans2 = 1;
    	int n = 1, sgn = 1; // sgn为符号函数
    	while(ans1 * 4.00 < pi - 1e-7 || ans1 * 4.00 > pi + 1e-7) {
    		ans1 += sgn * 1.00 / (2.00 * n - 1);
    		n ++ ;
    		sgn = -sgn;
    	}
    	printf("%.7lf
    ", ans1 * 4);
    	//公式2
    	n = 1; //计数器清零
    	while(ans2 * 2.00 < pi - 1e-7 || ans2 * 2.00 > pi + 1e-7) {
    		double tmp = 4.00 * n * n;
    		ans2 *= tmp / (tmp - 1);
    		// (2n * 2n) / ((2n - 1)*(2n + 1)) = tmp / (tmp - 1)
    		n ++ ;
    	}
    	printf("%.7lf
    ", ans2 * 2);
    	return 0;
    }
    

    [/collapse]

    [collapse status="false" title="8.3"]

    // 穷举法
    
    // #include <stdio.h>
    // int main() {
    //     int n, m, minn;
    //     scanf("%d%d", &n, &m);
    //     n > m ? minn = m : minn = n;
    //     for (int i = minn; i >= 1; i -- ) {
    //         if (n % i == 0 && m % i == 0) {
    //             printf("%d
    ", i);
    //             break;
    //         }
    //     }
    //     return 0;
    // }
    
    
    // 迭代法
    
    // #include <stdio.h>
    // int main() {
    //     int n, m, tmp, now;
    //     scanf("%d%d", &n, &m);
    //     if (m > n) {
    //         int tmp = n;
    //         n = m;
    //         m = tmp;
    //     }
    //     while (m != 0) {
    //         now = n % m;
    //         n = m;
    //         m = now;
    //     }
    //     printf("%d
    ", n);
    //     return 0;
    // }
    

    [/collapse]

    [collapse status="false" title="8.4"]

    #include <stdio.h>
    int main() {
        int cnt = 2;
        int tmp1 = 0, tmp2 = 1;
        printf("%-5d %-5d ", tmp1, tmp2);
        for (int i = 3; i <= 20; i ++ ) {
            int now = tmp1 + tmp2;
            tmp1 = tmp2;
            tmp2 = now;
            printf("%-5d ", now);
            if (i % 8 == 0) {
                printf("
    ");
            }
        }
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="8.5"]

    #include <stdio.h>
    int main() {
        int tmp1 = 1;
        int tmp2 = 2;
        double ans = 0;
        for (int i = 1; i <= 20; i ++ ) {
            ans += 1.0 * tmp2 / tmp1;
            int tmp = tmp1 + tmp2;
            tmp1 = tmp2;
            tmp2 = tmp;
        }
        printf("%f
    ",ans);
        return 0;
    }
    

    [/collapse]

    [collapse status="false" title="8.6"]

    //判断是否为素数
    
    // #include <stdio.h>
    // int main() {
    //     int a;
    //     scanf("%d", &a);
    //     if (a == 1) {
    //         printf("No");
    //         return 0;
    //     }
    //     for (int i = 2; i * i <= a; i ++ ) {
    //         if (a % i == 0) {
    //             printf("No");
    //             return 0;
    //         }
    //     }
    //     printf("Yes");
    //     return 0;
    // }
    
    //m,n之间的素数
    
    // #include <stdio.h>
    // int main() {
    //     int n, m, cnt = 0;
    //     bool flag = 1;
    //     scanf("%d%d", &n, &m);
    //     for (int i = n; i <= m; i ++ ) {
    //         if (i == 1) {
    //             continue ;
    //         }
    //         flag = 1;
    //         for (int j = 2; j * j <= i; j ++ ) {
    //             if (i % j == 0) {
    //                 flag = 0;
    //                 break;
    //             }
    //         }
    //         if (flag == 1) {
    //             cnt ++ ;
    //             printf("%d ", i);
    //         }
    //     }
    //     printf("
    total:%d
    ", cnt);
    //     return 0;
    // }
    

    [/collapse]

    [collapse status="false" title="8.7"]

    #include <stdio.h>
    int main() {
        int num;
        bool lazy = 0;
        scanf("%d", &num);
        printf("%d=", num);
        while (num > 1) {
            for (int i = 2; i <= num; i ++) {
                bool flag = 1;
                for (int j = 2; j * j <= i; j ++ ) {
                    if (i % j == 0) {
                        flag = 0;
                        break;
                    }
                } 
                if (flag == 1) {
                    while (num % i == 0) {
                        num /= i;
                        if (lazy == 0) {
                            printf("%d", i);
                            lazy = 1;
                        }
                        else {
                            printf("*%d", i);
                        }
                    }
                }
            }
        }
        return 0;
    }
    
    

    [/collapse]

  • 相关阅读:
    02-cocoapods的安装和使用
    01-唐巧之cocoapods
    class0513(html)
    程序集
    c#面向对象
    html
    dom
    Javascript
    Jquery
    ado.net
  • 原文地址:https://www.cnblogs.com/littleseven777/p/15501556.html
Copyright © 2020-2023  润新知