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

 

11720번: 숫자의 합

첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.

www.acmicpc.net

 

최대 정수의 범위를 넘어가는 예제 3 때문에 문자열로 받아야 하는것이 포인트 

string이나 char*을 쓰면 가능하다.

#include <iostream>
#include <string>
using namespace std;

int main(void) {
    int nLen;
    string nNum;
    int nResult = 0;
    cin >> nLen >> nNum;
    
    for(auto it : nNum) {
        nResult += it - '0';
    }
    
    cout << nResult;
    return 0;
}

+ Recent posts