zoukankan      html  css  js  c++  java
  • Ch1. Intro to Programming

    1-1 Input three integers and output the average number. Keep three decimal places. 

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int a, b, c;
        scanf("%d%d%d",&a,&b,&c);
        printf("%.3f
    ", (a+b+c)/3.0);
    }

    1-2 Input  Fahrenheit temperature f, output the centigrade degree c. Keep three decimal places. Hint: c=5(f-21)/9.

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        float f,c;
        scanf("%f",&f);
        c=5*(f-21)/9;
        printf("%.3f
    ", c);
    }

    1-3 Input a positive integer n, output the value of 1+2+3+...+n.

    Hint: Solve the problem rather than exercise programming. 

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int n,sum;
        sum=0;
        scanf("%d", &n);
        /*if(n == 1)
            printf("1
    ");
        else */
          {
              for(int i=1; i<=n; i++)
                sum=sum+i;
              printf("%d
    ", sum);
          }
    
    }

    1-4 Input positive integer n (n<360), output the sin and cos value of n.

    Hint: Use math functions.

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int n;
        scanf("%d", &n);
        printf("%f %f
    ", sin(n), cos(n));
    }

    1-5 A piece of clothes costs 95 yuan, and if the amount adds up to 300 yuan, it will have a 85% discount. Input  the sales number of clothes and output the money one should pay (yuan). Keep two places of decimal.

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int n;
        float total;
        scanf("%d", &n);
        total=n*95;
        if (total<300)
            printf("%.2f
    ", total);
        else
            printf("%.2f
    ", total*0.85);
    }

    1-6 Input three lengths of a triangle (all possitive integers), and determine whether they can be three sides of a triangle. If so, output yes; otherwise, output not a triangle.

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

    1-7 Input a year and determine if it is a leap year. If yes, output yes; otherwise, output no.

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int n;
        scanf("%d", &n);
        if ((n%400 == 0) || ((n%4 == 0) && (n%100 != 0 )))
            printf("yes");
        else
            printf("no");
        return 0;
    }

    Q1 What's the minimum and maximum number of int integers? (give accurate number)

    #include<stdio.h>
    //#include<math.h>
    int int_min()
    {
        int n=0, i=0;
        while(n>=i)
        {
            n=i;
            i--;
        }
        return n;
    }
    int int_max()
    {
        int n=0, i=0;
        while(n <=i)
        {
            n=i;
            i++;
        }
        return n;
    }
    
    int main()
    {
        printf("%d
    ", int_min());
        printf("%d
    ", int_max());
    }

    Q2 How actuate decimal places can double floats can be? Or, is it a right question?

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        double i =1.0, j = 3.0;
        printf("%.500f
    ", i/j);
        return 0;
    }

    Ref. https://www.zhihu.com/question/36662447/answer/68630339

    It shows 0.333333333333333310000000000...    (sixteen 3s ) so, there are maximum 16 decimal places in a double float. And  a double float can not acturately describe decimals.

    Q3 What's the minimum and maximum positive integer of double floats? (no need to give accurate numbers)

     (I don't know...)

    Ref. http://blog.csdn.net/architect19/article/details/8551145

    http://www.faceye.net/search/87209.html

    Not working through...

    Q4 What's the precedence of these logical operators, "&&", "||", and "!" ? How do you interpret a&&b||c ?

    "&&" and "||" at the same precedence level, and are operated from left to right.

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int a=2, b=0,c=1;
        printf("%d", b&&c||a);
        return 0;
    }

    It shows 1. 

    "!" has higher priority than "&&" and "||" .

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int a=2, b=0,c=1;
        printf("%d", !b&&c||a);
        return 0;
    }

    It shows 1.

    #include<stdio.h>
    //#include<math.h>
    int main()
    {
        int a=2, b=0,c=1;
        printf("%d", !(b&&c||a));
        return 0;
    }

    It shows 0. 

    Q5 What's the accurate meaning of if(a) if(b) x++; else y++? The else matches which if? Any accturate way to express the match?

    The esle matches the second if.

    #include<stdio.h>
    int main()
    {
        int a, b,x, y;
        if(a){
            if (b)
                x++;
            else
                y++;
        }
        return 0;
    }

    Use braces to enclose a series of codes following if(a). 

     

  • 相关阅读:
    云时代架构阅读笔记时十
    云时代架构”经典文章阅读感想九
    “云时代架构”经典文章阅读感想八
    antlr4
    Centos7定时脚本
    JDBC连接hive失败,一种原因的解决方法
    Linux实用操作命令
    secureCRT下载虚拟机文件到本地
    OpenFeign执行POST请求类型以及Python的requests.post()方法
    DataSphere安装配置
  • 原文地址:https://www.cnblogs.com/prmlab/p/6500700.html
Copyright © 2011-2022 走看看