Have Fun with Numbers (大数)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input file contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

Sample Input:

1234567899

Sample Output:

Yes

2469135798

  1 #include <iostream>
  2
  3 #include <string>
  4
  5 #include <algorithm>
  6
  7 using namespace std;
  8
  9
 10
 11 int aa[21];
 12
 13 int v1[10];
 14
 15 int v2[10];
 16
 17
 18
 19 int main()
 20
 21 {
 22
 23
 24
 25       string  ss;
 26
 27     while(cin>>ss)
 28
 29       {
 30
 31
 32
 33           int i;
 34
 35             for(i=0;i<21;i++)
 36
 37             {
 38
 39                 aa[i]=0;
 40
 41             }
 42
 43             for(i=0;i<10;i++)
 44
 45             {
 46
 47               v1[i]=0;
 48
 49               v1[i]=0;
 50
 51             }
 52
 53             int count=0;
 54
 55             for(i=ss.length()-1;i>=0;i--)
 56
 57             {
 58
 59                aa[count++]=ss[i]-‘0‘;
 60
 61                v1[ss[i]-‘0‘]=1;
 62
 63             }
 64
 65
 66
 67             for(i=0;i<count;i++)
 68
 69             {
 70
 71                 aa[i]=aa[i]*2;
 72
 73             }
 74
 75
 76
 77           int tem,len;
 78
 79         for(i=0;i<count;i++)
 80
 81             {
 82
 83                 if(aa[i]>9)
 84
 85                   {
 86
 87                      tem=aa[i]/10;
 88
 89                      aa[i+1]=aa[i+1]+tem;
 90
 91                      aa[i]=aa[i]%10;
 92
 93                   }
 94
 95             }
 96
 97
 98
 99             if(aa[count]==0) len=count;
100
101             else len=count+1;
102
103
104
105
106
107         reverse(aa,aa+len);
108
109
110
111         for(i=0;i<len;i++)
112
113         {
114
115            v2[aa[i]]=1;
116
117         }
118
119       bool ifis=true;
120
121         for(i=0;i<10;i++)
122
123         {
124
125            if(v1[i]!=v2[i]) ifis=false;
126
127         }
128
129
130
131         if(ifis) cout<<"Yes"<<endl;
132
133         else  cout<<"No"<<endl;
134
135
136
137         for(i=0;i<len;i++)
138
139         {
140
141            cout<<aa[i];
142
143         }
144
145         cout<<endl;
146
147
148
149       }
150
151       return 0;
152
153 }

时间: 2024-10-01 06:53:28

Have Fun with Numbers (大数)的相关文章

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5,对所有位的和来判断3. 代码就不粘了.

UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

题目链接:Uva 11582 [vjudge] 题意 输入两个非负整数a.b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,当中f(0) = 0,f(1) =  1.且对随意非负整数i.f(i+2)= f(i+1)+f(i). 分析 全部的计算都是对n取模.设F(i) =f(i)mod n, 非常easy发现,F(x)是具有周期性的,由于对N取模的值最多也就N个,当二元组(F(i-1),F(i))反复的时候.整个序列也就反复了.周期i

HDU - 3117 Fibonacci Numbers 矩阵快速幂 + 取大数前4位

题目大意:要求输出第n个fibonacci数,如果该数超过1e9,就输出该数的前4位和后四位 解题思路:通过打表可得,第40个fibonacci数是大于1e9的,所以40之前的可以直接计算 40之后的比较麻烦,参考了别人的题解 http://blog.sina.com.cn/s/blog_9bf748f301019q3t.html #include<cstdio> #include<cmath> using namespace std; typedef long long ll;

【Scala】Scala之Numbers

一.前言 前面已经学习了Scala中的String,接着学习Scala的Numbers. 二.Numbers 在Scala中,所有的数字类型,如Byte,Char,Double,Float,Int,Long,Short都是对象,这七种数字类型继承AnyVal特质,这七种数字类型与其在Java中有相同的范围,而Unit和Boolean则被认为是非数字值类型,Boolean有false和true两个值,你可以获取到各个数字类型的最值. 复杂的数字和日期 如果需要更强大的数类,可以使用spire,sc

HDU 1018 大数(求N!的位数/相加)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35382    Accepted Submission(s): 16888 Problem Description In many applications very large integers numbers are required. Some of these

ACM学习历程—HDU 3092 Least common multiple(数论 &amp;&amp; 动态规划 &amp;&amp; 大数)

hihoCoder挑战赛12 Description Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers

HDU 2424-Gary&#39;s Calculator(表达式计算+大数)

Gary's Calculator Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 950    Accepted Submission(s): 209 Problem Description Gary has finally decided to find a calculator to avoid making simple cal

hdu2054 A == B ? (大数)

A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 91202    Accepted Submission(s): 14548 Problem Description Give you two numbers A and B, if A is equal to B, you should print "YES"