본문 바로가기

USACO/Bronze

백준 5949 Adding Commas (USACO December 2010 Bronze 3번)

문제 링크: https://www.acmicpc.net/problem/5949

 

5949번: Adding Commas

Bessie is working with large numbers N (1 <= N <= 2,000,000,000) like 153920529 and realizes that the numbers would be a lot easier to read with commas inserted every three digits (as is normally done in the USA; some countries prefer to use periods every

www.acmicpc.net

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
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    string s, ans;
    cin >> s;
    int sz = s.size();
    if(sz > 9) {
        s.insert(1",");
        s.insert(5",");
        s.insert(9",");
    }
    else if(sz > 6) {
        s.insert(sz - 6",");
        s.insert(sz - 2",");
    }
    else if(sz > 3) {
        s.insert(sz - 3",");
    }
 
    cout << s;
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    string s;
    cin >> s;
    int n = s.size();
 
    for(int i = 0; i < n; i++) {
        if(i > 0 && (n - i) % 3 == 0cout << ',';
        cout << s[i];
    }
    cout << '\n';
    
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter