起步能力自测4 HaveFun With Numbers

题目:

Notice that the number 123456789 is a 9-digit number consistingexactly 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. 二十位长度的数字,超出了整形,长整型,以及无符号长整形的表示范围。因此,考虑用数组来表示数字。重新定义函数Multiple作为double的函数。
  2. 无论是Yes还是No,都需要输出double后的数字。
#include<stdio.h>

int main() {
	int i;
	char r;
	int a[21], b[21], c[21 ],acount = 0, bcount = 0;
	bool Istrue(int a[], int b[],int n,int m);
	void sort(int a[], int n);
	int multiple(int a[], int b[], int n);
	void reverse(int a[], int n);
	i = 0;
	r = getchar();   //将给出的数字存入数组a中
	while (r != '\n') {
		a[i] = r-'0'; 
		r = getchar();
		i++;
	}
	acount = i; //acount存储a的位数,在比较两数组是否相同时用到
	reverse(a, acount);    //疯狂逆置加倍的过程
	i = 0;
	bcount=multiple(a, b,acount);  
	for (i = 0; i < bcount; i++) {   //a,c数组冒泡排序,一次比较是否相等
		c[i] = b[i]; 
	}
	sort(a, acount);
	sort(c, bcount);
	if (Istrue(a, c,acount,bcount)) {
		printf("Yes\n");
	}
	else {
		printf("No");
	}
	reverse(b, bcount);  //逆置后的结果为最终结果
	for (i = 0; i <= bcount - 1; i++) printf("%d", b[i]);
	return 0;
}

void reverse(int a[], int n) {  //逆置函数
	int temp;
	for (int i = 0; i < n-1-i; i++) {
		temp = a[i];
		a[i] = a[n - 1 - i];
		a[n - 1 - i] = temp;
	}
}

int multiple(int a[], int b[],int n) {  //加倍函数
	int ci = 0,i;
	for (i = 0; i < n; i++) {
		b[i] = (a[i] * 2)%10+ci;
		ci = a[i] * 2 / 10;
	}
	if ((a[n - 1] * 2) > 9) {
		b[n] = ci;
		i++;
	}
	return i;
}

bool Istrue(int a[], int b[],int n, int m) { //判别函数
	if (n != m) return false;
	for (int i = 0; i <= m - 1; i++)
		if (a[i] != b[i]) return false;
	return true;
}

void sort(int a[],int n) {  //排序函数
	int r;
	for (int i = 0; i < n-1; i++){
		for (int j = 0; j < n - 1;j++) {
			if (a[j] < a[j + 1]) {
				r = a[j];
				a[j] = a[j + 1];
				a[j + 1] = r;
			}
		}
	}
}

惨不忍睹。
存在的问题:

  1. 排序后再进行比较,麻烦,
  2. 要求double后的值,需要将a数组逆置,加倍后的结果再逆置才是最终结果,麻烦。
    ··········除了能通过测试点外全是问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值