zoukankan      html  css  js  c++  java
  • Ch2. Loop Structure

    Ex

    Input some integers and output their min, max and average values (keep three decimal places). It is guaranteed that each input number is a integer less than 1000.

    Input contains many datasets, the first line of each dataset is the number of integers n, and the second line contains the n numbers. And the end mark of input is n=0, the program should overlook this dataset. There should be empty row between contiguous datasets.

    Sample input:

    8

    2 8 3 5 1 7 3 6

    4

    -4 6 10 0

    Sample ouput:

    Case 1: 1 8 4.375

    Case 2: -4 10 3.00

    #include<stdio.h>
    #define inf 1000000
    int main()
    {
        int x, n, min, max=0, s=0, kase=0;
        while(scanf("%d", &n) ==1 && n, min=inf, max=-inf){
            int s=0;
            for(int i=0;i<n;i++){
                scanf("%d",&x);
                s += x;
                if( x< min) min = x;
                if( x> max) max = x;
            }
            if(kase) printf("
    ");
            printf("Case %d: %d %d %.3f
    ", ++kase, min, max, (double)s/n);
        }
        return 0;
    }

     

    2-1 daffodil

    Print all the daffodil numbers from 100-999. If the triple digits ABC meets the condition ABC=A*A*A+B*B*B+C*C*C, then it is a daffodil number. For example, 153=1*1*1+5*5*5+3*3*3, so 153 is a daffodil number.

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int n;
        for(n=100; n<=999; n++)
            if (n == pow(n/100,3)+pow(n/10%10,3)+pow(n%10,3))
                printf("%d
    ", n);
            else
                continue;
        return 0;
    }

    2-2 hanxin

    Hanxin let soldiers stand three people per line, five people per line and seven people per line, and he only needs to check  the end line to know the total number of people.

     

    Input many datasets, and each dataset contains three non-negetive integer a,b,c, which represents the end line number of people (a<3, b<5, c<7), output the minimium  number of people (or report No answer). Given the total population is not less than 10 and no more than 100. Input ends at the end of file.

    For example,

    input:

    2 1 6

    2 1 3

    output:

    Case 1: 41

    Case 2: No answer

    #include<stdio.h>
    int main()
    {
        int a,b,c, kase=0;
        scanf("%d%d%d", &a, &b, &c);
        bool flag=false;
        int n = 10;
        while( flag == false && n<=100){
            if((n%3 == a) && (n%5 == b) && (n%7 == c))
            {
                     flag = true;
            }
            else
                n++;
        }
        if (flag == true)
            printf("%d
    ", n);
        else
            printf("No answer
    ");
        return 0;
    }

    2-3 triangle

    Input the positive integer n<=20, output a inverted triangle. 

    For example, n=5:

    *********

     ******* 

       ****

         *

     

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

    2-4 susequence

    Input two integers n<m<10^6, output 1/n^2+1/(1+n)^2+...+1/m^2, keep 5 decimal places. Input consists of many datasets, the end mark is n=m=0. 

    Hint: Mind the trap.

    Sample input:

    2 4

    65536 655360

    0 0

    Sample output:

    Case 1: 0.42361

    Case 2: 0.00001

    #include<stdio.h>
    int main()
    {
        int n, m =0;
        double s=0;
        scanf("%d%d", &n, &m);
        for(int i = 0; i <= m-n; i++){
                s +=1.0/(n+i)/(n+i);
            }
        printf("%.5f", s);
        return 0;
    }

    Trap: When the input number, n or m is very large, their product will be very large and overflow. Thus it n or m should be divided twice. 

    2-5 decimal

    Input integer a, b,c and output the decimal format of a/b, and rounded to c decimal places. a,b <=10^6, c<=100.

     

    Input

    1 6 4

     

    Output

    0.1667

     

     

    #include<stdio.h>
    int main()
    {
        int a=28,b=3,c=4;
        int d=a%b;
    
        int s;
        printf("0.");
        for(int i=0;i<c;i++)
        {
            d=d*10;
            s=d/b;
            d=d%b;
            printf("%d",s);
        }
    }

    2-6 permutation

    Form three three-digit numbers abc, def and ghi from 1,2,3,~, 9. Each figure should only be used once. abc: def: ghi=1:2:3. Output all solutions in order "abc def ghi". One solution each line. Don't think too hard on it.

    #include<stdio.h>
    int main()
    {
        int m;
        for(m=100;m<=333;m++)
            {
        if((m/100) != (m/10%10)&& (m/100)!= (m%10)&& (m%10)!= (m/10%10)
           && ((m/100)*(m/10%10)* (m%10)* (2*m/10%10) *(2*m%10)*(2*m/100)
           * (3*m/100) * (3*m/10%10) * (3*m%10) == 1*2*3*4*5*6*7*8*9)
           )
    
    
            printf("%d %d %d
    ",m,2*m,3*m);
    }
          return 0;
    }

    Prob 1

    Suppose we need to output 2,4,6,8,...,2n, one figure each line. Alter the following program to achieve this goal.

     

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

    Task 1

    Alter line 7 not line 6. 

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

    Alter line 6 not line 7. 

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

     Prob 1

    What's the result of the following program?

    #include<stdio.h>
    int main()
    {
        double i;
        for(i=0; i!= 10; i+=0.1)
            printf("%.1f
    ", i);
    
    
        return 0;
    }

    A lot of numbers...because i is double and 10 is an integer, and double can not be rounded to integer....

  • 相关阅读:
    Laxcus集群操作系统的分布计算模型
    LAXCUS集群操作系统能不能防止DDOS攻击
    elasticsearch安装启动过程遇到的问题
    Centos7安装snort可视化IDS平台
    网络与信息安全 -国际学术会议和期刊目录
    包的概念、导入与可见性---Go
    Encrypted Traffic Analytics 加密流量分析功能
    商务谈判技巧
    东南大学《数字图像处理》课程作业 8
    东南大学《数字图像处理》课程作业 7
  • 原文地址:https://www.cnblogs.com/prmlab/p/6520394.html
Copyright © 2011-2022 走看看