我一直希望对我的程序为什么以这种方式起作用有所了解。
这是“第2天”黑客排名挑战。
本质上,它是在寻找您采用餐食价格,小费百分比和税收百分比的输入来提供正确的价格作为输出的方式。
该代码最终通过了Hacker Rank测试过的所有测试用例,但遇到问题的是我在本地计算机上测试该代码的情况。
我使用Arch Linux。
Hacker Rank使用的输入是12.00、20和8。
更多下面的代码...
#include <bits/stdc++.h>
using namespace std;
double totalCost;
void solve(double meal_cost, double tip_percent, double tax_percent) {
tip_percent = tip_percent / 100;
tip_percent = meal_cost * tip_percent;
tax_percent = tax_percent / 100;
tax_percent = meal_cost * tax_percent;
totalCost = meal_cost + tip_percent + tax_percent;
}
int main(){
double meal_cost;
cin >> meal_cost;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
double tip_percent;
cin >> tip_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
double tax_percent;
cin >> tax_percent;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
solve(meal_cost, tip_percent, tax_percent);
cout << nearbyint(totalCost);
cout << totalCost << endl;
return 0;
}
起初,我使用...运行代码
$ echo 12 20 8 | ./a.out
这将为我提供正确的15或15.36的输出,而没有nearint(totalCost)。
此后,它就停止了运行,我不得不更改为...
$ printf "12\n8\n20\n" | ./a.out
哪个给出输出
15
15.36
要探究为什么会发生这种情况,我开始关注通过printf()将什么值存储在变量中。
#include <bits/stdc++.h>
using namespace std;
double totalCost;
void solve(double meal_cost, double tip_percent, double tax_percent){
tip_percent = tip_percent / 100;
tip_percent = meal_cost * tip_percent;
tax_percent = tax_percent / 100;
tax_percent = meal_cost * tax_percent;
totalCost = meal_cost + tip_percent + tax_percent;
}
int main(){
printf("Enter Meal Cost\n");
double meal_cost;
cin >> meal_cost;
printf("Reprinted Meal Cost\n");
cout << meal_cost << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
printf("Enter Tip Percent\n");
double tip_percent;
cin >> tip_percent;
printf("Reprinted Tip Percent\n");
cout << tip_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
printf("Enter Tax Percent\n");
double tax_percent;
cin >> tax_percent;
printf("Reprinted Tax Percent\n");
cout << tax_percent << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
solve(meal_cost, tip_percent, tax_percent);
printf("Total rounded to nearest int\n");
cout << nearbyint(totalCost) << endl;
printf("Total not rounded\n");
cout << totalCost << endl;
return 0;
}
我跑步时...
$ echo 12 20 8 | ./a.out
...在第二个程序中,我得到...的输出。
Enter Meal Cost
Reprinted Meal Cost
12
Enter Tip Percent
Reprinted Tip Percent
4.63597e-310
Enter Tax Percent
Reprinted Tax Percent
6.95272e-310
Total rounded to nearest int
12
Total not rounded
12
当我跑步时...
$ printf "12\n8\n20\n" | ./a.out
...在第二个程序中,我得到...
Enter Meal Cost
Reprinted Meal Cost
12
Enter Tip Percent
Reprinted Tip Percent
8
Enter Tax Percent
Reprinted Tax Percent
20
Total Rounded to nearest int
15
Total not rounded
15.36
从本质上讲,我希望了解为什么它最初在echo时正确运行,而现在却没有。
肖恩是对的!
我拿出了...
cin.ignore(numeric_limits<streamsize>::max(), '\n');
这是我对代码进行的更改...
#include <bits/stdc++.h>
using namespace std;
double totalCost;
void solve(double meal_cost, double tip_percent, double tax_percent) {
tip_percent = tip_percent / 100;
tip_percent = meal_cost * tip_percent;
tax_percent = tax_percent / 100;
tax_percent = meal_cost * tax_percent;
totalCost = meal_cost + tip_percent + tax_percent;
}
int main(){
double meal_cost;
cin >> meal_cost;
double tip_percent;
cin >> tip_percent;
double tax_percent;
cin >> tax_percent;
solve(meal_cost, tip_percent, tax_percent);
cout << nearbyint(totalCost) << endl;
cout << totalCost << endl;
return 0;
}
现在我是否要跑步...
$ echo 12 20 8 | ./a.out
或...
$ printf "12\n20\n8\n" | ./a.out
...两者的输出是...
15
15.36
答案 0 :(得分:0)
肖恩是对的!
我拿出了...
cin.ignore(numeric_limits<streamsize>::max(), '\n');
这是我对代码进行的更改...
#include <bits/stdc++.h>
using namespace std;
double totalCost;
void solve(double meal_cost, double tip_percent, double tax_percent) {
tip_percent = tip_percent / 100;
tip_percent = meal_cost * tip_percent;
tax_percent = tax_percent / 100;
tax_percent = meal_cost * tax_percent;
totalCost = meal_cost + tip_percent + tax_percent;
}
int main(){
double meal_cost;
cin >> meal_cost;
double tip_percent;
cin >> tip_percent;
double tax_percent;
cin >> tax_percent;
solve(meal_cost, tip_percent, tax_percent);
cout << nearbyint(totalCost) << endl;
cout << totalCost << endl;
return 0;
}
现在我是否要跑步...
$ echo 12 20 8 | ./a.out
或...
$ printf "12\n20\n8\n" | ./a.out
...两者的输出是...
15
15.36