팰린드롬 문제를 살짝 응용한 문제이다.
중간중간에 수 연산과정에 생기는 오류를 조심하자
팰린드롬을 간단하게 설명하면
숫자가 좌우 대칭인 숫자를 말한다.
예를들어 121 1221 10001등 좌우대칭인 수를 의미한다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <math.h>
#include <queue>
#include <set>
#include <utility>
#include <functional>
#define MAX 100005
#define INF 987654321
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;
int n;
char str[10];
int fun(int cur)
{
int tmp=0;
if(str[cur-1]>=str[strlen(str)-cur])
tmp=str[cur-1]-str[strlen(str)-cur];
// 앞의 검사하려는자리가 뒤의 검사하려는 자리보다 큰경우.
else
{
int times=1;
while(1)
{
if(str[strlen(str)-cur-times]=='9')
{
str[strlen(str)-cur-times]='0';
times++;
}
else
{
str[strlen(str)-cur-times]+=1;
break;
}
}
tmp=str[cur-1]-str[strlen(str)-cur]+10;
// 올림 된 크기를 tmp에 저장해주고 자리수 연산을 실시해준다.
}
str[strlen(str)-cur]=str[cur-1];
if(cur==strlen(str)/2)
return tmp*(int)pow(10,cur-1);
// 6자리(짝수)일때 3번 5자리(홀수)일대 2번검사 해준다.
return tmp*(int)pow(10,cur-1)+fun(cur+1);
// 재귀로 계속해서 더해준다.
}
int main()
{
scanf("%s",str);
while(!(strlen(str)==1&&str[0]=='0'))
// 0이 아닌경우에 반복시킨다.
{
printf("%d\n",fun(1));
scanf("%s",str);
}
return 0;
}'IT > BOJ' 카테고리의 다른 글
| 백준(BOJ) 2470번 두 용액 ** (0) | 2018.06.01 |
|---|---|
| 백준(BOJ) 10610 30 * (0) | 2018.06.01 |
| 백준(BOJ) 14502번 연구소 * (0) | 2018.06.01 |
| 백준(BOJ) 2931 가스관 ** (0) | 2018.06.01 |
| 백준(BOJ) 1865 웜홀 ** (0) | 2018.06.01 |