PAT-1023 Have Fun with Numbers (20)

本文提供了一种解决 PAT A 1023 问题的方法,该问题要求判断一个数乘以2后的各位数字是否仅在位置上发生变化。通过使用简单的字符串操作和排序技巧来实现解决方案。

题目大意:把给定数 N 乘以2,判断结果 M 中的各位数是不是与 N 中的各位数只是在位置顺序上不同。

解题思路:简单大数乘法。

题目链接:https://www.patest.cn/contests/pat-a-practise/1023

#include <iostream>              
#include <algorithm>              
#include <set>              
#include <map>              
#include <vector>              
#include <stack>              
#include <queue>              
#include <cmath>   
#include <cstring>           
using namespace std;

int main(int argc, char** argv) {
	char a[22],b[22],c[22];
	scanf("%s",a);
	int len = strlen(a);
	int push = 0;//进位 
	int cnt = 0;
	for(int i=len-1;i>=0;--i)
	{
		int x = (a[i]-'0')*2;
		if(x+push >= 10)
		{	
			b[cnt++] = (x+push) - 10 +'0'; 
			push = 1;
		}
		else
		{		
			b[cnt++] = x+push + '0';
			push = 0;
		}
	}
	if(push == 1)
	{
		b[cnt++] = 1+'0';
	}
	
	if(cnt != len)
	{
		cout << "No" <<endl;
		for(int i=cnt-1;i>=0;--i)
			cout<<b[i];
		cout << endl;
	}
	else
	{
		strcpy(c,b);
		sort(a,a+len);
		sort(b,b+len);
		if(strcmp(a,b) == 0)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
		for(int i=cnt-1;i>=0;--i)
			cout<<c[i];
		cout << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值