明明自己跑都成功1g,但測試執行時他卻說我是bye~Rabbit?
另外,我解題也一直NA,一直找不出原因,請各位大大解答
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int carrot[5] = { 0 };// 空,紅, 白, 黃, 黑
int times, poison,accumulated_poison, weight;
int read_carrot;
stringstream ss("");
string str;
cin >> times;
for (int i = 0; i < times; i++)
{
ss.str("");
ss.clear();
accumulated_poison = 0;
cin >> carrot[1] >> carrot[2] >> carrot[3] >> carrot[4] >> poison >> weight;
carrot[3] *= -1;
carrot[4] *= -1;
//上一行還有enter鍵還沒讀,所以要先清掉enter,再讀下一行
getline(cin, str);
getline(cin, str);
ss << str;
while (!ss.eof() && (weight -= accumulated_poison)>0 && str!="")
{
ss >> read_carrot;
weight += carrot[read_carrot];
if (read_carrot == 4)
accumulated_poison += poison;
}
(weight <= 0) ? (cout << "bye~Rabbit") : (cout << weight<<'g');
cout << endl;
}
return 0;
}
明明自己跑都成功1g,但測試執行時他卻說我是bye~Rabbit?
另外,我解題也一直NA,一直找不出原因,請各位大大解答
#include
#include
#include
using namespace std;
int main()
{
int carrot[5] = { 0 };// 空,紅, 白, 黃, 黑
int times, poison,accumulated_poison, weight;
int read_carrot;
stringstream ss("");
string str;
cin >> times;
for (int i = 0; i < times; i++)
{
ss.str("");
ss.clear();
accumulated_poison = 0;
cin >> carrot[1] >> carrot[2] >> carrot[3] >> carrot[4] >> poison >> weight;
carrot[3] *= -1;
carrot[4] *= -1;
//上一行還有enter鍵還沒讀,所以要先清掉enter,再讀下一行
getline(cin, str);
getline(cin, str);
ss << str;
while (!ss.eof() && (weight -= accumulated_poison)>0 && str!="")
{
ss >> read_carrot;
weight += carrot[read_carrot];
if (read_carrot == 4)
accumulated_poison += poison;
}
(weight <= 0) ? (cout << "bye~Rabbit") : (cout << weight<<'g');
cout << endl;
}
return 0;
}
while
的部份改成
while (ss >> read_carrot) { if ((weight -= accumulated_poison) <= 0) break; weight += carrot[read_carrot]; if (read_carrot == 4) accumulated_poison += poison;
}
就可以過測試了