• 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). 

     

  • 相关阅读:
    转:ITIL的開源軟件
    转:Asp.Net大型项目实践
    转:Ubuntu上apache多端口配置虚拟主机的方法
    转:JS 获取鼠标位置
    转:一切整合分享到新浪网易微博代码
    转:Facebook是如何发布代码的
    转:利用 Amazon Web Services 集成企业应用程序使用 Amazon SQS 发送 XML 消息
    sqlite3 常用操作
    转:4 款消息队列软件产品大比拼
    SQL Server 2008 列转行 实例
  • 原文地址:https://www.cnblogs.com/prmlab/p/6500700.html
Copyright © 2020-2023  润新知