好多if
2025-10-20 21:45:37
发布于:四川
暴力
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
long long ans=0;
long long negative=1;
long long cnt=0;
while(cin>>s)
{
if(s=="negative")
{
negative=-1;
}
if(s=="zero")cnt+=0;
if(s=="one")cnt+=1;
if(s=="two")cnt+=2;
if(s=="three")cnt+=3;
if(s=="four")cnt+=4;
if(s=="five")cnt+=5;
if(s=="six")cnt+=6;
if(s=="seven")cnt+=7;
if(s=="eight")cnt+=8;
if(s=="nine")cnt+=9;
if(s=="ten")cnt+=10;
if(s=="eleven")cnt+=11;
if(s=="twelve")cnt+=12;
if(s=="thirteen")cnt+=13;
if(s=="fourteen")cnt+=14;
if(s=="fifteen")cnt+=15;
if(s=="sixteen")cnt+=16;
if(s=="seventeen")cnt+=17;
if(s=="eighteen")cnt+=18;
if(s=="nineteen")cnt+=19;
if(s=="twenty")cnt+=20;
if(s=="thirty")cnt+=30;
if(s=="forty")cnt+=40;
if(s=="fifty")cnt+=50;
if(s=="sixty")cnt+=60;
if(s=="seventy")cnt+=70;
if(s=="eighty")cnt+=80;
if(s=="ninety")cnt+=90;
if(s=="hundred")cnt*=100;
if(s=="thousand")
{
ans+=(cnt*1000);
cnt=0;
}
if(s=="million")
{
ans+=(cnt*1000000);
cnt=0;
}
}
ans+=cnt;
cout<<ans*negative<<endl;
return 0;
}
全部评论 2
nb,看看我的
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<string, long long> num_map = {
{"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},
{"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9},
{"ten", 10}, {"eleven", 11}, {"twelve", 12}, {"thirteen", 13},
{"fourteen", 14}, {"fifteen", 15}, {"sixteen", 16}, {"seventeen", 17},
{"eighteen", 18}, {"nineteen", 19}, {"twenty", 20}, {"thirty", 30},
{"forty", 40}, {"fifty", 50}, {"sixty", 60}, {"seventy", 70},
{"eighty", 80}, {"ninety", 90}, {"hundred", 100},
{"thousand", 1000}, {"million", 1000000}
};
long long calc_part(const vector<string>& part) {
long long res = 0;
long long temp = 0;
for (const string& s : part) {
if (s == "hundred") {
temp *= 100;
} else {
temp += num_map[s];
}
}
res += temp;
return res;
}
int main() {
string line;
getline(cin, line);
stringstream ss(line);
vector<string> words;
string word;
while (ss >> word) {
words.push_back(word);
}
bool is_negative = false;
int start = 0;
if (words[0] == "negative") {
is_negative = true;
start = 1;
}
long long ans = 0;
vector<string> part;
for (int i = start; i < words.size(); ++i) {
if (words[i] == "million") {
ans += calc_part(part) * 1000000;
part.clear();
} else if (words[i] == "thousand") {
ans += calc_part(part) * 1000;
part.clear();
} else {
part.push_back(words[i]);
}
}
ans += calc_part(part);
if (is_negative) ans = -ans;
cout << ans;
return 0;
}5天前 来自 广东
0头文件错了

应该用#include <bits/stdc++.h>的2025-05-07 来自 四川
0没关系的
2025-07-27 来自 俄罗斯
0







有帮助,赞一个