A+B Problem
2026-01-28 11:50:24
发布于:广东
3阅读
0回复
0点赞
今天教大家做A+B Problem
#include <iostream>
#include <vector>
#include <string>
using std::cout;
using std::cin;
using std::vector;
using std::string;
using std::ios;
class IO{
public:
template<typename T>T read(){
T IN;cin>>IN;
return IN;
}
template<typename ...T>void write(T...args){
(cout<<...<<args);
}
template<typename ...T>void writeln(T...args){
write(args...,"\n");
}
};
class AB{
private:
string A,B;
IO * io = new IO();
vector<int> add(const vector<int>& a, const vector<int>& b) {
vector<int> res;
int carry = 0;
int i = 0;
while (i < a.size() || i < b.size() || carry > 0) {
int sum = carry;
if (i < a.size()) sum += a[i];
if (i < b.size()) sum += b[i];
res.push_back(sum % 10);
carry = sum / 10;
i++;
}
return res;
}
vector<int> stb(const string& s) {
vector<int> res;
for (int i = s.size() - 1; i >= 0; i--) {
res.push_back((s[i] - '0'));
}
return res;
}
public:
AB(bool YES_RUN_GO,int argc,const char* argv[]){
if(YES_RUN_GO){
this->A=io->read<string>(),this->B=io->read<string>();
}
}
void __init__(bool YES_INIT_GO){
if(YES_INIT_GO){
vector<int> a = this->stb(this->A);
vector<int> b = this->stb(this->B);
vector<int> c = this->add(a,b);
for(int i = c.size()-1;i>=0;i--){
io->write(int(c[i]));
}
io->writeln();
}
}
};
static bool AB_PROBLEM_YES=true;
int main(int argc,const char* argv[]){
ios::sync_with_stdio(0);
cin.tie(0);
AB * ab = new AB(AB_PROBLEM_YES,
argc,
argv);
ab->__init__(AB_PROBLEM_YES);
delete ab;
}
短短75行,包含了类、对象、函数、折叠表达式、模板、指针、维克托vector等基础,太简单了!
这里空空如也




有帮助,赞一个