pat 1024
来源:互联网 发布:sql rownum用法 编辑:程序博客网 时间:2024/05/17 04:59
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 (<= 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 3Sample Output 1:
4842Sample Input 2:
69 3Sample Output 2:
13533
#include <stdio.h>#include <string.h>char str[50], strRev[50], ans[50];int k;bool judge(){int len = strlen(str), i, j;for(i = 0, j = len - 1; i <= j; i++, j--)if(str[i] != str[j])return false;return true;}void swap(char &a, char &b){char tmp = b;b = a;a = tmp;}int main(){int i, j, l, h;while(scanf("%s %d", str, &k) != EOF){bool flag = false;for(i = 1; i <= k; i++){if(judge()){flag = true;printf("%s\n", str);printf("%d\n", i-1);break;}else{int len = strlen(str);for(l = 0, j = len - 1; j >= 0; l++, j--)strRev[l] = str[j];strRev[l] = 0;int carry = 0;for(l = len - 1, j = 0; l >= 0; l--, j++){//将该数与该数的翻转数相加ans[j] = str[l] - '0' + strRev[l] - '0' + carry + '0';carry = (ans[j] - '0') / 10;ans[j] = (ans[j] - '0') % 10 + '0';}if(carry > 0)ans[j++] = carry + '0';ans[j] = 0;for(l = 0, h = j - 1; l <= h; l++, h--){swap(ans[l], ans[h]);}strcpy(str, ans);}}if(flag == false){printf("%s\n", str);printf("%d\n", k);}}return 0;}
- pat 1024
- pat 1024
- pat 1024
- PAT 1024
- pat 1024
- PAT 1024
- PAT 1024
- PAT(A) 1024
- pat 1024 Palindromic Number
- PAT(甲级)1024
- 1024,打印沙漏,PAT
- 浙大pat甲级 1024
- PAT乙级1024
- pat-bl-1024
- PAT甲级1024
- pat乙级1024
- PAT甲级 1024
- PAT basic 1024
- 全国统一公车改革即将启动 一般公务用车将取消
- 为easyui组件datagrid在加载完之后追加新的数据行
- ...编码在os.listdir
- C++编程问题--注意指针参数的传递
- c# LINQ查询操作符实例操作总结3
- pat 1024
- C语言中的位运算
- 卷积神经网络(CNN)
- maven jar 包配置
- MTK功能机编译相关问题
- ARM Linux 3.x的设备树(Device Tree)
- 体验TinyXML读写XML文件数据
- 编程(C#)实现创建快捷方式文件
- 可变数组的数据添加