zoukankan      html  css  js  c++  java
  • 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 (<= 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.

    Sample Input 1:

    67 3
    

    Sample Output 1:

    484
    2
    

    Sample Input 2:

    69 3
    

    Sample Output 2:

    1353
    3


    用用long类型运行的时候,会有两个测试点不能通过, 用string来进行运算
     1 #include<iostream>
     2 using namespace std;
     3 int main(){
     4   long long n, k, i, copy, m;
     5   cin>>n>>k;
     6   for(i=0; i<k; i++){
     7     copy=n; m=0;
     8     while(n){
     9       m = m*10+(n%10);
    10       n/=10;
    11     }
    12     if(m==copy){
    13       cout<<m<<endl<<i;
    14       break;
    15     }
    16     n = m+copy;
    17     if(i==k-1) cout<<n<<endl<<k;
    18   }
    19   return 0;
    20 }
     1 #include<iostream>
     2 #include<string>
     3 #include<algorithm>
     4 using namespace std;
     5 string add(string s, string t){
     6   int len = s.size(), carry=0;
     7   for(int i=len-1; i>=0; i--){
     8     s[i] = s[i]+t[i]-'0'+carry;
     9     if(s[i]>'9') {carry=1; s[i]=char(s[i]-10);}
    10     else carry=0;
    11   }
    12   if(carry==1) s='1'+s;
    13   return s;
    14 }
    15 int main(){
    16   string s;
    17   int k, i;
    18   cin>>s>>k;
    19   for(i=0; i<k; i++){
    20     string t = s;
    21     reverse(t.begin(), t.end());
    22     if(t==s) {cout<<t<<endl<<i; break;}
    23     s = add(s, t);
    24     if(i==k-1) cout<<s<<endl<<k;
    25   }
    26   return 0;
    27 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    c/c++ linux 进程间通信系列7,使用pthread mutex
    c/c++ linux 进程间通信系列6,使用消息队列(message queue)
    c/c++ linux 进程间通信系列5,使用信号量
    eos 创建两对的公钥和私钥, 钱包,交易所转账到主网,主网到交易所
    c/c++ linux 进程间通信系列4,使用共享内存
    python基础-网络编程part02
    idea新建项目相关名词意义
    idea中当前模块资源目录上显示属于其他模块
    centos下安装rabbitmq
    JAVA中值传递,引用传递
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9155048.html
Copyright © 2011-2022 走看看