天天看點

2749(附大正整數減法)

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

bool change(char& ch)
{
	switch(ch)
	{
	case '3':
	case '4':
	case '7':
		return false;
	case '6':
		ch = '9';
		break;
	case '9':
		ch = '6';
		break;
	default:// 0 1 2 5 8
		break;
	}
	return true;
}

bool NSmaller(const string& a, const string& b)
{
	for(size_t i=0;i<a.size();i++)
	{
		if(a[i]==b[i])
			continue;
		if(a[i]>b[i])
			return true;
		else
			return false;
	}
	return true;
}

void Subtract(string& str)
{
	string big(str);
	for(size_t i=0; i<str.size(); i++)
	{
		if(!change(str[i]))
		{
			str = "ERROR";
			return;
		}
	}
	string small(str.rbegin(), str.rend());

	if(!NSmaller(big, small))
	{
		str = small;
		small = big;
		big = str;
	}

	for(int i=big.size()-1;i>=0;--i)
	{
		if(big[i]>=small[i])
			str[i] = big[i]-small[i]+'0';
		else
		{
			int k=i-1;
			while(k>=0)
			{
				if(big[k]>'0')
				{
					--big[k];
					for(int j=k+1;j<i;++j)
						big[j]+=9;
					big[i]+=10;
					break;
				}
				--k;
			}
			str[i] = big[i]-small[i]+'0';
		}
	}

	string::size_type position = str.find_first_not_of('0');
	if(position != str.npos)
		str.erase(0, position);
	else
		str = "0";
}


int main()
{
	int count;
	cin>>count;
	vector<string> svec;
	string str;
	for(int i=0; i<count; i++)
	{
		cin>>str;
		Subtract(str);
		svec.push_back(str);
	}
	for(int i=0; i<count; i++)
			cout<<svec[i]<<endl;
	return 0;
}
           

-----------------------

不是大整數

------------------

#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
#include <vector>
using namespace std;

bool change(char& ch)
{
	switch(ch)
	{
	case '3':
	case '4':
	case '7':
		return false;
	case '6':
		ch = '9';
		break;
	case '9':
		ch = '6';
		break;
	default:// 0 1 2 5 8
		break;
	}
	return true;
}

int Sub(const int& a)
{
	stringstream ss;
	string str;
	int b;
	ss<<a;
	ss>>str;
	ss.clear();
	for(int i=0; i<str.size(); i++)
	{
		if(!change(str[i]))
			return -1;
	}
	string s(str.rbegin(), str.rend());
	ss<<s;
	ss>>b;
	return abs(a-b);
}

int main()
{
	int count;
	cin>>count;
	vector<int> ivec;
	int a;
	for(int i=0; i<count; i++)
	{
		cin>>a;
		ivec.push_back(Sub(a));
	}

	for(int i=0; i<count; i++)
	{
		if(ivec[i]!=-1)
			cout<<ivec[i]<<endl;
		else
			cout<<"ERROR"<<endl;
	}
	return 0;
}
           

-------------------------------------

大正整數減法

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

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

using std::vector;

bool Subtract(vector<int> ivec1,vector<int> ivec2,vector<int> &ivec)
{
	if(ivec1.size()<ivec2.size())     //被減數小于減數
		return false;

	int t=ivec1.size()-1;
	if(ivec1.size()==ivec2.size())    //等長
	{
		for(;t>=0;--t)
		{
			if(ivec1[t]>ivec2[t])     //等長且被減數大于減數
				break;
			if(ivec1[t]<ivec2[t])     //等長但被減數小于減數
				return false;
		}
	}

	if(t<0)  //相等時
	{
		ivec.push_back(0);
		return true;
	}

	int x=ivec1.size()-ivec2.size();
	for(int i=0;i<x;++i)
		ivec2.push_back(0);

	for(int i=0;i!=ivec1.size();++i)
	{
		if(ivec1[i]>=ivec2[i])
			ivec.push_back(ivec1[i]-ivec2[i]);
		else
		{
			int k=i+1;
			while(k!=ivec1.size())
			{
				if(ivec1[k]>0)
				{
					--ivec1[k];
					for(int j=k-1;j>i;--j)
						ivec1[j]+=9;
					ivec1[i]+=10;
					break;
				}
				++k;
			}
			ivec.push_back(ivec1[i]-ivec2[i]);
		}
	}

	if(ivec.size()>1)
	{
		int m=ivec.size()-1;
		while(m!=0)
		{
			if(ivec[m]==0)
			{
				ivec.pop_back();
				m=ivec.size()-1;
			}
			else
				break;
		}
	}
	return true;
}

int main()
{
	int n=0;
	cin>>n;
	for(int i=0;i!=n;++i)
	{
		vector<int> ivec1;
		vector<int> ivec2;
		vector<int> ivec;

		string str1;
		string str2;
		cin>>str1;
		cin>>str2;

		for(int j=str1.size()-1;j>=0;--j)
			ivec1.push_back(str1[j]-'0');

		for(int j=str2.size()-1;j>=0;--j)
			ivec2.push_back(str2[j]-'0');

		if(!Subtract(ivec1,ivec2,ivec))
			Subtract(ivec2,ivec1,ivec);
		for(int i=ivec.size()-1;i>=0;--i)
			cout<<ivec[i];
		cout<<endl;
	}
	return 0;
}
           

繼續閱讀