Palindromic Number (25)
来源:互联网 发布:淘宝手机详情图尺寸 编辑:程序博客网 时间:2024/06/02 06:47
题目描述
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.
输入描述:
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.
输出描述:
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.
输入例子:
67 3
输出例子:
4842我的代码:
#include<iostream>#include<string.h>#include<string>#include<sstream>using namespace std;int judge(char a[]){ int n,k=0; n=strlen(a); while(n--) { if(a[k++]!=a[n]) return 0; } return 1;}string cir(char a[]){ stringstream ss; int n,k=0; string c; char b[1001]; n=strlen(a); while(n--) b[k++]=a[n]; b[k]='\0'; ss<<b; ss>>c; return c;}int main(){ char a[1001]; int n,i,r=0,p; string v; scanf("%s %d",a,&n); p=n; while(n--) { r++; stringstream ss,s; char b[1001],d[2001]; int m,flag=0,j=0,c[2001]={0}; m=strlen(a); ss<<cir(a); ss>>b; for(i=0;i<m;i++) { c[i]=c[i]+(a[i]-'0')+(b[i]-'0'); if(c[i]>=10) { c[i]=c[i]-10; c[i+1]++; } } for(i=2001;i>=0;i--) { if(c[i]!=0) flag=1; if(flag==1) d[j++]=c[i]+'0'; } d[j]='\0'; if(judge(d)==1) { s<<d; s>>v; cout<<v<<endl; break; } else strcpy(a,d); if(r==p) { s<<a; s>>v; cout<<v<<endl; } } cout<<r<<endl; return 0;}
提交结果:
阅读全文
0 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)
- 【Android笔记】Android 7.0 FileUriExposedException完美解决
- Linux 命令集锦
- JavaEE——HttpServletRequest对象
- 出口商贸易融资工具:福费廷
- html如何公用布局模块
- Palindromic Number (25)
- 热部署插件 JRebel 安装激活及使用
- 文本编辑工具Vim及三种模式用法
- Linux下live555的编译
- 转:PHP中防止SQL注入的方法
- android webview 真正实现---保存整个网页源码
- NOIP模拟(11.07)T3 图
- 2017模拟赛 现代艺术(二维差分)
- 【技术教程】MySQL to SequoiaDB数据迁移