题目:https://pintia.cn/problem-sets/17/problems/263

题解:https://www.liuchuo.net/archives/2151

题解代码:

#include <stdio.h>
#include <string.h>
int book[10]; //萝卜坑,记录0-9数字出现的个数
int main()
{
	char num[20];
	scanf("%s", num);
	int len = strlen(num);
	int carry = 0; //进位
	//乘以2
	for (int i = len - 1; i >= 0; i--)
	{
		int temp = num[i] - '0';
		book[temp]++;
		temp = temp * 2 + carry;
		carry = 0;
		if (temp >= 10)//大于等于!
		{
			carry = temp / 10;
			temp = temp % 10;
		}
		num[i] = temp + '0';
		book[temp]--;
	}
	//打印结果
	bool isOriginal = true;
	for (int i = 0; i < 10; i++)
	{
		if (book[i] != 0)
		{
			isOriginal = false;
			break;
		}
	}
	printf("%s\n", isOriginal && carry == 0 ? "Yes" : "No"); //原位数字相同、且无进位
	if (carry > 0)
	{
		printf("%d", carry);
	}
	printf("%s\n", num);
	return 0;
}

 

问题:无法自主设计完备的测试用例。

 

自解代码(未通过全部用例):

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//GetPermutation();
int main(){

	unsigned long long int x;
	cin>>x;
	unsigned long long int dx = x*2; 
	unsigned long long int dis = dx;

	vector<int> a(20);
	int c=0;
	while(x>0){
		int t = x%10;
		x= x/10;
		a.insert(a.begin()+c,t);
//		cout<<t<<endl;
	}

	c=0;
	vector<int> b(20);
	while(dx>0){
		int t = dx%10;
		dx= dx/10;
		b.insert(b.begin()+c, t);
//		cout<<t<<endl;
	}
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	if(a.size()==b.size()) {
		bool f = true;
		for(unsigned int i=0;i<a.size();i++){
			if(a[i]!=b[i]){
				cout<<"No"<<endl;
				f=false;
				break;
			}
		}
		if(f){
			cout<<"Yes"<<endl;
			
		}
	}else{
		cout<<"No"<<endl;
	}
	cout<<dis<<endl;

	return 0;
}

 

自解结果:

 

 

参考:

C++ 基础类型 https://en.cppreference.com/w/cpp/language/types

C++ vector insert() https://www.geeksforgeeks.org/vector-insert-function-in-c-stl/

 

 posted on 2020-07-23 15:24  邢涌芝  阅读(74)  评论(0编辑  收藏  举报