1024 Palindromic Number int_string转换 大整数相加

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 (≤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 after K steps, just output the number obtained at the Kth step and K instead.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 100015;

bool isPalindromic(string v){
    int l=0, r=v.length()-1;
    while(l<r){
        if(v[l]!=v[r]) return false;
        l++; r--;
    }
    return true;
}

string Add(string n,string rn){
    long a=stod(n);
    long b=stod(rn);
    return to_string(a+b);
}

int main(){
    string n;
    int k;
    cin>>n;
    scanf("%d",&k);
    if(isPalindromic(n)){
        cout<<n<<endl;
        printf("0");
        return 0;
    }
    int t=0;
    while(t<k){
        t++;
        string rn=n;
        reverse(rn.begin(), rn.end());
        n=Add(n,rn);
        if(isPalindromic(n)){
            cout<<n<<endl;
            printf("%d",t);
            return 0;
        }
    }
    cout<<n<<endl;
    printf("%d",t);
}

原文地址:https://www.cnblogs.com/lokwongho/p/10013047.html

时间: 2024-10-28 00:34:08

1024 Palindromic Number int_string转换 大整数相加的相关文章

HDU 1002 A + B Problem II(大整数相加)

A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input c

大整数相加问题

#include <iostream> #include <string> /*project:两个大整数相加 **@author:浅滩 **data:2019.05.15 */ using namespace std; void add(const string &,const string &); int main() {int n; string str1,str2; cin>>str1>>str2; add(str1,str2); }

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

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 n

算法---大整数相加

原文:算法---大整数相加 开通博客开始第一次写发表算法博客.深知一半算法考试都是用C,C++,由于大四开始到今年毕业工作到现在一直从事C#开发,C++用得很少了.链表,指针也只知道一个概念了.用得没以前熟练了.所以后续更新的算法题我都是基于C#语法的.算法主要体现的是解题思路.跟题目一样,本次算法主要实现大数据相加. 解题思路: 1. 将大数据存储到一个链表中,C#中用List<int>来存储,每个节点表示每一位的数字. {1,2,3,4,5} =>12345 和{9,6,5,9,5}

实现大整数相加(考虑符号位,可能有负整数) 思维严谨程度!!

//实现大整数相加 //还得考虑符号位,一个比另一个短 #include<iostream> #include <string> #include <cstring> using namespace std; #define maxlen 2001 int a[maxlen]; int b[maxlen]; int len1, len2, i, j; int bigger(int a, int b) { return a>b ? a : b; } void Add

SOJ 1002/1003/1004 大整数相加/相乘/相除

三个题目分别考察大整数相加相乘相除运算.如果按照传统算法是取一个长数组,之后进行模拟或者FFT来进行运算.但是相对繁琐. 后来昨天的青岛区域赛网赛1001,用到了JAVA的BigDecimal,于是反过来想到了这几个题目.用JAVA写了以后果然很简单. 1002:大数相加: AC代码: import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { // TO

ACM: A + B Problem II (两个大整数相加)

Code: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 1000 //给数组赋值 void arrV(int a[],int len,int p){ int i; for(i=0;i<len;i++){ a[i]=p; } } //打印数组中的运算结果 void printRverse(int a[]){ int len=0,i=0; while(a[i]!=-1){

[Java]#从头学Java# Java大整数相加

重操旧业,再温Java,写了个大整数相乘先回顾回顾基本知识.算法.效率什么的都没怎么考虑,就纯粹实现功能而已. 先上代码: 1 package com.tacyeh.common; 2 3 public class MyMath { 4 5 public static String BigNumSum(String... n) { 6 int length = n.length; 7 StringBuilder result = new StringBuilder(); 8 //这里判断其实不需

poj2305-Basic remains(进制转换 + 大整数取模)

进制转换 + 大整数取模一,题意: 在b进制下,求p%m,再装换成b进制输出. 其中p为b进制大数1000位以内,m为b进制数9位以内二,思路: 1,以字符串的形式输入p,m; 2,转换:字符串->整数 十进制->b进制; 3,十进制下计算并将整形结果转换成字符串形式,并倒序储存; 4,输出.三,步骤: 1,输入p[],m[]; 2,字符串->整形 + 进制->b进制: i,进制转换语句:m2 = m2*b + m[j]-'0'; ii,大整数取模,大整数可以写成这样的形式: 12