• 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;
    } 
    
  • 相关阅读:
    ASP的URL重写技术 IIS的ISAPI
    关于如何去勾引百度谷歌的蜘蛛爬虫
    如何让百度和google的蜘蛛爬虫迅速爬过来
    本机不安装Oracle客户端,使用PL/SQL Developer和 Instant Client 工具包连接oracle 11g远程数据库
    Java小知识点总结
    Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)
    OracleDBconsoleorcl无法启动解决方案
    Java中PreparedStatement的错误使用
    Oracle 中行列转换问题总结
    Java中 Interger 的自动装箱
  • 原文地址:https://www.cnblogs.com/houzm/p/12269159.html
Copyright © 2020-2023  润新知