반응형
#include <iostream>
#include <algorithm>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char ch[10];
using namespace std;
int main() {
int ans = 0;
scanf("%s",&ch);
for(int i=0; ch[i]!='\0'; i++){
if(ch[i]=='C'){
if(ch[i+1]=='H'){
ans+=12;
continue;
}
else{
int tmp =0;
for(int j=i+1; ch[j]!='H'; j++){
tmp = tmp*10 + (ch[j]-'0');
}
ans+=tmp*12;
}
}
else if(ch[i]=='H'){
if(ch[i+1]=='\0'){
ans++;
break;
}
else{
int tmp =0;
for(int j=i+1; ch[j]!='\0'; j++){
tmp = tmp*10 + (ch[j]-'0');
}
ans+=tmp;
}
}
}
printf("%d",ans);
return 0;
}
for(int j=i+1; ch[j]!='H'; j++){
tmp = tmp*10 + (ch[j]-'0');
}
tmp 처럼 하면
예를 들어
"1234" 인 문자열이 있으면
tmp = 1
tmp = 10 + 2
tmp = 12*10 + 3
tmp = 123*10 + 4 = 1234
이런 식으로 별도 모듈 필요 없이
계산으로 숫자를 추출해낼 수 있다.
반응형
'Algorithm > etc' 카테고리의 다른 글
연속된 자연수의 합 (0) | 2022.01.07 |
---|---|
LRU( Least Recently Used) cpp 구현 (0) | 2022.01.06 |
3의 개수 (0) | 2021.12.27 |
N! 0의 개수 (0) | 2021.12.27 |
N! 표현법 (0) | 2021.12.27 |