首页 > 代码库 > HDU 3925 Substring 【大数相减】
HDU 3925 Substring 【大数相减】
题目意思是,给你提供两个数字 a 和 b
a 可以不断的往上加, 直到b 为其子串
问的是 a 最小加几?
显而易见,a 的数据范围给了10 ^100非常大,直接模拟肯定不行
那么就用 b 减去 a 来找,也算是一种模拟的方法
举个例子, a = 1299, b = 33
<1>33 330 3300 33000
(12)99 (1)299 1299 1299
------ -------- -------- -------
34 31 2001 32001
如果当前 b 比a的部分小,在前面添1
每次比较完在b后面添0
取所有比较结果中最小的数字作为答案
当然,如果a <= b 是可以直接得到答案的 ans = b - a
但是这题用G++提交不知道为什么会WA
C++就能过
Source code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler#include <stdio.h>#include <iostream>#include <fstream>#include <cstring>#include <cmath>#include <stack>#include <string>#include <map>#include <queue>#include <vector>#include <algorithm>#define LL long long#define Max(a,b) (((a) > (b)) ? (a) : (b))#define Min(a,b) (((a) < (b)) ? (a) : (b))#define Abs(x) (((x) > 0) ? (x) : (-(x)))#define MOD 1000000007using namespace std;string Plus(string s1,string s2){ if(s1.length() < s2.length()){ string temp = s1; s1 = s2; s2 = temp; } for(int i = s1.length() - 1, j = s2.length() - 1; i >= 0; --i, --j){ s1[i] = char(s1[i] + (j >= 0 ? s2[j]- ‘0‘ : 0)); if(s1[i] - ‘0‘ >= 10){ s1[i] = char((s1[i] - ‘0‘) % 10 + ‘0‘); if(i) ++s1[i-1]; else s1 += ‘1‘; } } return s1;}string Minus(string a,string b){ string c="",ans="",t; int flag = 0, k = 0; bool flag2 = false; if(a.length() < b.length() || (a.length() == b.length()&& a.compare(b) < 0)){ t = a; a = b; b = t; flag2 = true; } int i = a.length() - 1, j = b.length() - 1; while(i >= 0 && j >= 0){ if(a[i] + flag > b[j]){ c += a[i] + flag - b[j] + ‘0‘; flag = 0; } else if(a[i] + flag == b[j]){ c += ‘0‘; flag = 0; } else{ c += (a[i] - ‘0‘) + flag + 10 - (b[j] - ‘0‘) + ‘0‘; flag = -1; } --i; --j; ++k; } while(i >= 0){ if(a[i] + flag < ‘0‘){ c += a[i] + flag + 10 + ‘0‘; flag = -1; } else{ c += a[i] + flag; flag = 0; } --i; ++k; } int len = k - 1; while(c[len] == ‘0‘ && len > 0) --len; for(j = 0; j <= len; ++j) ans += c[j]; if(flag2){ ans += ‘-‘; } char tt; for(i = 0, j = ans.length()-1; i < j; ++i, --j){ tt = ans[i]; ans[i] = ans[j]; ans[j] = tt; } return ans;}bool judge(string a, string b){ if(a.size() < b.size()) return false; else if(a.size() > b.size()) return true; else{ for(int i = 0; i < a.size(); ++i){ if(a[i] > b[i]) return true; else if(a[i] < b[i]) return false; } }}int main(){ int i, j, t, n, m, numCase = 0; string s1,s2, hh; string cnt, ans; bool flag1; scanf("%d",&t); while(t--){ cin >> s1 >> s2; flag1 = false; int len1 = s1.size(); int len2 = s2.size(); if(judge(s2, s1)){ cout << "Case #" << ++numCase << ": " << Minus(s2, s1) << endl; continue; } for(i = 0; i < len1 - len2; ++i){ string ww = s1.substr(i, len2); if(ww.compare(s2) == 0){ flag1 = true; break; } } if(flag1){ cout << "Case #" << ++numCase << ": " << ‘0‘ << endl; continue; } for(i = 0; i < len1 - len2 + 2; ++i){ if(i == len1 - len2 + 1){ hh = s1.substr(len1 - len2 - i + 1, i + len2 - 1); } else{ hh = s1.substr(len1 - len2 - i, i + len2); } if(judge(hh, s2)){ string gg = "1"; gg += s2; ans = Minus(gg ,hh); } else{ ans = Minus(s2 ,hh); } s2 += "0"; if(i == 0){ cnt = ans; } else{ if(judge(cnt, ans)){ cnt = ans; } } } cout << "Case #" << ++numCase << ": " << cnt << endl; } return 0;}
HDU 3925 Substring 【大数相减】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。