自测04——Have Fun with Numbers
2017-11-04 23:12
274 查看
题目:
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.
//错误版,主要问题:1.输入问题——当时char数组的时候,不知道怎样停止输入;2.输入数字太大,无法用int保存,想用char数组,但是乘2不行。
//后来看了下面的链接,发现c++中cin>>a(数组名)直接就有断行处理……
#include<iostream>
using namespace std;
#define N 20
int main(){
float a,b,c;
cin >> a;
c = a;
b = a * 2;
cout << a;
//存入数组
int ori
, add
;
int i;
for ( i = 0; c > 0; i++){
ori[i] = int(c - c / 10 * 10);
c = c / 10;
cout << ori[i]<<endl;
}
int j;
for (j = 0, c = b; c > 0; j++){
add[j] = int(c - c / 10 * 10);
c = (int)c / 10;
}
//判断
int count = 0;
if (i < j){
count = 1;
}
else{
int flag;
for (int k = 0; k < i; k++){
flag = 0;
for (int g = 0; g < i; g++){
if (add[k] == ori[g]){
ori[g] = 0;//去掉已经用掉过得数字
flag = 1;
break;
}
}
if (flag != 1){
count = 1;
break;
}
}
}
if (count == 1){
cout << "No" << endl;
}
else
cout << "Yes" << endl;
for (int k = i - 1; k >= 0; k--){
cout << add[k];
}
cout << endl;
system("pause");
return 0;
}
//正确版
//这是我看了别人做的,略有不同——看了主要方法以后自己写了,再看他的方法,觉得他的比较简单。主要问题就是:自己没有开另外空间来使方法更加简便吧(空间换时间)附上链接:http://www.cnblogs.com/wuxiaotianC/p/6368310.html
#include<iostream>
#include<string.h>
using namespace std;
#define MAXC 22
int main()
{
char a[MAXC];
int change[MAXC];
cin >> a;
int temp ,jin,yu;int length = strlen(a);
jin = 0;
for (int i = length - 1; i >= 0; i--){
//1234567899
temp = a[i] - '0';
yu = temp * 2 + jin;
jin = yu/ 10;
change[i] = yu % 10;
}
if (jin){
cout << "No" << endl<<jin;
}
else{
int flag, count = 1;
for (int i = 0; i < length; i++){
flag = 0;
for (int k = 0; k < length; k++){
if (change[i] == a[k] - '0'){
a[k] = '0';
flag = 1;
break;
}
}
if (!flag){
count = 0;
break;
}
}
if (!count)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
for (int i = 0; i < length; i++){
cout << change[i];
}
cout << endl;
system("pause");
return 0;
}
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
//错误版,主要问题:1.输入问题——当时char数组的时候,不知道怎样停止输入;2.输入数字太大,无法用int保存,想用char数组,但是乘2不行。
//后来看了下面的链接,发现c++中cin>>a(数组名)直接就有断行处理……
#include<iostream>
using namespace std;
#define N 20
int main(){
float a,b,c;
cin >> a;
c = a;
b = a * 2;
cout << a;
//存入数组
int ori
, add
;
int i;
for ( i = 0; c > 0; i++){
ori[i] = int(c - c / 10 * 10);
c = c / 10;
cout << ori[i]<<endl;
}
int j;
for (j = 0, c = b; c > 0; j++){
add[j] = int(c - c / 10 * 10);
c = (int)c / 10;
}
//判断
int count = 0;
if (i < j){
count = 1;
}
else{
int flag;
for (int k = 0; k < i; k++){
flag = 0;
for (int g = 0; g < i; g++){
if (add[k] == ori[g]){
ori[g] = 0;//去掉已经用掉过得数字
flag = 1;
break;
}
}
if (flag != 1){
count = 1;
break;
}
}
}
if (count == 1){
cout << "No" << endl;
}
else
cout << "Yes" << endl;
for (int k = i - 1; k >= 0; k--){
cout << add[k];
}
cout << endl;
system("pause");
return 0;
}
//正确版
//这是我看了别人做的,略有不同——看了主要方法以后自己写了,再看他的方法,觉得他的比较简单。主要问题就是:自己没有开另外空间来使方法更加简便吧(空间换时间)附上链接:http://www.cnblogs.com/wuxiaotianC/p/6368310.html
#include<iostream>
#include<string.h>
using namespace std;
#define MAXC 22
int main()
{
char a[MAXC];
int change[MAXC];
cin >> a;
int temp ,jin,yu;int length = strlen(a);
jin = 0;
for (int i = length - 1; i >= 0; i--){
//1234567899
temp = a[i] - '0';
yu = temp * 2 + jin;
jin = yu/ 10;
change[i] = yu % 10;
}
if (jin){
cout << "No" << endl<<jin;
}
else{
int flag, count = 1;
for (int i = 0; i < length; i++){
flag = 0;
for (int k = 0; k < length; k++){
if (change[i] == a[k] - '0'){
a[k] = '0';
flag = 1;
break;
}
}
if (!flag){
count = 0;
break;
}
}
if (!count)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
for (int i = 0; i < length; i++){
cout << change[i];
}
cout << endl;
system("pause");
return 0;
}
相关文章推荐
- 00-自测4. Have Fun with Numbers (20)
- 00-自测4 Have Fun with Numbers (20)
- PTA自测-4 Have Fun with Numbers
- 00-自测4. Have Fun with Numbers (20)
- 00-自测4. Have Fun with Numbers (20)
- 00-自测4. Have Fun with Numbers (20)
- 数据结构练习 00-自测4. Have Fun with Numbers (20)
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测4. Have Fun with Numbers (20) 【二星级】
- 自测4. Have Fun with Numbers
- 00-自测4. Have Fun with Numbers (20)
- PAT00-自测4. Have Fun with Numbers (20)
- 00-自测4. Have Fun with Numbers (20)
- 自测-4 Have Fun with Numbers
- 数据结构起步能力自测4-have fun with numbers
- 自测4. Have Fun with Numbers
- 00-自测4. Have Fun with Numbers (20)
- 00-自测4. Have Fun with Numbers (20)
- 00-自测4. Have Fun with Numbers (20)
- 00-自测4. Have Fun with Numbers (20)
- PTA-自测-4 Have Fun with Numbers