http://pat.zju.edu.cn/contests/pat-practise/1024

本文探讨了如何找到给定整数与其经过一系列操作后变为回文数所需的最小步骤数。通过实现算法,读者可以了解整数转换为回文数的过程及其背后的数学原理。
部署运行你感兴趣的模型镜像

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <algorithm>  
  4. #include <memory.h>  
  5. #include <cstdio>  
  6. #include <cstdlib>  
  7. #include <queue>  
  8. #include <vector>  
  9. using namespace std;  
  10. #define MAX 0Xfffffff  
  11. int c[1002];  
  12.   
  13. bool isTrue(string s){  
  14.     for(int i=0;i<s.length()/2;++i){  
  15.         if(s[i]!=s[s.length()-1-i])  
  16.             return false;  
  17.     }  
  18.     return true;  
  19. }  
  20. string add(string a, string b){  
  21.     int len = a.length()>b.length()?a.length():b.length();  
  22.     memset(c, 0, sizeof(c));  
  23.     int m, n;  
  24.     int k = 0;  
  25.     string res;  
  26.     for(int i=len-1;i>=0;--i){  
  27.         if(a.length()-1<i)  
  28.             m = 0;  
  29.         else   
  30.             m = a[i]-'0';  
  31.         if(b.length()-1<i)  
  32.             n = 0;  
  33.         else   
  34.             n = b[i]-'0';  
  35.         c[k++] = m + n;  
  36.     }  
  37.     for(int i=0;i<k;++i){  
  38.         c[i+1] += c[i]/10;  
  39.         c[i] = c[i]%10;  
  40.     }  
  41.     if(c[k]!=0)  
  42.         k++;  
  43.     for(int i=k-1;i>=0;i--)  
  44.         res += (c[i]+'0');  
  45.     return res;  
  46. }  
  47. string reversestr(string s){  
  48.     string str = s;  
  49.     for(int i=0;i<str.length()/2;++i){  
  50.         char c = str[i];  
  51.         str[i] = str[str.length()-1-i];  
  52.         str[str.length()-1-i] = c;  
  53.     }  
  54.     return str;  
  55. }  
  56. int main(){  
  57.   
  58.    //freopen("in.txt", "r", stdin);  
  59.   
  60.    string s;  
  61.    int k;  
  62.    cin>>s;  
  63.    cin>>k;  
  64.    int i;  
  65.     for(i=0;i<k;++i){  
  66.         if(isTrue(s)){  
  67.             cout<<s<<endl<<i<<endl;  
  68.             break;  
  69.         }else  
  70.             s = add(s, reversestr(s));  
  71.     }  
  72.     if(i>=k){  
  73.         cout<<s<<endl<<k<<endl;  
  74.     }  
  75.      
  76.    //fclose(stdin);  
  77. }     

