smallest/largest 值的逻辑帮助
logic help for smallest/largest value
我经历了太多版本的算法来对最小和最大进行排序,以至于我的大脑都炸了。到目前为止,这本书和在线搜索都没有任何帮助。
我在保存最后一个时遇到了困难。
我使用 3 英寸、10 厘米和 5 厘米作为测试用例。首先输入 3,变成最大,输入 5 cm 第二变成最小,然后再输入 10 cm 变成最小。尝试了 2 个多小时的不同版本,甚至重写了整个部分。在《Programming Principles and Practices using C++》一书中,它在复习部分,在那之前我找不到任何帮助我的东西。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
vector<double>all_meters;
double smallest= 0,print_smallest, largest = 0,print_largest, num = 0;
string unit, s_input, num_s_input, small_unit, large_unit;
while(cin.good()){
cout << "\n\t\t\t\tEnter '|' to exit.\n\n";
cout << "\t\tNumber to compare followed by white space and unit:";
cin >> num_s_input;
if(num_s_input.compare("|") == 0 || (s_input.compare("|") == 0)){
double sum = 0;
for (double x : all_meters) sum+=x;
cout << "Sum: " << setprecision(4) << sum << "m\n";
cout << "Smallest number: " << print_smallest << small_unit << endl
<< "Largest number: " << print_largest << large_unit << endl
<< "Total number of values: " << all_meters.size() << endl
<< "All the entered numbers converted to meters are: \n";
for (double i = 0; i<all_meters.size(); ++i){
cout << all_meters[i] << setprecision(2) <<"m ";
}
cout << "\nAlright now, goodbye then !\n" << endl;
break;
}
else{
cin >> s_input;
num = strtod(num_s_input.c_str(), NULL);
unit = s_input;
double meter = 0;
if(unit=="cm"){
meter = num / 100;}
else if(unit=="in"){
meter = num / 39.370;}
else if(unit=="ft"){
meter = num / 3.2808;}
else if(unit=="m"){
meter = num;}
else {
cout << "\n\tYou entered wrong unit!\t\n";}
if(largest==0){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest==0&&meter<largest){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
else if(largest<meter){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest>meter){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
all_meters.push_back(meter);
sort(all_meters.begin(),all_meters.end());
}
}
}
设法在不使用限制的情况下解决它,将新的更改添加到代码中。感谢您的帮助!
您的问题很可能是因为您将 smallest
初始化为 0。如果您从不输入任何小于 0 的值,那么 smallest
将永远不会改变。
找到最小值和最大值时,您希望将初始值分别设置为它可以容纳的最大或最小数。所以在这种情况下我们会使用
double smallest = std::numeric_limits<double>::max();
double largest = std::numeric_limits<double>::lowest()
double num = 0;
这是你的数据集中的任何东西都应该小于 smallest
并且所有东西都应该大于 largest
。
这确实需要 #include <limits>
您需要选择标准计量单位。该问题建议使用米,因此请使用它(您可以为此使用 float
或 double
,具体取决于您需要的精度)。
问题很简单,为每个新输入的总和、最小可见值和最大可见值创建一些变量,转换为标准格式,并更新变量。
解决方案(让您入门,而不是工作代码)可能如下所示:
// You can represent the different types of units as integers
float convertToMeters(float unconvertedValue, int unit) {
// Convert unconvertedValue based on unit
}
float smallest = std::numeric_limits<float>::max();
float largest = std::numeric_limits<float>::lowest();
float sum = 0.0f;
// Update for each new input
while (new_input) {
float convertedValue = convertToMeters(new_value, unit);
// Update total
sum += convertedValue;
// Update smallest and largest
if (convertedValue > largest) largest = convertedValue;
else if (convertedValue < smallest) smallest = convertedValue;
}
正如 Nathan 提到的,#include <limits>
限制。
我经历了太多版本的算法来对最小和最大进行排序,以至于我的大脑都炸了。到目前为止,这本书和在线搜索都没有任何帮助。 我在保存最后一个时遇到了困难。 我使用 3 英寸、10 厘米和 5 厘米作为测试用例。首先输入 3,变成最大,输入 5 cm 第二变成最小,然后再输入 10 cm 变成最小。尝试了 2 个多小时的不同版本,甚至重写了整个部分。在《Programming Principles and Practices using C++》一书中,它在复习部分,在那之前我找不到任何帮助我的东西。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
vector<double>all_meters;
double smallest= 0,print_smallest, largest = 0,print_largest, num = 0;
string unit, s_input, num_s_input, small_unit, large_unit;
while(cin.good()){
cout << "\n\t\t\t\tEnter '|' to exit.\n\n";
cout << "\t\tNumber to compare followed by white space and unit:";
cin >> num_s_input;
if(num_s_input.compare("|") == 0 || (s_input.compare("|") == 0)){
double sum = 0;
for (double x : all_meters) sum+=x;
cout << "Sum: " << setprecision(4) << sum << "m\n";
cout << "Smallest number: " << print_smallest << small_unit << endl
<< "Largest number: " << print_largest << large_unit << endl
<< "Total number of values: " << all_meters.size() << endl
<< "All the entered numbers converted to meters are: \n";
for (double i = 0; i<all_meters.size(); ++i){
cout << all_meters[i] << setprecision(2) <<"m ";
}
cout << "\nAlright now, goodbye then !\n" << endl;
break;
}
else{
cin >> s_input;
num = strtod(num_s_input.c_str(), NULL);
unit = s_input;
double meter = 0;
if(unit=="cm"){
meter = num / 100;}
else if(unit=="in"){
meter = num / 39.370;}
else if(unit=="ft"){
meter = num / 3.2808;}
else if(unit=="m"){
meter = num;}
else {
cout << "\n\tYou entered wrong unit!\t\n";}
if(largest==0){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest==0&&meter<largest){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
else if(largest<meter){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest>meter){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
all_meters.push_back(meter);
sort(all_meters.begin(),all_meters.end());
}
}
}
设法在不使用限制的情况下解决它,将新的更改添加到代码中。感谢您的帮助!
您的问题很可能是因为您将 smallest
初始化为 0。如果您从不输入任何小于 0 的值,那么 smallest
将永远不会改变。
找到最小值和最大值时,您希望将初始值分别设置为它可以容纳的最大或最小数。所以在这种情况下我们会使用
double smallest = std::numeric_limits<double>::max();
double largest = std::numeric_limits<double>::lowest()
double num = 0;
这是你的数据集中的任何东西都应该小于 smallest
并且所有东西都应该大于 largest
。
这确实需要 #include <limits>
您需要选择标准计量单位。该问题建议使用米,因此请使用它(您可以为此使用 float
或 double
,具体取决于您需要的精度)。
问题很简单,为每个新输入的总和、最小可见值和最大可见值创建一些变量,转换为标准格式,并更新变量。
解决方案(让您入门,而不是工作代码)可能如下所示:
// You can represent the different types of units as integers
float convertToMeters(float unconvertedValue, int unit) {
// Convert unconvertedValue based on unit
}
float smallest = std::numeric_limits<float>::max();
float largest = std::numeric_limits<float>::lowest();
float sum = 0.0f;
// Update for each new input
while (new_input) {
float convertedValue = convertToMeters(new_value, unit);
// Update total
sum += convertedValue;
// Update smallest and largest
if (convertedValue > largest) largest = convertedValue;
else if (convertedValue < smallest) smallest = convertedValue;
}
正如 Nathan 提到的,#include <limits>
限制。