"C" Baekjun4344 Repeating Statement Does Not Work Well

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

https://www.acmicpc.net/problem/4344

#include <stdio.h>

int main()

{
    int testcase;
    scanf("%d",&testcase);
    For(inti=0;i<testcase;i++) //Received the number of test cases
    {
        double arr[1000],sum=0,avg=0,num=0,count=0;
        scanf("%lf",&num); //Received number of strings

        for(int i=0;i<num;i++){
            scanf("%lf",&arr[i]); //Enter string in array arr
        }

        for(int i=0;i<num;i++){
            sum+=arr[i];
            avg=sum/num; //The average of the scores is obtained
        }

        for(int i=0;i<num;i++){
            if(arr[i]>avg)
                count++; //Determines the score above average
        }

        printf("%.3f%\n", count/num*100); //ratio output
    }
}

Input

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
3 70 90 81
9 100 99 98 97 96 95 94 93 91

Expected Results

40.000%
57.143%
33.333%
66.667%
55.556%

Output Results

40.000%
57.143%
33.333%
66.667%

If the test case is 5, the repeat statement should be executed 5 times, but only 4 times.

c

2022-09-20 13:13

1 Answers

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
3 70 90 81
9 100 99 98 97 96 95 94 93 91

When I ran the above input with the corresponding code, I got the following results.

$ cat ../input_large.txt | Debug/main.exe 
40.000%
57.143%
33.333%
66.667%
55.556%

I checked that there are 5 results without any problems.

Just in case, put fflush(stdout); in the last line of the code.

Do not use i in for(inti=0;i<testcase;i++). These codes can lead to mistakes, making it difficult to find problems. Typically, i, j, and k are used to declare variables for the index.


2022-09-20 13:13

If you have any answers or tips


© 2022 pinfo. All rights reserved.