题解(有陷阱)
2026-08-15 23:07:10
发布于:浙江
4阅读
0回复
0点赞
好久没写题解了,写一篇
第十个测试点有一个数有两个前导零:
#10输入:
003257521461422009164021086405825199230944903020364089670997910422399419849832734236617940541487752429303786770438386980913735666307805170880206594585806315784408043679149377313479145839523607641660605367575000571489220264804833468418915411234389841358758545180365504769119290647634302848487567030807423636524483897558953066659791657659227338446256904653526987286904096938911728718877682886351604173098150142646505241418053981326227682300331394729782714111463063564050267694354033890193415664538517670413490202847060826736074255312067147869738136309599178138625123566546567423986364890098823492200808639505181341008206340101975597861028957263145472928096316707055314578478419899068166544167926579141578995002928379277359967159587790168527165213214671246084964228360533376156861481552038760743640515658336626771502900971179033907746966998158994110780885313586623761946404881976473204770831981210355016484132561521448107914659903790936292924889510764525991729588402093978496559525668685103113744939347083372287911054517601561674736425544117926572678995371049721254390638071012605469528336758812606402505447062288941387315286715935512166555078862265222284590218895495651001312980320391758275729878390589699330110658175061377446552123528327790179380905642359955430461623530982425704820427002718411827024887792707218656720794262212266830628730424491329779809901222794711144754668632245703660636830097634517317532451453482305544207005629052722059079702642509372960378339220593101774398648901776518888282369691384389110568643955817331770773521492512707867215903943796057723468357092839414846263345235169254260809477603499620434024170056615851456110244756912877140895386930097600314116713724768324956865316178372430980392440407307829768991306741929295853175573224054686865288327858690065099041700868870158495744508681712716299373210370714505397539427691284640145364667510539497104885490733330500043815952553377604627895769897354630954859413928807358609936603188410178434923481429295772642774769238981489461050197811129804231695604080359646709966419893031113950487998638738346470876409711268236260803089704330760669928156579324156670852159696774647839992778118953951829858219865094029716639996123171686823880145945094593424295858692727459537388776335548085573615123535016202431666419446724907899461855020093284092160472767084727909121413747662315740340060029004646609246935894039100580530399071362764283930523426822238266769651285077684414449796271738395835058348988780129202527691672909193779949915179483717914778015838411850144501291361735209533057557226156392391726461342843124095098894149196960637274201327664747004518248647525834997010040757382670385818554397206192488806213594683480090466308781417953675414990930434502625359814634129853406142665119854766747343982170427200746307577579101384606926321709255809386485583276814786736401454478472026042257596862545591784568263740895908455514410789744631877543810084553112102095353916244996142915033509941483396561162727385696904746293631329591297240032686557593998335419747066779525736143315476634098959373007816530808877612415002458154821406430136743808755478926702586389735106294819865427868537661783809355965977275233864887199006981556222781803147874905962558258170118384562345633267774911793
35521011145055789767110438766901605984092572541306774905929305171276341708104479278436275997238867342082953572565965267892330348972044855297339646724902272331381555809577211291979267645479058859648847849454711055189871941473055830761741954919740139437483462341008946596935948676858640620055683707046055064985201241913672415353990365075050150654925497788730622106160317272106271652257947305810907729662451282473173421398671499335410007985074035646351246670555419309634340828937871146325899434065775803111824218810122123946071522594654143942055431573683514201457334800401835511647018608069325604999692220256595187264268481757784933824360923033551701488929083060202829274910866132473341346390271983974385599001957283831790834634051067821476712309820024159407767690835642994945792787862849466290732989250630417813596056810091964229648978680244399967284526537736724884575320873116179935041013299316999710581499250641842258747242027817448915772474526170059492858231227571600391692302734392077059715093218593459571667613949024882294270356408409519125260010082801310730608178808696840185595265206173497859423145943954300202198573970369649966915564089915102221066842979767535389061708334446430669364292906146537219815597774942006077986465464520263977262658572507448231225385123931289398474164128758428873150114905092645148684355415846330411977338004616612519948931719577240584993181671905815553839153385381992237466245496986280575589360474699218595975487355053451968021031086550557656481378375774323167138732906897930720370032376921865592161414666472465746306165298818853916675625506849810544698917081976396673185400844423123297262783835648909103942605859998788478902251877949798709857622219755949688478765975892542287880824206449895509298351038288401577636446401775037195098282624137442045001099223946692519834691661745154860917954493085006362611539234878845234441580869312194306238
可以看到,有前导零
所以大数转字符串时要去前导零
废话多说,上代码:
我用的class
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class bigint{
private:
vector<int> a;
friend istream& operator>> (istream& in, bigint &a);//定义输入
friend ostream& operator<< (ostream& out, bigint a);//定义输出
bigint string_to_bigint(string s) {
reverse(s.begin(), s.end());
bigint b;
for(char c : s) {
b.a.push_back(c - '0');
}
return b;
}
string bigint_to_string(bigint a) {
string res;
for(auto it = a.a.rbegin(); it != a.a.rend(); ++it) {
res += *it + '0';
}
size_t pos = res.find_first_not_of('0');//下面这段代码用来去前导0
if(pos == string::npos) {
return "0";
}
return res.substr(pos);
}
public:
explicit operator vector<int>() {//类型转换
return a;
}
bigint operator+(bigint b) {//加法
bigint res;
int carry = 0;
for(int i = 0; i < a.size() || i < b.a.size() || carry; i++) {
if(i < a.size()) carry += a[i];
if(i < b.a.size()) carry += b.a[i];
res.a.push_back(carry % 10);
carry /= 10;
}
return res;
}
};
istream& operator>> (istream& in, bigint &a) {//实现输入
string s;
in >> s;
a.a = (vector<int>)a.string_to_bigint(s);
return in;
}
ostream& operator<< (ostream& out, bigint a) {//实现输出
string s = a.bigint_to_string(a);
out << s;
return out;
}
int main() {
bigint a, b;
cin >> a >> b;
cout << a + b;
}
这里空空如也







有帮助,赞一个