problemscpp
A collection of my answers to algorithm problems in c++.
函数
pat::b::b1074 命名空间参考

1074 宇宙无敌加法器 更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (b1074, case1)
 

详细描述

1074 宇宙无敌加法器

函数说明

◆ main()

int pat::b::b1074::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp2587 行定义.

2587 {
2588 string str_n;
2589 string str_a;
2590 string str_b;
2591 cin >> str_n >> str_a >> str_b;
2592 vector<int> n;
2593 vector<int> a;
2594 vector<int> b;
2595 vector<int> ans;
2596 for(int i = str_n.length() - 1; i >= 0; i--) {
2597 if(str_n[i] == '0') {
2598 n.push_back(10);
2599 } else {
2600 n.push_back(str_n[i] - '0');
2601 }
2602 }
2603 for(int i = str_a.length() - 1; i >= 0; i--) {
2604 a.push_back(str_a[i] - '0');
2605 }
2606 for(int i = str_b.length() - 1; i >= 0; i--) {
2607 b.push_back(str_b[i] - '0');
2608 }
2609 int carry = 0;
2610 for(int i = 0; i < max(a.size(), b.size()) || carry != 0; i++) {
2611 const int radix = i < n.size() ? n[i] : 10;
2612 const int num_a = i < a.size() ? a[i] : 0;
2613 const int num_b = i < b.size() ? b[i] : 0;
2614 const int sum = num_a + num_b + carry;
2615 ans.push_back(sum % radix);
2616 carry = sum / radix;
2617 }
2618 int i = ans.size() - 1;
2619 while(ans[i] == 0 && i > 0) {
2620 i--;
2621 }
2622 for(; i >= 0; i--) {
2623 cout << ans[i];
2624 }
2625 return 0;
2626 }

被这些函数引用 TEST().

◆ TEST()

pat::b::b1074::TEST ( b1074  ,
case1   
)

在文件 pat_test.cpp1230 行定义.

1230 {
1231 istringstream in("30527\n"
1232 "06203\n"
1233 "415");
1234 auto out = ostringstream();
1235 main(in, out);
1236 const auto ans = out.str();
1237 ASSERT_EQ("7201", out.str());
1238 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().