1024. Palindromic Number (25)

1.判断一个数字与其位数翻转后相加是否回文,需要在限定的步数之内。

2.使用string存储数字。

3.用朴素的方法检测回。

4.需要考虑进位,和首位为0的情况(字符串翻转相加,为了避免长度不等,不需要去掉前面的‘0’)。

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

bool isPalindromic(string s)
{
	for (int i = 0; i < s.size() / 2; i++)
	{
		if (s[i] != s[s.size() - 1 - i])
			return false;
	}
	return true;
}
//string reverse(string s)
//{
//	string t = "";
//	int i = 0;
//	for (; i < s.size(); i++)
//	{//去掉开头的0
//		if (s[i] != '0') break;
//	}
//
//	for (; i < s.size(); i++)
//	{
//		t += s[i];
//	}
//	return t;
//}

int main(void)
{
	string s;//以string保存数字
	int k;
	cin >> s >> k;
	int step = 0;
	string t = s;
	while (step < k&&!isPalindromic(t))
	{
		s = t;
		t = "";
		for (int i = 0; i < s.size(); i++)
		{//t为s的翻转
			t = s[i] + t;
		}

		int carry = 0;
		string tmp = "";
		for (int i = s.size() - 1; i >= 0; i--)
		{//进行累加
			int digit = (s[i] - '0') + (t[i] - '0') + carry;
			carry = digit / 10;
			char c = digit % 10 + '0';
			tmp = c + tmp;
		}
		if (carry != 0)
		{//如果有进位,则需要添加
			char c = carry + '0';
			tmp = c + tmp;
		}
		int i = 0;
		for (; i < tmp.size(); i++)
		{//去掉前面的0
			if (tmp[i] != '0') break;
		}
		t = "";
		for (; i < tmp.size(); i++)
			t += tmp[i];//生成最终结果
		step++;
	}
	cout << t << endl << step << endl;

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注