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 (≤) is the initial numer and K (≤) 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

    题目分析:大数加法 把是否进1记录就好了
     1 #include<iostream>
     2 #include<vector>
     3 #include<queue>
     4 #include<stack>
     5 #include<algorithm>
     6 #include<string>
     7 using namespace std;
     8 string Add(string s1, string s2)
     9 {
    10     int flag = 0;
    11     int len = s1.length();
    12     for (int i = len - 1; i >= 0; i--){
    13         int temp = flag+s1[i] - '0' + s2[i] - '0';
    14         if (temp > 9){
    15             s1[i] = (temp % 10) + '0';
    16             flag = 1;
    17         }
    18         else {
    19             s1[i] = temp + '0';
    20             flag = 0;
    21         }
    22     }
    23     if (flag)return '1' + s1;
    24     else
    25         return s1;
    26 }
    27 int main()
    28 {
    29     string N;
    30     int K;
    31     cin >> N >> K;
    32     int i = 0;
    33     for (; i < K; i++){
    34         string s =N;
    35         reverse(s.begin(), s.end());
    36         if (s == N)
    37             break;
    38         else
    39             N = Add(N, s);
    40     }
    41     cout << N << endl << i;
    42 }
    View Code
  • 相关阅读:
    ubuntu16.04服务自启动(弹控制台)
    第十集 爱在天路唐古拉,青藏梦止长江源
    第九集 生死穿越风火山,感受尘世间轮回
    第八集 昆仑初度尘未洗,夜宿禁区五道梁
    第七集 久历风尘凡间路,终见西域玉珠峰
    第六集 飞沙走石共患难,夜扎昆仑饮圣泉
    第五集 插肩而过茶卡湖,朝发夕至格尔木
    辞职之后在家的挣扎与老爸的工作
    2018,怎么这么难,我该怎么办,我的ai和感情
    17年11月兄弟小聚
  • 原文地址:https://www.cnblogs.com/57one/p/11966933.html
Copyright © 2011-2022 走看看