甲级 1024-Palindromic Number (大数加法)

题目:

1024 Palindromic Number (25 分)

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

题意:在限定次数内,将一个不大于10的10次方的数,与其反转的数相加,是否能变为一个回文数。
这里用long long也不行,因为他是不断地跟自身累加,比如67 + 76 = 143 , 143 + 341 = 484 这样10的十次方这样累加100次肯定会超过long long的范围。
这里给一下int,unsigned long,long long的范围:
  int : 2乘10的10次方内
  unsigned long : 4乘10的10次方内
  long long:9乘10的19次方
  unsigned long long:10的20次方

所以要用大数加法(字符串加法)

AC代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<sstream>
#include<algorithm>
#include<iomanip>
#include<map>
#include<cstring>
#include<set>
#define INF 0x3f3f3f3f

using namespace std;
//PTA 1024 大数加法
typedef long long ll;

bool judgePalindromic(const string &numStr){
    for(int i=0;i<(int)numStr.size();i++)
        if(numStr[i]!=numStr[(int)numStr.size()-i-1])
            return false;
    return true;
}

string Reverse(const string &num){
    string newStr = num;
    reverse(newStr.begin(),newStr.end());
    return newStr;
}

string Add(string a,string b){
    string result;
    int posA,posB;
    posA = a.size() - 1;
    posB = b.size() - 1;
    int more = false;
    while(posA>=0 && posB>=0){
        int curNum,numA,numB;
        numA = a[posA]-'0';
        numB = b[posB]-'0';
        posA--;
        posB--;
        curNum = numA+numB;
        if(more){
            curNum++;
            more = false;
        }
        if(curNum/10!=0)
            more = true;
        curNum%=10;
        result.push_back(curNum+'0');
        if(more && (posA<0 || posB<0))
            result.push_back('1');
    }
    while(posA>=0)
        result.push_back(a[posA--]);
    while(posB>=0)
        result.push_back(b[posB--]);
    result = Reverse(result);
    return result;
}

int main(){
     //freopen("in.txt","r",stdin);
     string num;
     int times,Count = 0;
     cin>>num>>times;
     while(!judgePalindromic(num)){
        if(Count>=times)
            break;
        Count++;
        string reverseNum = Reverse(num);
        num = Add(num,reverseNum);
     }
     cout<<num<<endl<<Count<<endl;
     return 0;
}

 

 
posted @ 2019-03-07 00:06  巴特曼  阅读(372)  评论(0)    收藏  举报