1024. Palindromic Number (25)
来源:互联网 发布:炒白银实时数据 编辑:程序博客网 时间:2024/05/24 04:34
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 <stdlib.h>#include <string.h>#define MAX 100void Reverse(char a[]){int lenth;int i,j,t;lenth = strlen(a);j = lenth-1;i = 0;while(i<j){t = a[i];a[i] = a[j];a[j] = t;i++;j--;}}void Add(char a[],char b[]){int i, j, k;int carry;char c[MAX];i = strlen(a)-1;j = strlen(b)-1;k = 0;carry = 0;while(i>=0 || j>=0){c[k] = 0;if(i>=0){c[k] += a[i] - '0';}if(j>=0){c[k] += b[j] - '0';}c[k] += carry;carry = c[k]/10;c[k] = c[k]%10;c[k] += '0';k++;i--;j--;}if(carry){c[k] = '1';k++;}c[k] = 0;Reverse(c);strcpy(a,c);}int IsPal(char a[]){int lenth;int i,j;lenth = strlen(a);j = lenth-1;i = 0;while(i<j){if(a[i] != a[j]){break;}i++;j--;}if(i<j){return 0;}return 1;}int main(){char a[MAX],b[MAX];int k,i;scanf("%s %d",a,&k);i = 0;while(!IsPal(a)){strcpy(b,a);Reverse(b);Add(a,b);i++;if(i == k){break;}}printf("%s\n%d",a,i);//system("pause");return 0;}
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- spring 管理的事务。个人理解(SSH项目注解配置)
- 第二周项目2-程序的多文件组织
- 两种排序算法的运行时间
- 项目3-2 体验复杂度 汉诺塔问题
- CSS基础复习笔记
- 1024. Palindromic Number (25)
- CCF 201604-1 折点计数
- unbuntu安装eclipse编写c程序出现launch failed.Binary not found
- hadoop MapReduce程序中解决第三方jar包问题
- boa:Hello CGI
- android开发环境安装(Eclipse)出现的几个问题
- LeetCode 11. Container With Most Water
- Spawn-FCGI安装
- Express框架详解