|
60鱼币
这是题目,简单来说就是求一个数乘2之后,还是不是原数的数字构成。
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 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
然后这是我的代码(陷入死循环出不来了,怎么办啊,想了一晚上,不知道哪里错了)
#include<stdio.h>
int main(void)
{
long long int num1,num2,num3;
int n1[20],n2[20];
int i,j,flag = 1;
scanf("%lld",&num1);
num2 = num1 * 2;
num3 = num2;
for(i = 0;i < 19;i++)
{
n1[i] = num1 % 10;
num1 = num1 / 10;
}
for(j = 0;j < 19;j++)
{
n2[j] = num2 % 10;
num2 = num2 / 10;
}
for(i = 0;i < 20;i++)
{
for(j = 0;j < 20;j++)
{
if(n1[i] == n2[j])
{
n1[i] = -1;
n2[j] = -1;
break;
}
else if(j = 19 && n1[i] != n2[j])
{
flag = 0;
}
}
}
if(flag == 0)
{
printf("No\n");
}
else if(flag == 1)
{
printf("Yes\n");
}
printf("%lld",num3);
return 0;
}
不是多大的悬赏的问题!你的问题主要在于:欠了不少的帖子没有结帖,别人回答了你的问题,又不要你付出什么,举手之劳,选个最佳答案这么简单的事,你都不愿意去做。换作是你,付出了得不到回报,你会怎么想?
算了,不跟你计较了,来说说你的程序,没有大问题,只有一处,不细心敲漏了一个键,看代码:
- for(i = 0;i < 20;i++)
- {
- for(j = 0;j < 20;j++)
- {
- if(n1[i] == n2[j])
- {
- n1[i] = -1;
- n2[j] = -1;
- break;
- }
- else if(j = 19 && n1[i] != n2[j]) // 你不断地给 j 赋值 :j = 19,毫无疑问是错误的,应该是:j == 19 吧?
- {
- flag = 0;
- }
- }
-
- }
复制代码
|
最佳答案
查看完整内容
不是多大的悬赏的问题!你的问题主要在于:欠了不少的帖子没有结帖,别人回答了你的问题,又不要你付出什么,举手之劳,选个最佳答案这么简单的事,你都不愿意去做。换作是你,付出了得不到回报,你会怎么想?
算了,不跟你计较了,来说说你的程序,没有大问题,只有一处,不细心敲漏了一个键,看代码:
|