zoukankan      html  css  js  c++  java
  • A1024. Palindromic Number

    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 (<= 1010) 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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<math.h>
     5 #include<string.h>
     6 using namespace std;
     7 char str[21];
     8 typedef struct info{
     9     int num[100];
    10     int len;
    11     info(){
    12         for(int i = 0; i < 100; i++)
    13             num[i] = 0;
    14         len = 0;
    15     }
    16 }bign;
    17 void reverse(bign &a, bign &b){
    18     b.len = 0;
    19     for(int i = a.len - 1; i >= 0; i--){
    20         b.num[b.len++] = a.num[i];
    21     }
    22 }
    23 bign add(bign a, bign b){
    24     bign c;
    25     int carry = 0;
    26     for(int i = 0; i < a.len || i < b.len; i++){
    27         int temp = carry + a.num[i] + b.num[i];
    28         c.num[c.len++] = temp % 10;
    29         carry = temp / 10;
    30     }
    31     if(carry != 0){
    32         c.num[c.len++] = carry;
    33     }
    34     return c;
    35 }
    36 int isPal(bign a){
    37     for(int i = 0, j = a.len - 1; i <= j; i++, j--){
    38         if(a.num[i] != a.num[j])
    39             return 0;
    40     }
    41     return 1;
    42 }
    43 bign a, b, c;
    44 int main(){
    45     int K;
    46     scanf("%s %d", str, &K);
    47     for(int i = strlen(str) - 1; i >= 0; i--){
    48         a.num[a.len++] = str[i] - '0';
    49     }
    50     int find = 0;
    51     if(isPal(a)){
    52         find = 1;
    53         for(int j = a.len - 1; j >= 0; j--){
    54             printf("%d", a.num[j]);
    55         }
    56         printf("
    %d", 0);
    57     }
    58     for(int i = 0; find == 0 && i < K; i++){
    59         reverse(a, b);
    60         a = add(a, b);
    61         if(isPal(a)){
    62             find = 1;
    63             for(int j = a.len - 1; j >= 0; j--){
    64                 printf("%d", a.num[j]);
    65             }
    66             printf("
    %d", i + 1);
    67             break;
    68         }
    69     }
    70     if(find == 0){
    71         for(int j = a.len - 1; j >= 0; j--){
    72             printf("%d", a.num[j]);
    73         }
    74         printf("
    %d", K);
    75     }
    76     cin >> K;
    77     return 0;
    78 }
    View Code

    总结:

    1、仍然是大整数加法,注意有可能给出的数不用做处理就是对称数,这是应输出这个数,次数位0。

  • 相关阅读:
    openmediavault 5.5.23 安装插件失败的解决方案
    qt下载地址
    qt 5.12 增加 mysql驱动
    选基金标准
    关注几个基金
    调仓的几个问题
    要读的书
    ubuntu 20.04 LTS 安装webmin
    set的常见用法
    斜率优化dp([HNOI2008]玩具装箱)
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8522627.html
Copyright © 2011-2022 走看看