인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

qkrwndnjs06064794's profile image
qkrwndnjs06064794

asked

Introduction to Algorithm Problem Solving for IT Employment (with C/C++): Coding Test Preparation

30. How many 3's? (large: time limit 1 second)

[30번 3의 개수는?(large)] Case #04만 Wrong_answer가 나옵니다.

Written on

·

491

0

강사님의 풀이 개념을 바탕으로 string을 사용해 풀어봤습니다.

제 코드에서는 Case #04만 정답보다 1이 작게 나와 Wrong_answer가 나오는데, 원인을 모르겠습니다...

코드는 아래와 같습니다. 잘못된 부분을 바로 잡아주시면 정말 감사드립니다ㅠㅠ

(들여쓰기가 자동으로 없어지네요...)

int main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL);

freopen("input.txt", "rt", stdin);

int pre, now, next, digit, cnt = 0;

string n;

cin >> n;

for (int i = 0; i < n.size(); i++) {

digit = pow(10, n.size() - 1 - i);

if (i != 0) {

pre = stoi(n.substr(0, i));

cnt += pre * digit;

}

now = n[i] - '0';

if (i != n.size() - 1) {

if (now >= 4) cnt += digit;

else if (now == 3) {

next = stoi(n.substr(i + 1));

cnt += next;

}

}

else {

if (now >= 3) cnt += digit;

}

}

cout << cnt;

return 0;

}

30번코테 준비 같이 해요! 3의개수C++

Answer 2

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

코테 실전까지 마무리 잘 해서 꼭 목표하시는바를 이루시기 바랍니다.

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

now==3일때 잘 생각해보세요.

아 0부터 세지니까 +1를 해야했네요! 코멘트 감사합니다!

이번에 올리신 코테 실전 강의 바로 구매했어요.

계속 좋은 강의 만들어주세요 :)

qkrwndnjs06064794's profile image
qkrwndnjs06064794

asked

Ask a question