๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[๋ฐฑ์ค€/C++] 4344๋ฒˆ : ํ‰๊ท ์€ ๋„˜๊ฒ ์ง€

by nitronium102 2021. 7. 8.

๋ฌธ์ œ

๋Œ€ํ•™์ƒ ์ƒˆ๋‚ด๊ธฐ๋“ค์˜ 90%๋Š” ์ž์‹ ์ด ๋ฐ˜์—์„œ ํ‰๊ท ์€ ๋„˜๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๋‹น์‹ ์€ ๊ทธ๋“ค์—๊ฒŒ ์Šฌํ”ˆ ์ง„์‹ค์„ ์•Œ๋ ค์ค˜์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ํ•™์ƒ์˜ ์ˆ˜ N(1 ≤ N ≤ 1000, N์€ ์ •์ˆ˜)์ด ์ฒซ ์ˆ˜๋กœ ์ฃผ์–ด์ง€๊ณ , ์ด์–ด์„œ N๋ช…์˜ ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ ์ˆ˜๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

๊ฐ ์ผ€์ด์Šค๋งˆ๋‹ค ํ•œ ์ค„์”ฉ ํ‰๊ท ์„ ๋„˜๋Š” ํ•™์ƒ๋“ค์˜ ๋น„์œจ์„ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

ํ’€์ด

- ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ 1000์œผ๋กœ ์„ ์–ธ

- ์†Œ์ˆ˜์  ์ž๋ฆฌ์˜ ๊ฒฝ์šฐ cout << fixed์™€ cout.precision์„ ์ด์šฉํ•˜์—ฌ ์†Œ์ˆ˜์  ์ž๋ฆฌ์˜ ๊ธธ์ด๋ฅผ ๊ณ ์ •ํ•œ๋‹ค.

- ์ž๋ฃŒํ˜•์— ์ฃผ์˜! ์ด์ƒํ•˜๊ฒŒ ์ž๊พธ ํ‹€๋ ธ๋‹ค๊ณ  ํ•ด์„œ ์ฐพ์•„๋ดค๋Š”๋ฐ result = (double)num/n*100์—์„œ double๋กœ ์•ˆ ๊ณ ์ณ์ค˜์„œ result๊ฐ’์— ์ •์ˆ˜ ๋‚˜๋ˆ„๊ธฐ๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒƒ์ด๋‹ค.

#include <iostream>
using namespace std;

int main()
{
    int c, n, num = 0;
    double result, avg = 0;
    int score[1000] = {0};
    cin >> c; // ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๊ฐœ์ˆ˜

    for (int i = 0; i < c; i++)
    {
        cin >> n; // ํ•™์ƒ์˜ ์ˆ˜
        // N๋ช…์˜ ์ ์ˆ˜ ๋ฐ›๊ธฐ
        for (int j = 0; j < n; j++)
        {
            cin >> score[j];
            avg += score[j];
        }
        avg = avg / n;
        // ํ‰๊ท ์„ ๋„˜๋Š” ํ•™์ƒ๋“ค ๋น„์œจ ๋ฐ˜์˜ฌ๋ฆผ
        for (int j = 0; j < n; j++)
        {
            if (score[j] > avg)
                num++;
        }
        result = (double)num / n * 100;
        cout << fixed;
        cout.precision(3);
        cout << result << "%" << endl;

        avg = 0;
        num = 0;
    }
}

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

 

4344๋ฒˆ: ํ‰๊ท ์€ ๋„˜๊ฒ ์ง€

๋Œ€ํ•™์ƒ ์ƒˆ๋‚ด๊ธฐ๋“ค์˜ 90%๋Š” ์ž์‹ ์ด ๋ฐ˜์—์„œ ํ‰๊ท ์€ ๋„˜๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๋‹น์‹ ์€ ๊ทธ๋“ค์—๊ฒŒ ์Šฌํ”ˆ ์ง„์‹ค์„ ์•Œ๋ ค์ค˜์•ผ ํ•œ๋‹ค.

www.acmicpc.net


๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์ž(/')๋Š” ๋‘ ๊ฐ€์ง€ "๋ชจ๋“œ(mode)"๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ํ”ผ์—ฐ์‚ฐ์ž ๋ชจ๋‘ ์ •์ˆ˜๋ผ๋ฉด / ์—ฐ์‚ฐ์ž๋Š” ์ •์ˆ˜ ๋‚˜๋ˆ„๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ •์ˆ˜์˜ ๋ชจ๋“  ์†Œ์ˆ˜์ ์€ ์‚ญ์ œํ•˜๊ณ  ์ •์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (Ex. 7 / 4 = 1)

๋‘ ํ”ผ์—ฐ์‚ฐ์ž ์ค‘ ํ•˜๋‚˜ ์ด์ƒ์ด ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž๋ฉด / ์—ฐ์‚ฐ์ž๋Š” ๋ถ€๋™ ์†Œ์ˆ˜์  ๋‚˜๋ˆ„๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. (Ex. 7.0/3=2.333, 7/3.0=2.333, 7.0/3.0=2.333)


์ถœ์ฒ˜: https://boycoding.tistory.com/158 [์†Œ๋…„์ฝ”๋”ฉ]

๋Œ“๊ธ€