1024. Palindromic Number (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

吃饱才能写好代码,哈哈。
注意要大数。

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

──(root㉿xcs)-[/home/xcs/桌面] └─# gpg --show-keys /etc/apt/trusted.gpg.d/kali-archive-keyring.gpg gpg: 目录‘/root/.gnupg’已创建 gpg: 钥匙箱‘/root/.gnupg/pubring.kbx’已创建 pub rsa4096 2025-04-17 [SC] [有效至:2028-04-17] 827C8569F2518CC677FECA1AED65462EC8D5E4C5 uid Kali Linux Archive Automatic Signing Key (2025) <devel@kali.org> ┌──(root㉿xcs)-[/home/xcs/桌面] └─# apt update 错误:1 http://mirrors.aliyun.com/kali kali-rolling InRelease 403 Forbidden [IP: 124.225.96.42 80] 命中:2 http://http.kali.org/kali kali-rolling InRelease 获取:3 http://http.kali.org/kali kali-rolling/main i386 Packages [20.4 MB] 获取:4 http://http.kali.org/kali kali-rolling/main i386 Contents (deb) [47.7 MB] 获取:5 http://http.kali.org/kali kali-rolling/contrib i386 Packages [97.1 kB] 获取:6 http://http.kali.org/kali kali-rolling/contrib i386 Contents (deb) [183 kB] 获取:7 http://http.kali.org/kali kali-rolling/non-free i386 Packages [147 kB] 获取:8 http://mirrors.neusoft.edu.cn/kali kali-rolling/non-free i386 Contents (deb) [859 kB] 错误: 无法下载 http://mirrors.aliyun.com/kali/dists/kali-rolling/InRelease 403 Forbidden [IP: 124.225.96.42 80] 错误: 仓库 “http://mirrors.aliyun.com/kali kali-rolling InRelease” 没有数字签名。 注意: 无法安全地用该源进行更新,所以默认禁用该源。 注意: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。 ┌──(root㉿xcs)-[/home/xcs/桌面] └─#
07-28
~ $ pkg update && pkg upgrade -y No mirror or mirror group selected. You might want to select one by running 'termux-change-repo' Testing the available mirrors: [*] (10) https://packages-cf.termux.dev/apt/termux-main: ok [*] (1) https://mirror.meowsmp.net/termux/termux-main: ok [*] (1) https://mirror.nevacloud.com/applications/termux/termux-main: ok [*] (1) https://mirrors.saswata.cc/termux/termux-main: ok [*] (1) https://tmx.xvx.my.id/apt/termux-main: ok [*] (1) https://mirrors.krnk.org/apt/termux/termux-main: ok [*] (1) https://termux.niranjan.co/termux-main: ok [*] (1) https://mirror.textcord.xyz/termux/termux-main: bad [*] (1) https://mirror.bardia.tech/termux/termux-main: bad [*] (1) https://mirror.twds.com.tw/termux/termux-main: ok [*] (1) https://mirror.albony.in/termux/termux-main: ok [*] (1) https://mirrors.cbrx.io/apt/termux/termux-main: ok [*] (1) https://mirror.jeonnam.school/termux/termux-main: ok [*] (1) https://mirror.rinarin.dev/termux/termux-main: ok [*] (1) https://mirror.freedif.org/termux/termux-main: ok [*] (1) https://linux.domainesia.com/applications/termux/termux-main: ok [*] (1) https://mirrors.ravidwivedi.in/termux/termux-main: ok [*] (1) https://mirrors.nguyenhoang.cloud/termux/termux-main: ok [*] (1) https://mirrors.in.sahilister.net/termux/termux-main/: bad [*] (1) https://mirrors.sdu.edu.cn/termux/termux-main: ok [*] (1) https://mirrors.hust.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.nju.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.cernet.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirror.nyist.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.cqupt.edu.cn/termux/termux-main: ok [*] (1) https://mirror.iscas.ac.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.sau.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.pku.edu.cn/termux/termux-main/: ok [*] (1) https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.bfsu.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.zju.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.sustech.edu.cn/termux/apt/termux-main: bad [*] (1) https://mirror.sjtu.edu.cn/termux/termux-main/: bad [*] (1) https://mirrors.aliyun.com/termux/termux-main: ok [*] (1) https://mirrors.ustc.edu.cn/termux/termux-main: ok [*] (1) https://packages.termux.dev/apt/termux-main: ok [*] (1) https://ro.mirror.flokinet.net/termux/termux-main: ok [*] (1) https://termux.cdn.lumito.net/termux-main: ok [*] (1) https://nl.mirror.flokinet.net/termux/termux-main: ok [*] (1) https://mirror.sunred.org/termux/termux-main: ok [*] (1) https://mirror.mwt.me/termux/main: ok [*] (1) https://ftp.fau.de/termux/termux-main: ok [*] (1) https://ftp.agdsn.de/termux/termux-main: ok [*] (1) https://mirrors.de.sahilister.net/termux/termux-main: ok [*] (4) https://grimler.se/termux/termux-main: ok [*] (1) https://mirror.polido.pt/termux/termux-main: bad [*] (1) https://termux.3san.dev/termux/termux-main: ok [*] (1) https://mirrors.medzik.dev/termux/termux-main: ok [*] (1) https://termux.mentality.rip/termux-main: ok [*] (1) https://mirror.leitecastro.com/termux/termux-main: ok [*] (1) https://termux.librehat.com/apt/termux-main: ok [*] (1) https://md.mirrors.hacktegic.com/termux/termux-main: ok [*] (1) https://mirrors.cfe.re/termux/termux-main: bad [*] (1) https://mirror.accum.se/mirror/termux.dev/termux-main: ok [*] (1) https://mirror.autkin.net/termux/termux-main: ok [*] (1) https://mirror.bouwhuis.network/termux/termux-main: ok [*] (1) https://is.mirror.flokinet.net/termux/termux-main: ok [*] (1) https://mirror.termux.dev/termux-main: bad [*] (1) https://plug-mirror.rcac.purdue.edu/termux/termux-main: bad [*] (1) https://mirrors.utermux.dev/termux/termux-main: ok [*] (1) https://dl.kcubeterm.com/termux-main: bad [*] (1) https://termux.danyael.xyz/termux/termux-main: ok [*] (1) https://gnlug.org/pub/termux/termux-main: ok [*] (1) https://mirror.vern.cc/termux/termux-main: bad [*] (1) https://mirror.csclub.uwaterloo.ca/termux/termux-main: ok [*] (1) https://mirror.mwt.me/termux/main: ok [*] (1) https://mirror.quantum5.ca/termux/termux-main: ok [*] (1) https://mirror.fcix.net/termux/termux-main: ok [*] (1) https://mirrors.middlendian.com/termux/termux-main: ok [*] (1) http://mirror.mephi.ru/termux/termux-main: ok [*] (1) https://repository.su/termux/termux-main/: bad Picking mirror: (24) /data/data/com.termux/files/usr/etc/termux/mirrors/asia/mirrors.nguyenhoang.cloud Get:1 https://mirrors.nguyenhoang.cloud/termux/termux-main stable InRelease [14.0 kB] Get:3 https://mirrors.nguyenhoang.cloud/termux/termux-main stable/main aarch64 Packages [527 kB] Get:2 https://turdl.kcubeterm.com tur-packages InRelease [18.4 kB] Get:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages [72.4 kB] Ign:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Get:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages [72.4 kB] Ign:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Get:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages [72.4 kB] Ign:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Ign:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Err:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Connection timed out [IP: 2606:50c0:8001::153 443] Get:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages [223 kB] Fetched 783 kB in 3min 18s (3950 B/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done 5 packages can be upgraded. Run 'apt list --upgradable' to see them. No mirror or mirror group selected. You might want to select one by running 'termux-change-repo' Checking availability of current mirror: [*] https://mirrors.nguyenhoang.cloud/termux/termux-main: ok Hit:2 https://mirrors.nguyenhoang.cloud/termux/termux-main stable InRelease Hit:1 https://turdl.kcubeterm.com tur-packages InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done 5 packages can be upgraded. Run 'apt list --upgradable' to see them. Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: bash command-not-found libsqlite readline termux-keyring 5 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 2025 kB of archives. After this operation, 1323 kB of additional disk space will be used. Get:1 https://mirrors.nguyenhoang.cloud/termux/termux-main stable/main aarch64 readline aarch64 8.3 [294 kB] Get:2 https://mirrors.nguyenhoang.cloud/termux/termux-main stable/main aarch64 bash aarch64 5.3 [956 kB] Get:3 https://mirrors.nguyenhoang.cloud/termux/termux-main stable/main aarch64 termux-keyring all 3.13 [39.8 kB] Get:4 https://mirrors.nguyenhoang.cloud/termux/termux-main stable/main aarch64 command-not-found aarch64 3.1-2 [101 kB] Get:5 https://mirrors.nguyenhoang.cloud/termux/termux-main stable/main aarch64 libsqlite aarch64 3.50.2 [635 kB] Fetched 2025 kB in 1min 20s (25.5 kB/s) (Reading database ... 18441 files and directories currently installed.) Preparing to unpack .../readline_8.3_aarch64.deb ... Unpacking readline (8.3) over (8.2.13) ... Setting up readline (8.3) ... (Reading database ... 18446 files and directories currently installed.) Preparing to unpack .../archives/bash_5.3_aarch64.deb ... Unpacking bash (5.3) over (5.2.37-2) ... Setting up bash (5.3) ... Installing new version of config file /data/data/com.termux/files/usr/etc/bash.bashrc ... (Reading database ... 18453 files and directories currently installed.) Preparing to unpack .../termux-keyring_3.13_all.deb ... Unpacking termux-keyring (3.13) over (3.12-1) ... Setting up termux-keyring (3.13) ... (Reading database ... 18456 files and directories currently installed.) Preparing to unpack .../command-not-found_3.1-2_aarch64.deb ... Unpacking command-not-found (3.1-2) over (3.1-1) ... Preparing to unpack .../libsqlite_3.50.2_aarch64.deb ... Unpacking libsqlite (3.50.2) over (3.50.1-1) ... Setting up libsqlite (3.50.2) ... Setting up command-not-found (3.1-2) ... ~ $ pkg update && pkg upgrade -y No mirror or mirror group selected. You might want to select one by running 'termux-change-repo' Checking availability of current mirror: [*] https://mirrors.nguyenhoang.cloud/termux/termux-main: bad Testing the available mirrors: [*] (10) https://packages-cf.termux.dev/apt/termux-main: bad [*] (1) https://mirror.meowsmp.net/termux/termux-main: bad [*] (1) https://mirror.nevacloud.com/applications/termux/termux-main: bad [*] (1) https://mirrors.saswata.cc/termux/termux-main: bad [*] (1) https://tmx.xvx.my.id/apt/termux-main: bad [*] (1) https://mirrors.krnk.org/apt/termux/termux-main: bad [*] (1) https://termux.niranjan.co/termux-main: bad [*] (1) https://mirror.textcord.xyz/termux/termux-main: bad [*] (1) https://mirror.bardia.tech/termux/termux-main: bad [*] (1) https://mirror.twds.com.tw/termux/termux-main: bad [*] (1) https://mirror.albony.in/termux/termux-main: ok [*] (1) https://mirrors.cbrx.io/apt/termux/termux-main: ok [*] (1) https://mirror.jeonnam.school/termux/termux-main: ok [*] (1) https://mirror.rinarin.dev/termux/termux-main: ok [*] (1) https://mirror.freedif.org/termux/termux-main: ok [*] (1) https://linux.domainesia.com/applications/termux/termux-main: ok [*] (1) https://mirrors.ravidwivedi.in/termux/termux-main: ok [*] (1) https://mirrors.nguyenhoang.cloud/termux/termux-main: ok [*] (1) https://mirrors.in.sahilister.net/termux/termux-main/: ok [*] (1) https://mirrors.sdu.edu.cn/termux/termux-main: ok [*] (1) https://mirrors.hust.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.nju.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.cernet.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirror.nyist.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.cqupt.edu.cn/termux/termux-main: ok [*] (1) https://mirror.iscas.ac.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.sau.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.pku.edu.cn/termux/termux-main/: ok [*] (1) https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.bfsu.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.zju.edu.cn/termux/apt/termux-main: ok [*] (1) https://mirrors.sustech.edu.cn/termux/apt/termux-main: bad [*] (1) https://mirror.sjtu.edu.cn/termux/termux-main/: bad [*] (1) https://mirrors.aliyun.com/termux/termux-main: ok [*] (1) https://mirrors.ustc.edu.cn/termux/termux-main: ok [*] (1) https://packages.termux.dev/apt/termux-main: ok [*] (1) https://ro.mirror.flokinet.net/termux/termux-main: ok [*] (1) https://termux.cdn.lumito.net/termux-main: ok [*] (1) https://nl.mirror.flokinet.net/termux/termux-main: ok [*] (1) https://mirror.sunred.org/termux/termux-main: ok [*] (1) https://mirror.mwt.me/termux/main: ok [*] (1) https://ftp.fau.de/termux/termux-main: ok [*] (1) https://ftp.agdsn.de/termux/termux-main: ok [*] (1) https://mirrors.de.sahilister.net/termux/termux-main: ok [*] (4) https://grimler.se/termux/termux-main: ok [*] (1) https://mirror.polido.pt/termux/termux-main: bad [*] (1) https://termux.3san.dev/termux/termux-main: ok [*] (1) https://mirrors.medzik.dev/termux/termux-main: ok [*] (1) https://termux.mentality.rip/termux-main: ok [*] (1) https://mirror.leitecastro.com/termux/termux-main: ok [*] (1) https://termux.librehat.com/apt/termux-main: ok [*] (1) https://md.mirrors.hacktegic.com/termux/termux-main: ok [*] (1) https://mirrors.cfe.re/termux/termux-main: bad [*] (1) https://mirror.accum.se/mirror/termux.dev/termux-main: ok [*] (1) https://mirror.autkin.net/termux/termux-main: ok [*] (1) https://mirror.bouwhuis.network/termux/termux-main: ok [*] (1) https://is.mirror.flokinet.net/termux/termux-main: ok [*] (1) https://mirror.termux.dev/termux-main: bad [*] (1) https://plug-mirror.rcac.purdue.edu/termux/termux-main: bad [*] (1) https://mirrors.utermux.dev/termux/termux-main: ok [*] (1) https://dl.kcubeterm.com/termux-main: bad [*] (1) https://termux.danyael.xyz/termux/termux-main: ok [*] (1) https://gnlug.org/pub/termux/termux-main: ok [*] (1) https://mirror.vern.cc/termux/termux-main: bad [*] (1) https://mirror.csclub.uwaterloo.ca/termux/termux-main: ok [*] (1) https://mirror.mwt.me/termux/main: ok [*] (1) https://mirror.quantum5.ca/termux/termux-main: ok [*] (1) https://mirror.fcix.net/termux/termux-main: ok [*] (1) https://mirrors.middlendian.com/termux/termux-main: ok [*] (1) http://mirror.mephi.ru/termux/termux-main: ok [*] (1) https://repository.su/termux/termux-main/: ok Picking mirror: (40) /data/data/com.termux/files/usr/etc/termux/mirrors/europe/termux.librehat.com Get:2 https://termux.librehat.com/apt/termux-main stable InRelease [14.0 kB] Get:3 https://termux.librehat.com/apt/termux-main stable/main aarch64 Packages [527 kB] Get:1 https://turdl.kcubeterm.com tur-packages InRelease [18.4 kB] Ign:1 https://turdl.kcubeterm.com tur-packages InRelease Ign:1 https://turdl.kcubeterm.com tur-packages InRelease Get:1 https://turdl.kcubeterm.com tur-packages InRelease [18.4 kB] Get:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages [72.4 kB] Ign:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Ign:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages Get:4 https://turdl.kcubeterm.com tur-packages/tur aarch64 Packages [72.4 kB] Fetched 582 kB in 4min 16s (2276 B/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date. No mirror or mirror group selected. You might want to select one by running 'termux-change-repo' Checking availability of current mirror: [*] https://termux.librehat.com/apt/termux-main: ok Hit:1 https://termux.librehat.com/apt/termux-main stable InRelease Hit:2 https://turdl.kcubeterm.com tur-packages InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date. Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. ~ $
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值