• 【洛谷】P2142 高精度减法


    #include<bits/stdc++.h>
    using namespace std;
    struct bign{
    	int len; string s;
    	void length(){
    		len = s.length();
    	}
    	bign operator - (const bign x) const
    	{
    		bign ret,a1,a2;
    		a1.len = len; a1.s = s;
    		a2.len = x.len; a2.s = x.s;
    		int l = a1.len;
    		if (a1.len > a2.len)
    		{
    			for (int i = 1 ; i <= a1.len- a2.len ; i ++) a2.s = '0'+a2.s;
    		}
    		for (int i = l - 1 ; i >= 0 ; i --)
    		{
    			if (a1.s[i] < a2.s[i])
    			{
    				a1.s[i] = char(a1.s[i]+10);
    				a1.s[i-1] = char(a1.s[i-1]-1);
    			}
    			ret.s = char(a1.s[i]-a2.s[i] + 48) + ret.s;
    		}
    		while (ret.s[0] == '0' && ret.s.length() > 1) ret.s.erase(0,1);
    		ret.len = ret.s.length();
    		return ret;
    	}
    }a,b;
    
    bool cmp(bign x,bign y)
    {
    	
    	if (x.len > y.len) return 0;
    	if (x.len < y.len) return 1;
    	for (int i = 0 ; i < x.len ; i ++)
    	{
    		if (x.s[i] > y.s[i]) return 0;
    		if (x.s[i] < y.s[i]) return 1;
    	}
    	return 0;
    }
    int main()
    {
    	getline(cin,a.s);
    	getline(cin,b.s);
    	a.length(); b.length();
    	if (cmp(a,b)) 
    	{
    		swap(a,b);
    		cout<<'-';
    	}
    	a = a - b;
    	cout<<a.s<<endl; 
    	return 0;
    }
    

      自己写的,重载运算符版,哈哈~

  • 相关阅读:
    lists 函数整理
    orddict 练习
    github 的使用
    wxListCtrl 例子 二
    Erlang eunit
    Erlang 中 Tuple 使用 以及 List 模块意外
    Erlang Json
    模块和包
    Mysql作为zabbix数据库ibdata1文件太高解决
    用户管理和数据库安全
  • 原文地址:https://www.cnblogs.com/YMY666/p/8045813.html
Copyright © 2020-2023  润新知