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

1044 火星数字 更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (b1044, case1)
 
 TEST (b1044, case2)
 
 TEST (b1044, case3)
 

详细描述

1044 火星数字

函数说明

◆ main()

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

在文件 pat.cpp1415 行定义.

1415 {
1416 string abc[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
1417 string abc2[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
1418 unordered_map<string, int> um;
1419 unordered_set<string> us2;
1420 for(int i = 0; i < 13; i++) {
1421 us2.insert(abc2[i]);
1422 um[abc[i]] = i;
1423 um[abc2[i]] = i;
1424 }
1425 int n;
1426 cin >> n;
1427 char cccc[16];
1428 cin.getline(cccc, 16);
1429 while(n-- != 0) {
1430 auto *cstr = new char[1024];
1431 cin.getline(cstr, 1024);
1432 stringstream ss;
1433 ss << cstr;
1434 if(isdigit(ss.peek()) != 0) {
1435 // 地球数字
1436 unsigned long long num;
1437 ss >> num;
1438 vector<string> output;
1439 bool flag = true;
1440 if(num == 0) {
1441 cout << "tret";
1442 } else {
1443 while(num != 0) {
1444 if(flag) {
1445 if(num % 13 != 0) {
1446 output.push_back(abc[num % 13]);
1447 }
1448 flag = false;
1449 } else {
1450 output.push_back(abc2[num % 13]);
1451 }
1452 num /= 13;
1453 }
1454 for(auto it = output.rbegin(); it != output.rend(); ++it) {
1455 cout << *it;
1456 if(it + 1 != output.rend()) {
1457 cout << ' ';
1458 }
1459 }
1460 }
1461 } else {
1462 // 火星数字
1463 int b = 0;
1464 for(int i = 0; cstr[i] != '\0'; i++) {
1465 if(cstr[i] == ' ') {
1466 b++;
1467 }
1468 }
1469 b++;
1470 string str;
1471 unsigned long long num = 0;
1472 while(b != 0) {
1473 ss >> str;
1474 num += um[str] * pow(13, --b);
1475 }
1476 if(us2.contains(str)) {
1477 num *= 13;
1478 }
1479 cout << num;
1480 }
1481 delete[] cstr;
1482 if(n != 0) {
1483 cout << endl;
1484 }
1485 }
1486 return 0;
1487 }

被这些函数引用 TEST().

◆ TEST() [1/3]

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

在文件 pat_test.cpp665 行定义.

665 {
666 istringstream in("4\n"
667 "29\n"
668 "5\n"
669 "elo nov\n"
670 "tam");
671 auto out = ostringstream();
672 main(in, out);
673 const auto ans = out.str();
674 ASSERT_EQ("hel mar\n"
675 "may\n"
676 "115\n"
677 "13",
678 out.str());
679 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

◆ TEST() [2/3]

pat::b::b1044::TEST ( b1044  ,
case2   
)

在文件 pat_test.cpp681 行定义.

681 {
682 istringstream in("1\n"
683 "tret tret\n");
684 auto out = ostringstream();
685 main(in, out);
686 const auto ans = out.str();
687 ASSERT_EQ("0", out.str());
688 }

引用了 main().

◆ TEST() [3/3]

pat::b::b1044::TEST ( b1044  ,
case3   
)

在文件 pat_test.cpp690 行定义.

690 {
691 istringstream in("1\n"
692 "0\n");
693 auto out = ostringstream();
694 main(in, out);
695 const auto ans = out.str();
696 ASSERT_EQ("tret", out.str());
697 }

引用了 main().