zoukankan      html  css  js  c++  java
  • PAT Advanced 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 nonpalindromic 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 (<= 10^10) 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 afer 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

    题目分析

    给出一个整数N(<=10^10),最大操作次数K(<=100),若整数不为回文数,进行如下操作(1. 反转N得到FN;2.N=FN+N),循环执行,直到操作次数达到K次或者N成为回文数
    N若为最大值1010操作100次大于1020,超过long long类型范围,需要看做大整数处理数据(string)

    解题思路

    思路1

    1. 判断N是否为回文数,如果不是回文数,进入循环操作,直到操作次数达到k或者N成为回文数
    2. 大整数处理,需要逆置字符串,因为操作从数字的个位开始(但是a,b互为反转,所以本题中只需要反转一个就可以)

    思路2

    高精度加法

    Code

    Code 01

    #include <iostream>
    #include <cmath>
    #include <string>
    #include <algorithm>
    using namespace std;
    bool isPac(string & s) {
    	for(int i=0; i<s.length(); i++) {
    		if(i>=s.length()-1-i)break;
    		if(s[i]!=s[s.length()-1-i])return false;
    	}
    	return true;
    }
    string bignadd(string a) {
    	// b是a的反转 长度一定相等,无需考虑不相等的情况
    	string b = a; 
    	reverse(b.begin(),b.end());
    	int carry=0;
    	string c;
    	for(int i=0; i<a.length(); i++) {
    		int tempa = a[i]-'0',tempb = b[i]-'0';
    		int temp = tempa+tempb+carry;
    		c.push_back(temp%10+'0');
    		carry=temp/10;
    	}
    	if(carry==1)c.push_back('1');
    	reverse(c.begin(),c.end());
    	return c;
    }
    int main(int argc,char *argv[]) {
    	string n,temp;
    	int m;
    	cin>>n>>m;
    	temp = n;
    	int i=1;
    	for(i=1; i<=m&&!isPac(temp); i++) {
    		temp = bignadd(temp);
    	}
    	printf("%s
    ",temp.c_str());
    	printf("%d",i-1);
    	return 0;
    }
    

    Code 02(高精度加法)

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    /*
    	高精度加法 
    	
    */
    struct bign{
    	int d[1000];
    	int len;
    	bign(){
    		memset(d,0,sizeof(d));
    		len = 0;
    	}
    }; 
    bign change(char str[]){
    	bign a;
    	a.len=strlen(str);
    	for(int i=0;i<a.len;i++){
    		a.d[i]=str[a.len-1-i]-'0';
    	}
    	return a; 
    }
    bign add(bign a,bign b){
    	bign c;
    	int r = 0;
    	for(int i=0;i<a.len;i++){
    		int temp = a.d[i]+b.d[i]+r;
    		c.d[c.len++]=temp%10;
    		r = temp/10;
    	}
    	if(r!=0)c.d[c.len++]=r;
    	return c;
    }
    bool judge(bign a){
    	for(int i=0;i<=a.len/2;i++){
    		if(a.d[i]!=a.d[a.len-1-i]){
    			return false;
    		}
    	}
    	return true;
    }
    void print(bign a){
    	for(int i=a.len-1;i>=0;i--){
    		printf("%d",a.d[i]);
    	}
    	printf("
    ");
    }
    int main(int argc,char * argv[]){
    	char s[1000];
    	int	k,t=0;
    	scanf("%s %d",s,&k);
    	bign a = change(s);
    	while(!judge(a)&&t<k){
    		bign b = a;
    		// reverse
    		reverse(b.d,b.d+b.len);
    		// s = s+r
    		a = add(a,b);
    		t++;
    	}
    	print(a);
    	printf("%d
    ",t);
    	return  0;
    } 
    
  • 相关阅读:
    stm32 fatfs 文件系统分析和代码解析
    STM32 USB协议和代码分析
    微型跟踪器A产品体验和分析
    辅听一号产品体验和测评
    华为sound x智能音箱初体验
    TPC-H 分析
    论文解析 -- TPC-H Analyzed: Hidden Messages and Lessons Learned from an Influential Benchmark
    Calcite分析 -- Cost
    Calcite分析 -- ConverterRule
    Calcite分析 -- TopDownRuleDriver
  • 原文地址:https://www.cnblogs.com/houzm/p/12269159.html
Copyright © 2011-2022 走看看