반응형

2438번 문제 : 별 찍기

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 
using namespace std;
 
int main() {
    int inp;
    cin >> inp;
 
    for (int i = 0; i < inp; i++)
    {
        for(int j = 0; j < i + 1; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
 
    return 0;
}
cs



메모: 

-endl 대신 cout "\n" 사용하면 4ms 빨라짐



2292번 문제 : 벌집

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
 
int main() {
    int inp;
    cin >> inp;
    int counter = 0;
    int num = 1;
 
    if (inp != 1)
    {
        while (inp > num)
        {
            num = num + 6 * counter;
            counter++;
        }
    }
    else
    {
        counter++;
    }
 
    cout << counter;
 
    return 0;
}
cs


메모: 

-반례 inp : 1때문에 inp !=1 조건문이 필요



1193번 문제 : 분수찾기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
 
int main()
{
    int inp;
    cin >> inp;
        
    int total = 1;
    int row = 1;
    while (total < inp)
    {
        row++;
        total += row;
    }
 
    int col = inp - (total - row);
 
    if (row % 2 != 0)//odd
    {
        cout << row - col + 1 << "/" << col;
    }
    else //even
    {
        cout << col << "/" << row - col + 1;
    }
 
    return 0;
}
 
cs


10250번 문제 : ACM 호텔

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <string>
using namespace std;
 
int main()
{
    int inp;
    cin >> inp;
 
    vector<string> result;
 
    while (inp > 0)
    {
        int h, w, n;
        cin >> h >> w >> n;
 
        string roomNum = "";
        int counter = 1;
        int leftovers = n;
 
        while (leftovers > h) 
        {
            counter++;
            leftovers -= h;
        }
 
        if (counter < 10)
        {
            roomNum += to_string(leftovers);
            roomNum += "0";
            roomNum += to_string(counter);
        }
        else
        {
            roomNum += to_string(leftovers);
            roomNum += to_string(counter);
        }
 
        result.push_back(roomNum);
 
        inp--;
    }
 
    for (auto i : result)
        cout << i << endl;
 
    return 0;
}
 
cs


1924번 문제 : 2007년

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
 
int main(void) {
 
    string dayName[7= { "SUN" , "MON""TUE""WED""THU""FRI""SAT" };
    int month[13= { 0312831303130313130313031 };
    int totalDays = 0;
    int inputM, inputD;
 
    cin >> inputM >> inputD;
 
    for (int i = 0; i < inputM; i++)
        totalDays += month[i];
    totalDays += inputD;
    int leftover = totalDays % 7;
 
    cout << dayName[leftover];
 
    return 0;
}
cs



2775번 문제 : 부녀회장이 될테야

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
 
int arr[15][14= { 0 };
 
int GetNum(int k, int n)
{
    if (k == 0)
        return arr[k][n];
    else
    {
        if (arr[k][n] != 0)
            return arr[k][n];
        else
        {
            for (int i = 0; i <= n; i++)
            {
                arr[k][n] += GetNum(k - 1 , i);
            }
            return arr[k][n];
        }
    }
}
 
int main() {
 
    for (int i = 0; i < 14; i++)
    {
        arr[0][i] = i + 1;
    }
 
    int inp;
    cin >> inp;
 
    while (inp > 0
    {
        int a, b;
        cin >> a >> b;
        cout << GetNum(a, b - 1<< endl;
 
        inp--;
    }
    
 
    return 0;
}
 
cs

메모:
-성능을 위해  메모라이제이션 사용


1475번 문제 : 방 번호

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
 
int main()
{
    int set[10= { 0 };
    int inp;
    cin >> inp;
 
    if (inp == 0)
    {
        cout << "1" << endl;
        return 0;
    }
    else
    {
        while (inp > 0)
        {
            int temp = inp % 10;
            set[temp] ++;
            inp /= 10;
        }
 
        int maxSet = 0;
        for (int i = 0; i < 10; i++)
        {
            if (i == 6 || i == 9)
                continue;
 
            if (set[i] > maxSet)
                maxSet = set[i];
        }
 
        int sixNineSet;
        if ((set[6+ set[9]) % 2 == 0)
            sixNineSet = (set[6+ set[9]) / 2;
        else
            sixNineSet = (set[6+ set[9]) / 2 + 1;
        if (sixNineSet > maxSet)
            maxSet = sixNineSet;
 
        cout << maxSet << endl;
    }
 
    return 0;
}
 
cs


1475번 문제 : 카잉 달력
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
using namespace std;
 
int GCD(int a, int b)
{
    if (a % b == 0)
        return b;
 
    return GCD(b, a % b);
}
 
int LCM(int a, int b)
{
    return (a * b) / GCD(a, b);
}
 
int main()
{
    int inp;
    cin >> inp;
 
    vector<int> result;
 
    while (inp > 0)
    {
        int M, N, x, y;
        cin >> M >> N >> x >> y;
 
        int counter = 1;
        int maxYear = LCM(M, N);
        while (true)
        {
            if (x > maxYear || (x - 1) % N + 1 == y)
                break;
 
            x += M;
        }
 
        if (x > maxYear)
            result.push_back(-1);
        else
            result.push_back(x);
 
        inp--;
    }
 
    for (auto i : result)
        cout << i << "\n";
 
    return 0;
}
 
cs


반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

소수 구하기  (0) 2019.02.13
정렬해보기  (0) 2019.02.10
문자열 사용하기 3  (0) 2019.02.05
문자열 사용하기 2  (0) 2019.02.03
문자열 사용하기  (0) 2018.11.27
반응형

2908번 문제 : 상수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
 
using namespace std;
 
int ReverseNum(int inp)
{
    int reversedNum = 0, leftover = 0;
    while (inp > 0)
    {
        leftover = inp % 10;
        reversedNum = reversedNum * 10 + leftover;
        inp /= 10;
    }
 
    return reversedNum;
}
 
int main() {
    int a,b;
    int rA, rB;
    cin >> a >> b;
 
    rA = ReverseNum(a);
    rB = ReverseNum(b);
 
    int result = (rA > rB) ? rA : rB;
    cout << result;
        
    return 0;
}
cs


5622번 문제 : 다이얼

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;
 
int main() {
    string inp;
    cin >> inp;
    int totalTime = 0;
 
    for (int i = 0; i < inp.length(); i++)
    {
        int temp = (int)inp[i] - 65;
 
        if (temp < 15) {
            temp /= 3;
            totalTime += (temp + 3);
        }
        else if (temp < 19) {
            totalTime += 8;
        }
        else if (temp < 22) {
            totalTime += 9;
        }
        else {
            totalTime += 10;
        }
    }
    cout << totalTime;
 
    return 0;
}
cs


2941번 문제 : 크로아티아 알파벳

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace std;
 
int main() {
    string inp;
    cin >> inp;
    
    string arr[8= {"c=""c-""dz=""d-""lj""nj""s=""z="};
    int counter = 0;
    
 
    for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
    {
        size_t pos = inp.find(arr[i], 0);
        while (pos != string::npos)
        {
            counter++;
            pos = inp.find(arr[i], pos + 1);
        }
    }
 
    cout << inp.size() - counter;
 
    return 0;
}
cs


메모: 

-배열의 길이는 size(), count(), length() 와 같은 함수로 구할 수 없음. (c++ 14기준)

 C 스타일의 sizeof(arr)/sizeof(arr[0] 로 구하거나 c++17 이상 부터 size(arr) 사용이 가능

-처음부터 container를 사용해야지 길이 구하는 함수를 사용할 수 있음






반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

정렬해보기  (0) 2019.02.10
규칙 찾기  (0) 2019.02.05
문자열 사용하기 2  (0) 2019.02.03
문자열 사용하기  (0) 2018.11.27
1차원 배열 사용하기  (0) 2018.11.27
반응형

1157번 문제 : 단어 공부

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int alphabet[26= { 0 };
    string str;
    cin >> str;
 
    for (int i = 0; i < str.length(); i++)
    {
        int ASCIInum = (int)str[i];
 
        //ASCII code 'A':65 ~ 'Z': 90      'a': 97 ~ 'z': 122
        if(ASCIInum>=65 && ASCIInum <=90)
            alphabet[(int)str[i] - 65++;
        else if(ASCIInum >= 97 && ASCIInum <= 122)
            alphabet[(int)str[i] - 97++;
    }
 
    int max = 0;
    int index = 0;
    bool isTheOnlyMax = true;
    for (int i = 0; i < 26; i++)
    {
        if (alphabet[i] > max)
        {
            max = alphabet[i];
            index = i;
        }
    }
 
    for (int i = 0; i < 26; i++)
    {
        if (index == i)
            continue;
 
        if (alphabet[i] == max)
        {
            isTheOnlyMax = false;
            break;
        }
    }
 
    if (isTheOnlyMax)
        cout << (char)(index + 65);
    else
        cout << "?";
 
    return 0;
}
cs


메모: 

-ASCII Table상에서 'Z' 다음은'a' 가 아님


1316번 문제 : 그룹 단어 체커

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int num = 0;
    int counter = 0;
    cin >> num;
    while (num > 0
    {
        string input;
        bool flag = false//used to break nested loop
        cin >> input;
 
        if (input.length() > 2)
        {
            for (int i = 1; i < input.length(); i++)
            {
                if (input[i - 1!= input[i])
                {
                    for (int k = 0; k < i; k++)
                    {
                        if (input[i] == input[k])
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }
 
                if (i == input.length() - 1)
                    counter++;
            }
        }
        else
        {
            counter++;
        }
 
        num--;
    }
 
    cout << counter;
 
    return 0;
}
cs


메모: 

-이중루프(nested loop)를 빠져나오기 위해 bool 형식의 flag를 사용하고 있음



1152번 문제 : 단어의 개수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
 
using namespace std;
 
int main() {
    string inp;
    getline(cin, inp);
    istringstream iss(inp);
    int counter = 0;
 
    string str;
    while (iss >> str)
    {
        counter++;
    }
    cout << counter << endl;
 
    return 0;
}
cs


메모: 

-C++string Split 할 수 있는 여러가지 방법들

http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html

https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/





반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

규칙 찾기  (0) 2019.02.05
문자열 사용하기 3  (0) 2019.02.05
문자열 사용하기  (0) 2018.11.27
1차원 배열 사용하기  (0) 2018.11.27
함수 사용하기  (0) 2018.11.18
반응형

11654번 문제 : 아스키 코드

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


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
 
using namespace std;
 
int main() {
    char c;
    cin >> c;
    cout << (int)c << endl;
 
    return 0;
}
cs


10809번 문제 : 알파벳 찾기

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int alphabet[26];
    for (int i = 0; i < 26; i++)
    {
        alphabet[i] = -1;
    }
 
    string inp;
    cin >> inp;
 
    for (int i = 0; i < inp.size(); i++)
    {
        if (alphabet[(int)inp[i] - 97== -1)
        {
            alphabet[(int)inp[i] - 97= i;
        }    
    }
 
    for (int i = 0; i < 26; i++)
    {
        cout << alphabet[i] << " ";
    }
 
    return 0;
}
 
cs


2675번 문제 : 문자열반복

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int num;
    cin >> num;
    while (num > 0)
    {
        int n;
        cin >> n;
        string str;
        cin >> str;
 
        string answer;
        for (int i = 0; i < str.size(); i++)
        {
            for (int j = 0; j < n; j++)
            {
                answer += str[i];
            }
        }
 
        cout << answer << endl;
 
        num--;
    }
 
    return 0;
}
cs


반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

문자열 사용하기 3  (0) 2019.02.05
문자열 사용하기 2  (0) 2019.02.03
1차원 배열 사용하기  (0) 2018.11.27
함수 사용하기  (0) 2018.11.18
if문 사용해보기  (0) 2018.11.14
반응형

1152번 문제 : 단어의 개수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
 
using namespace std;
 
int main() {
    string inp;
    getline(cin, inp);
    istringstream iss(inp);
    int counter = 0;
 
    string str;
    while (iss >> str)
    {
        counter++;
    }
    cout << counter << endl;
 
    return 0;
}
cs



2577번 문제 : 숫자의 개수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
int main() {
    int a[3];
    cin >> a[0>> a[1>> a[2];
    
    int multi = a[0* a[1* a[2];
    int num[10= {0};
    vector<int> v;
 
    while (multi > 0)
    {
        int temp = multi % 10;
        num[temp] ++;
        multi /= 10;
    }
 
    for (int i = 0; i < 10; i++)
    {
        cout << num[i] << endl;
    }
 
    return 0;
}
cs


8958번 문제 : OX퀴즈

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
int main() {
    int num;
    cin >> num;
    while (num > 0)
    {
        string str;
        cin >> str;
        int score = 0;
        int additionalScore = 1;
        for (int i = 0; i < str.length(); i++)
        {
            if (str[i] == 'O')
            {
                score += additionalScore;
                additionalScore++;
            }
            else if (str[i] == 'X')
            {
                additionalScore = 1;
            }
        }
        
        cout << score << endl;
 
        num--;
    }
 
    return 0;
}
cs


2920번 문제 : 음계

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
int main() {
    vector<int> v;
 
    for (int i = 0; i < 8; i++)
    {
        int inp;
        cin >> inp;
        v.push_back(inp);
    }
 
    int diff = v.at(0- v.at(1);
    string answer;
 
    for (int i = 0; i < v.size() - 1; i++)
    {
        int temp = v.at(i) - v.at(i + 1);
        if (temp == diff)
        {
            if (temp == 1)
            {
                answer = "descending";
            }
            else if (temp == -1)
            {
                answer = "ascending";
            }
        }
        else
        {
            answer = "mixed";
            break;
        }
    }
 
    cout << answer << endl;
 
    return 0;
}
cs


10039번 문제 : 평균 점수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
 
using namespace std;
 
int main() {
    int sum = 0;
 
    for (int i = 0; i < 5; i++)
    {
        int inp;
        cin >> inp;
 
        if (inp <= 40)
        {
            sum += 40;
        }
        else
        {
            sum += inp;
        }
    }
 
    int mean = sum / 5;
    cout << mean << endl;
 
    return 0;
}
cs


반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

문자열 사용하기 2  (0) 2019.02.03
문자열 사용하기  (0) 2018.11.27
함수 사용하기  (0) 2018.11.18
if문 사용해보기  (0) 2018.11.14
for문 사용해보기  (0) 2018.11.07
반응형

4673번 문제 : 셀프 넘버

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
 
using namespace std;
 
bool numArr[10001];
 
void SelfNumberCheck(int num)
{
    int sum = num;
    int temp = num;
 
    while (temp > 0)
    {
        sum += temp % 10;
        temp /= 10;
    }
 
    if (sum > 10000 || numArr[sum] == false)
        return;
 
    numArr[sum] = false;
    SelfNumberCheck(sum);
}
 
int main()
{
    for (int i = 1; i <= 10000; i++//initialize
    {
        numArr[i] = true;
    }
 
    for (int i = 1; i <= 10000; i++)
    {
        SelfNumberCheck(i);
    }
 
    for (int i = 1; i <= 10000; i++)
    {
        if (numArr[i] == true)
            cout << i << endl;
    }
 
    return 0;
}
cs


1065번 문제 : 한수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
using namespace std;
bool CheckIfHansoo(int n)
{
   if (n < 100)
   {
      return true;
   }
   vector<int> v;
   while (n > 0)
   {
      int num = n % 10;
      v.push_back(num);
      n /= 10;
   }
   
   int diff = v.at(0- v.at(1);
   for (int i = 0; i < v.size() - 1; i++)
   {
      int temp = v.at(i) - v.at(i + 1);
      if (diff != temp)
         return false;
   }
   return true;
}
int main()
{
   int inp;
   cin >> inp;
   int num = 0;
   for (int i = 1; i <= inp; i++)
   {
      if (CheckIfHansoo(i))
         num++;
   }
   cout << num;
   return 0;
}
cs


반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

문자열 사용하기  (0) 2018.11.27
1차원 배열 사용하기  (0) 2018.11.27
if문 사용해보기  (0) 2018.11.14
for문 사용해보기  (0) 2018.11.07
사칙연산 도전하기  (0) 2018.11.07
반응형

9498번 문제 : 시험 성적

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
 
using namespace std;
 
int main()
{
    int score;
    cin >> score;
 
    if(score<=100 && score>=90)
        cout<<"A"<<endl;
    else if((score<90 && score>=80))
        cout<<"B"<<endl;
    else if((score<80 && score>=70))
        cout<<"C"<<endl;
    else if((score<70 && score>=60))
        cout<<"D"<<endl;
    else
        cout<<"F"<<endl;
 
 
    return 0;
}
cs



10817번 문제 : 세 수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
 
    vector<int> v;
 
    v.push_back(a);
    v.push_back(b);
    v.push_back(c);
 
    sort(v.begin(), v.end());
 
    cout<< v.at(1)<<endl;
 
    return 0;
}
cs


메모:

-STL 사용하지 않고 단순 비교연산자로 풀수 있음: 세수이기때문에 속도면에서 더 빠를수도?


10871번 문제 : X보다 작은 수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
    int a,b;
    cin>>a>>b;
 
    vector<int> v;
 
    while(a>0)
    {
        int num;
        cin>>num;
        if(num<b)
            v.push_back(num);
        a--;
    }
 
    for(int i=0; i<v.size(); ++i)
    {
        cout<<v.at(i)<<" ";
    }
    
    return 0;
}
cs


메모:

-vector안에 값모두 출력할떄 auto 키워드사용해서 출력가능

1
2
3
4
for(auto i: v)
{
    cout<<i<<" ";
}
cs


1546번 문제 : 평균

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
    int a;
    cin>>a;
 
    vector<float> v;
 
    while(a>0)
    {
        float num;
        cin>>num;
        v.push_back(num);
        a--;
    }
    sort(v.begin(), v.end());
 
    for(int i=0; i<v.size(); i++)
    {
        v.at(i) = v.at(i) / v.at(v.size()-1* 100;
    }
 
    float sum = 0;
    
    for(int i=0; i<v.size(); i++)
    {
        sum += v.at(i);
    }
 
    float mean = sum / v.size();
    
    cout<< mean <<endl;
 
    return 0;
}
cs


4344번 문제 : 평균은 넘겠지 

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
 
using namespace std;
 
float GetMean(vector<float> v)
{
    float sum = 0;
    for (int i = 0; i<v.size(); i++)
        sum += v.at(i);
 
    float mean = sum / v.size();
    return mean;
}
 
 
int main()
{
    int a;
    cin >> a;
 
    vector<float> answer;
 
    while (a>0)
    {
        int b;
        cin >> b;
        vector<float> v;
        v.clear();
        while (b>0)
        {
            float num;
            cin >> num;
            v.push_back(num);
            b--;
        }
 
        int num = 0;
        float mean = GetMean(v);
        for (int i = 0; i<v.size(); i++)
        {
            if (v.at(i)> mean)
                num++;
        }
 
        answer.push_back((float)num / v.size() * 100);
 
        a--;
    }
 
    cout.setf(ios::fixed);
    cout.precision(3);
 
    for (int i = 0; i<answer.size(); i++)
    {
        float result = round(answer.at(i)*1000/ 1000;
        cout << result << "%" << endl;
    }
 
    return 0;
}
cs

메모:
-소수점 지정
1
2
    cout.setf(ios::fixed);
    cout.precision(3);
cs

-반올림의 경우는 <math.h>의 round()함수를 사용하여 구할 수 있음




1110번 문제 : 더하기 사이클

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
 
using namespace std;
 
int main()
{
    int initialNum;
    int counter = 0;
    cin >> initialNum;
 
    if (initialNum < 10)
    {
        initialNum = initialNum * 10;    
    }
 
    int temp = initialNum;
    int newNum1 = 0;
    int newNum2 = 0;
    int finalnum = 0;
 
    while (initialNum != finalnum )
    {
        newNum1 = temp / 10 + temp % 10;
        newNum1 = newNum1 % 10;
        newNum2 = temp % 10;
        finalnum = newNum2 * 10 + newNum1;
        temp = finalnum;
        counter++;
    }
 
    if (initialNum == 0)
        counter = 1;
 
    cout << counter << endl;
 
    return 0;
}
cs


메모:

- input값이 0일때 주의!

반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

1차원 배열 사용하기  (0) 2018.11.27
함수 사용하기  (0) 2018.11.18
for문 사용해보기  (0) 2018.11.07
사칙연산 도전하기  (0) 2018.11.07
입/출력 받아보기  (0) 2018.11.06
반응형

2741번 문제 : N찍기

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int i = 1; i <= num; i++)
    {
        cout << i;
        if (i < num)
            cout << "\n";
    }
 
    return 0;
}
cs


메모:

-"\n" 대신 endl 사용하면 시간초과발생 (속도 \n(더 빠름)> endl)

-만약 더 속도 빠르게 하려면 cout 대신 <cstdio>의 printf함수가 더빠름



2742번 문제 : 기찍 N

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int i = num; i >= 1; i--)
    {
        cout << i;
        if (i > 1)
            cout << "\n";
    }
 
    return 0;
}
cs



2739번 문제 : 구구단

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int i = 1; i < 10; i++)
    {
        cout << num << " * " << i << " = " << num * i;
        if (i < 9)
            cout << "\n";
    }
 
    return 0;
}
cs


2438번 문제 : 별 찍기 - 1

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int row = 0; row < num; row++)
    {
        for (int i = 0; i < row + 1; i++)
            cout << "*";
        if(row < num-1)
            cout << "\n";
    }
 
    return 0;
}
cs


2439번 문제 : 별 찍기 - 2

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int row = 0; row < num; row++)
    {
        for (int i = 0; i < num-row-1; i++)
            cout << " ";
 
        for (int i = 0; i < row + 1; i++)
            cout << "*";
 
        if(row < num-1)
            cout << "\n";
    }
 
    return 0;
}
cs


메모:

-푸는 방법은 아주 다양할듯...


2440번 문제 : 별 찍기 - 3

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int row = 0; row < num; row++)
    {
        for (int i = 0; i < num-row; i++)
            cout << "*";
 
        if(row < num-1)
            cout << "\n";
    }
 
    return 0;
}
cs


2441번 문제 : 별 찍기 - 4

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
 
int main(void) {
    int num;
    cin >> num;
 
    for (int row = 0; row < num; row++)
    {
        for (int i = 0; i < row; i++)
            cout << " ";
 
        for (int i = 0; i < num-row; i++)
            cout << "*";
 
        if(row < num-1)
            cout << "\n";
    }
 
    return 0;
}
cs


1924번 문제 : 2007년

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
 
int main(void) {
 
    string dayName[7= { "SUN" , "MON""TUE""WED""THU""FRI""SAT" };
    int month[13= { 0312831303130313130313031 };
    int totalDays = 0;
    int inputM, inputD;
 
    cin >> inputM >> inputD;
 
    for (int i = 0; i < inputM; i++)
        totalDays += month[i];
    totalDays += inputD;
    int leftover = totalDays % 7;
 
    cout << dayName[leftover];
 
    return 0;
}
cs


8393번 문제 : 합

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
 
int main(void) {
 
    int input;
    int sum = 0;
    cin >> input;
 
    for (int i = 1; i <= input; i++)
    {
        sum += i;
    }
    
    cout << sum;
 
    return 0;
}
cs


11720번 문제 : 숫자의 합

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
 
int main(void) {
 
    int sum = 0;
 
    int inputA;
    string inputB;
 
    cin >> inputA >> inputB;
 
    for (int i = 0; i < inputA; i++)
    {
        sum += inputB[i] - 48//Subtract 48 because of ASCII Code
    }
    
    cout << sum;
 
    return 0;
}
cs


메모:

-string의 각각 원소는 Char값인 동시에 int값을 가지고 있음. ASCII Code에 의해 0은 48부터 시작

-ASCII Code Table은 아래와 같음


Ascii Table


11721번 문제 : 열 개씩 끊어 출력하기

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
 
int main() {
    string line;
    cin >> line;
 
    for (int i = 0; i < line.size(); i++)
    {
        cout << line[i];
 
        if (i !=0 && i % 10 == 9)
            cout << endl;
    }
 
    return 0;
}
cs



15552번 문제 : 빠른 A+B

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdio>
using namespace std;
 
int main() {
    
    int num;
    scanf("%d"&num);
 
    while (num > 0)
    {
        int a, b;
        scanf("%d %d"&a, &b);
        printf("%d\n", a + b);
 
        num--;
    }
 
    return 0;
}
cs


메모: 

-cin과 cout보다 빠른 c언어 계열 함수인 scanf, printf 사용

-비쥬얼스튜디오는 scanf / printf 사용하면 컴파일 에러 발생, scanf_s / printf_s 사용해야함

-cin cout를 사용하고 싶으면 

1
2
3
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
cs
이런 방법이 있다고함... (하지만 C언어 계열 함수를 사용하는게 좋다고함)


반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

1차원 배열 사용하기  (0) 2018.11.27
함수 사용하기  (0) 2018.11.18
if문 사용해보기  (0) 2018.11.14
사칙연산 도전하기  (0) 2018.11.07
입/출력 받아보기  (0) 2018.11.06

+ Recent posts