1023. Have Fun with Numbers (20) 解答和记录

题目地址:https://www.patest.cn/contests/pat-a-practise/1012

解题思路:用string 来保存大数计算的结果,然后将输入和输出分别映射成向量,进行比较。算法复杂度:O(length)

坑点:注意结果的逆序,注意判断is_p()函数变量的定义,不能加const修饰

代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

string double_n(const string &n)
{
	string m;
	string a;
	int flag = 0;
	for(int i = n.size() - 1; i >= 0; i--)
	{
		a += (2 * (n[i] - '0') + flag) % 10 + '0';
		flag = (2 * (n[i] - '0') + flag) >= 10 ? 1 : 0;
	}

	if(flag == 1)
		a += '1';
	//cout << a << endl;
	for(string::reverse_iterator it = a.rbegin(); it != a.rend(); it++)
		m += *it;
	return m;
}

bool is_p(string &n, string &m) //there is problem 
{
	string a = "0000000000", b = "0000000000";
	for(string::iterator it = n.begin(); it != n.end(); it++)
		a[*it-'0'] += 1;
	for(string::iterator it = m.begin(); it != m.end(); it++)
		b[*it-'0'] += 1;
	if(a == b)
		return true;
	else
		return false;
}

int main(void)
{
	string line;
	while(getline(cin, line))
	{
		string n, m;
		stringstream ss(line);
		ss >> n;
		m = double_n(n);
		if(is_p(n, m))
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
		cout << m << endl;
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值