Age and Illness - Godlike Memory and Speed - Learned Another Trick Today

**Key questions
1. How does the C language input percentage function use two % to indicate **
2. How to input a line of numbers to display the specified number of inputs, then use the for loop, and then use the sanf function to solve the number in one column, accounting for A lot of memory, great!

Total time limit: 1000ms Memory limit: 65536kB Description
A hospital wants to count whether the acquisition of a certain disease is related to age, and needs to sort out the previous diagnosis records, according to 0-18, 19-35, 36-60, 61
The proportion of the number of sick people in the four age groups above (including 61) to the total number of sick people.

Enter
a total of 2 lines, the first line is the number n of past patients (0 < n <= 100), and the second line is the age of each patient at the time of illness.
Output
According to the four age groups of 0-18, 19-35, 36-60, 61 and above (including 61), output the proportion of the number of patients in this period to the total number of patients, output in the form of a percentage, accurate to two decimal places . One row for each age group, four rows in total.
Sample input
10
1 11 21 31 41 51 61 71 81 91
Sample output
20.00%
20.00%
20.00%
40.00%
Small memory method

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int n,a[100],i=0,value;
    scanf("%d",&n);
    int k = i,m1=0,m2=0,m3=0,m4=0;
    for(int j =0;j<n;j++){
        scanf("%d",&value);
        if(value>0&&value<=18)
          m1=m1+1;
        else if(value>=19&&value<=35)
          m2 = m2 +1;
        else if(value>=36&&value<=60)
          m3 = m3 +1;
        else if(value>=61)
          m4 = m4 +1;     
    };
    float n1=n;
    printf("%.2f%%\n%.2f%%\n%.2f%%\n%.2f%%\n",(m1/n1)*100,(m2/n1)*100,(m3/n1)*100,(m4/n1)*100);
    return 0;
}

Larger memory method - use for loop with caution

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int n,a[100],i=0;
    scanf("%d",&n);
    if (n<0||n>100)
       exit(0);
    while(scanf("%d",&a[i++]))
    {
        if(getchar()=='\n')
        break;
    };
    int k = i,m1=0,m2=0,m3=0,m4=0;
    for(int j =0;j<k;j++){
        if(a[j]>0&&a[j]<=18)
          m1=m1+1;
        else if(a[j]>=19&&a[j]<=35)
          m2 = m2 +1;
        else if(a[j]>=36&&a[j]<=60)
          m3 = m3 +1;
        else if(a[j]>=61)
          m4 = m4 +1;     
    };
    float n1=n;
    printf("%.2f%%\n%.2f%%\n%.2f%%\n%.2f%%\n",(m1/n1)*100,(m2/n1)*100,(m3/n1)*100,(m4/n1)*100);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731592&siteId=291194637