1748번 - 수 이어쓰기 1
그냥 재귀함수를 통해서 답을 구했다.
go 함수는
1~9 까지는 각 한 자리씩 해서 더하고,
10~99 까지는 각 두자리씩 해서 더하고,
100~999까지는 각 세자리씩 해서 더하고..
이런식으로 문제를 해결했다.
<정답 코드>
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> using namespace std; int ans = 0; void go(int a,int b,int c,int n) { if (n < a) { ans += c*(n - b + 1); return; } if (n >= a) { ans += c*(a - b); a *= 10; b *= 10; go(a, b, c + 1, n); } } int main() { int n; scanf("%d", &n); go(10,1,1,n); printf("%d\n", ans); return 0; } | cs |
반응형