比赛 防止浮躁的小练习v0.4 评测结果 AAAAAA
题目名称 增强的减法问题 最终得分 100
用户昵称 要要要不吃药 运行时间 0.034 s
代码语言 C++ 内存使用 0.26 MiB
提交时间 2016-10-13 16:54:16
显示代码纯文本
#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <iostream>  
#include <algorithm>  
using namespace std;   
const int maxn = 10010;   
int len1, len2;   
int a[maxn], b[maxn];   
string s1, s2;   
void swap(string &a, string &b)   
{  
	string t;   
	t = a;   
	a = b;   
	b = t;   
}  
bool compare(string a, string b)  
{  
	if (a.size() < b.size()) return true;   
	if (a.size() > b.size()) return false;   
	for (int i = 0; i < a.size(); ++i)       
	{  
		if (a[i] > b[i]) return false;      
		if (b[i] > a[i]) return true;   
	}  
	return false;   
}  
int main()  
{  
	freopen("sub.in","r",stdin);  
	freopen("sub.out","w",stdout);  
	cin >> s1;             
	cin >> s2;   
	if (compare(s1, s2))   
	{  
		swap(s1, s2);    
		putchar('-');   
	}  
	len1=s1.size();   
	len2=s2.size();   
	for (int i = 0; i<len1; ++i) a[len1 - i] = s1[i] - '0';   
	for (int i = 0; i<len2; ++i) b[len2 - i] = s2[i] - '0';   
	for (int i = 1; i <= len1; ++i)  
	{  
		a[i] -= b[i];                 
		if (a[i] < 0)                
		{                      
			a[i+1]--;              
			a[i]+=10;   
		}
	}  
	while (a[len1]==0&&len1>1)len1--;   
	for (int i=len1;i>=1;--i)  
		printf("%d",a[i]);   
	return 0;   
}