1023 Have Fun with Numbers

Posted CSU迦叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1023 Have Fun with Numbers相关的知识,希望对你有一定的参考价值。

考察大数乘法(整型是2)或者加法(两个相同的数字相加),然后将两个大数用到的0-9的个数比对。

进入比对前先判断长度是否相等,如不等,说明一定不是原序列。

一些需要注意的细节:

1. 字符串的字符转化为整数时不要忘记 减'0'

2. 封装的函数不要忘记返回值

3. 乘法不要忘记改变乘积的长度

AC代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>

using namespace std;
typedef long long LL;

const int maxn = 10007;
const int MOD = 1000000007;
const int INF = 1000000000;//INF:下确界  
const LL SUP = (1LL<<63)-1;//SUP:上确界 
const double eps = 1e-5;

struct bign{
	int d[1000];
	int len;
	
	bign(){
		memset(d,0,sizeof(d));
		len = 0;
	}
};

bign change(char s[]){
	bign bg;
	bg.len = strlen(s);
	for(int i=0;i<bg.len;i++){
		bg.d[i] = s[bg.len-1-i]-'0';//容易忘记减0 
	}
	return bg;//容易忘记返回 
}

bign doub(bign a){
	bign c;
	int carry = 0;
	int product;
	for(int i=0;i<a.len;i++){
		product = a.d[i]*2 + carry;
		c.d[c.len++] = product%10;//容易把下标写成i,这样c的长度不变 
		carry = product/10;
	}
	
	while(carry!=0){
		c.d[c.len++] = carry%10;
		carry /= 10;
	} 
	
	return c;//容易忘记返回 
}

int main(){
	
	char s[25];
	scanf("%s",s);
	bign a = change(s);
	bign b = doub(a);
	
	int a_count[10] = {0},b_count[10] = {0};//分别数大数a和b中0-9的个数

	//如果a和b长度都不等,显然不满足
	if(a.len!=b.len){
		printf("No\\n");
		for(int i=b.len-1;i>=0;i--){
			printf("%d",b.d[i]);
		}
		return 0;
	} 
	
	for(int i=0;i<a.len;i++){
		a_count[a.d[i]]++;
	}
	
	for(int i=0;i<b.len;i++){
		b_count[b.d[i]]++;
	}
	
	//比对a和b
	for(int i=0;i<10;i++){
		if(a_count[i]!=b_count[i]){
			printf("No\\n");
			for(int i=b.len-1;i>=0;i--){
				printf("%d",b.d[i]);
			}
			return 0;
		}
	} 
	
	printf("Yes\\n");
	for(int i=b.len-1;i>=0;i--){
		printf("%d",b.d[i]);
	}
	 
	 
	return 0;
}

以上是关于1023 Have Fun with Numbers的主要内容,如果未能解决你的问题,请参考以下文章