浙江大学PAT_甲级_1024. Palindromic Number (25)
2015-06-15 21:31
537 查看
题目链接:点击打开链接
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
回文数
python程序:
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
回文数
python程序:
def check(str): left=int(0) right=len(str)-1 while left<=right: if str[left]!=str[right]: return 0 left=left+1 right=right-1; return 1 a=[int(i) for i in raw_input().split()] n=str(a[0]) k=int(a[1]) temp_int=0 count_k=0 for i in range(1,k+1): if check(n): break; temp_str=n[::-1] temp_int=int(n)+int(temp_str) n=str(temp_int) count_k=count_k+1 print n print count_k
相关文章推荐
- POJ之路11-1789 Truck History
- Android中高效的显示图片之三——缓存图片
- winform程序多行textbox滚动条移动当前光标位置
- 现代汽车电子的问题和未来趋势
- Windows下Scrapy安装
- mini2440 一线触摸配置
- 3d之ui快速切换图像
- Linux grep参数详解
- Android中高效的显示图片之二——在非UI线程中处理图片
- hdu 2041 整理下水题 递推动规
- [BI项目记]-新任务处理
- Linux-Unix环境高级编程(第三版)代码编译
- MatLab之HDL coder
- QQ互发消息
- PyCharm简单使用介绍及注意事项
- Program received signal SIGSEGV, Segmentation fault.(转)
- 34Exchange 2010升级到Exchange 2013-先觉条件及架构扩展
- android学习笔记(三)基础UI组件1——按钮,文本框,CheckBox,Radiobutton介绍
- PyCharm简单使用介绍及注意事项
- oracle创建用户 和 给用户授权