我如何使该程序更小,更高效
How do I make this program smaller, more efficient?
我正在做一个这样的练习问题:
编写一个要求用户输入汉堡包数量的程序由十个不同的人(p1,p2,...等)
输入数据后,该程序必须分析最少汉堡包的数据和输出,并输出吃最多的汉堡包的人。
我知道我的代码不是理想的(至少可以说,但是我决心要完成它,这样我就可以解决这个问题。如果您愿意提供帮助,请不要害怕向我扔一些更复杂的东西,这就是我学习的方式,对吗?
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;
int p5 = 0;
int p6 = 0;
int p7 = 0;
int p8 = 0;
int p9 = 0;
int p10 = 0;
cout << "How many pancakes did p1 eat?" << endl;
cin >> p1;
cout << "How many pancakes did p2 eat?" << endl;
cin >> p2;
cout << "How many pancakes did p3 eat?" << endl;
cin >> p3;
cout << "How many pancakes did p4 eat?" << endl;
cin >> p4;
cout << "How many pancakes did p5 eat?" << endl;
cin >> p5;
cout << "How many pancakes did p6 eat?" << endl;
cin >> p6;
cout << "How many pancakes did p7 eat?" << endl;
cin >> p7;
cout << "How many pancakes did p8 eat?" << endl;
cin >> p8;
cout << "How many pancakes did p9 eat?" << endl;
cin >> p9;
cout << "How many pancakes did p10 eat?" << endl;
cin >> p10;
// LARGE section of if statements incoming
// Test to see which person ate the least pancakes
if (p1 < p2 && p1 < p3 && p1 < p4 && p1 < p5 && p1 < p6 && p1 < p7 && p1 < p8 && p1 < p9 && p1 < p10)
{
cout << "p1 ate the least pancakes." << endl;
}
if (p2 < p1 && p2 < p3 && p2 < p4 && p2 < p5 && p2 < p6 && p2 < p7 && p2 < p8 && p2 < p9 && p2 < p10)
{
cout << "p2 ate the least pancakes." << endl;
}
if (p3 < p1 && p3 < p2 && p3 < p4 && p3 < p5 && p3 < p6 && p3 < p7 && p3 < p8 && p3 < p9 && p3 < p10)
{
cout << "p3 ate the least pancakes." << endl;
}
if (p4 < p1 && p4 < p2 && p4 < p3 && p4 < p5 && p4 < p6 && p4 < p7 && p4 < p8 && p4 < p9 && p4 < p10)
{
cout << "p4 ate the least pancakes." << endl;
}
if (p5 < p1 && p5 < p2 && p5 < p3 && p5 < p4 && p5 < p6 && p5 < p7 && p5 < p8 && p5 < p9 && p5 < p10)
{
cout << "p5 ate the least pancakes." << endl;
}
if (p6 < p1 && p6 < p2 && p6 < p3 && p6 < p4 && p6 < p5 && p6 < p7 && p6 < p8 && p6 < p9 && p6 < p10)
{
cout << "p6 ate the least pancakes." << endl;
}
if (p7 < p1 && p7 < p2 && p7 < p3 && p7 < p4 && p7 < p5 && p7 < p6 && p7 < p7 && p7 < p9 && p7 < p10)
{
cout << "p7 ate the least pancakes." << endl;
}
if (p8 < p1 && p8 < p2 && p8 < p3 && p8 < p4 && p8 < p5 && p8 < p6 && p8 < p7 && p8 < p9 && p8 < p10)
{
cout << "p8 ate the least pancakes." << endl;
}
if (p9 < p1 && p9 < p2 && p9 < p3 && p9 < p4 && p9 < p5 && p9 < p6 && p9 < p7 && p9 < p8 && p9 < p10)
{
cout << "p9 ate the least pancakes." << endl;
}
if (p10 < p1 && p10 < p2 && p10 < p3 && p10 < p4 && p10 < p5 && p10 < p6 && p10 < p7 && p10 < p8 && p10 < p9)
{
cout << "p10 ate the least pancakes." << endl;
}
// Test to see who ate the most pancakes
if (p1 > p2 && p1 > p3 && p1 > p4 && p1 > p5 && p1 > p6 && p1 > p7 && p1 > p8 && p1 > p9 && p1 > p10)
{
cout << "p1 ate the most pancakes." << endl;
}
if (p2 > p1 && p2 > p3 && p2 > p4 && p2 > p5 && p2 > p6 && p2 > p7 && p2 > p8 && p2 > p9 && p2 > p10)
{
cout << "p2 ate the most pancakes." << endl;
}
if (p3 > p1 && p3 > p2 && p3 > p4 && p3 > p5 && p3 > p6 && p3 > p7 && p3 > p8 && p3 > p9 && p3 > p10)
{
cout << "p3 ate the most pancakes." << endl;
}
if (p4 > p1 && p4 > p2 && p4 > p3 && p4 > p5 && p4 > p6 && p4 > p7 && p4 > p8 && p4 > p9 && p4 > p10)
{
cout << "p4 ate the most pancakes." << endl;
}
if (p5 > p1 && p5 > p2 && p5 > p3 && p5 > p4 && p5 > p6 && p5 > p7 && p5 > p8 && p5 > p9 && p5 > p10)
{
cout << "p5 ate the most pancakes." << endl;
}
if (p6 > p1 && p6 > p2 && p6 > p3 && p6 > p4 && p6 > p5 && p6 > p7 && p6 > p8 && p6 > p9 && p6 > p10)
{
cout << "p6 ate the most pancakes." << endl;
}
if (p7 > p1 && p7 > p2 && p7 > p3 && p7 > p4 && p7 > p5 && p7 > p6 && p7 > p7 && p7 > p9 && p7 > p10)
{
cout << "p7 ate the most pancakes." << endl;
}
if (p8 > p1 && p8 > p2 && p8 > p3 && p8 > p4 && p8 > p5 && p8 > p6 && p8 > p7 && p8 > p9 && p8 > p10)
{
cout << "p8 ate the most pancakes." << endl;
}
if (p9 > p1 && p9 > p2 && p9 > p3 && p9 > p4 && p9 > p5 && p9 > p6 && p9 > p7 && p9 > p8 && p9 > p10)
{
cout << "p9 ate the most pancakes." << endl;
}
if (p10 > p1 && p10 > p2 && p10 > p3 && p10 > p4 && p10 > p5 && p10 > p6 && p10 > p7 && p10 > p8 && p10 > p9)
{
cout << "p10 ate the most pancakes." << endl;
}
return 0;
}
煎饼数量的向量
尝试以下操作:
typedef std::vector<unsigned int> Pancake_Container;
const unsigned int MAXIMUM_PARTICIPANTS = 10;
Pancake Container pancake_quantities(MAXIMUM_PARTICIPANTS);
for (unsigned int i = 0U; i < MAXIMUM_PARTICIPANTS; ++i)
{
static const char prompt_text1[] = "nHow many pancakes did participant #";
static const char prompt_ending[] = " eat? ";
cout.write(prompt_text1, sizeof(prompt_text1) - 1U);
cout << i;
cout.write(prompt_ending, sizeof(prompt_ending) - 1U));
unsigned int quantity = 0U;
cin >> quantity;
pancake_quantities[i] = quantity;
}
unsigned int max_person_index = 0U;
unsigned int max_pancakes_eaten = 0U;
unsigned int min_person_index = 0U;
unsigned int min_pancakes_eaten = MAX_UINT;
for (unsigned int i = 0U; i < MAXIMUM_PARTICIPANTS; ++i)
{
const unsigned int quantity = pancake_quantities[i];
if (quantity > max_pancakes_eaten)
{
max_person_index = i;
max_pancakes_eaten = quantity;
}
if (quantity < min_pancakes_eaten)
{
min_person_index = i;
min_pancakes_eaten = quantity;
}
}
cout << "Person #" << max_person_index << " ate " << max_pancakes_eaten << "n";
cout << "Person #" << min_person_index << " ate " << min_pancakes_eaten << "n";
结构的向量
这个允许分类以找出最大值和最小值。
struct Pancake_Info
{
unsigned int id;
unsigned int quantity;
Pancake_Info() : id(0), quantity(0)
{}
bool operator < (const Pancake_Info& other)
{
return quantity < other.quantity;
}
};
typedef std::vector<Pancake_Info> Pancake_Container;
Pancake_Container pancake_quantities;
for (unsigned int i = 0U; i < MAXIMUM_PARTICIPANTS; ++i)
{
static const char prompt_text1[] = "nHow many pancakes did participant #";
static const char prompt_ending[] = " eat? ";
cout.write(prompt_text1, sizeof(prompt_text1) - 1U);
cout << i;
cout.write(prompt_ending, sizeof(prompt_ending) - 1U);
unsigned int quantity = 0U;
Pancake_Info p_i;
cin >> p_i.quantity;
p_i.id = i;
pancake_quantities.push_back(p_i);
}
std::sort(pancake_quantities.begin(), pancake_quantities.end());
cout << "Minimum of "
<< pancake_quantities[0].quantity
<< " eaten by person "
<< pancake_quantities[0].id
<< "n";
cout << "Maximum of "
<< pancake_quantities[MAXIMUM_PARTICIPANTS - 1].quantity
<< " eaten by person "
<< pancake_quantities[MAXIMUM_PARTICIPANTS - 1].id
<< "n";
我不明白为什么答案必须如此复杂。
使用vector
可以很容易地完成操作:
auto num_eaten = std::vector<int>(10);
for (auto i = 0u; i < num_eaten.size(); ++i) {
cout << "Enter number of hamburgers eaten by " << (i + 1) << "> ";
cin >> num_eaten[i];
}
auto min_it = std::min_element(num_eaten.begin(), num_eaten.end());
cout << (std::distance(num_eaten.begin(), min_it) + 1) << " ate the least"
<< endl;
auto max_it = std::max_element(num_eaten.begin(), num_eaten.end());
cout << (std::distance(num_eaten.begin(), max_it) + 1) << " ate the most"
<< endl;
或使用map
:
auto eaten = std::unordered_map<int, int>{};
for (auto i = 1; i < 10; ++i) {
cout << "Enter number of hamburgers eaten by " << i << "> ";
cin >> eaten[i];
}
auto min_it = std::min_element(
eaten.begin(), eaten.end(),
[](auto const &lhs, auto const &rhs) { return lhs.second < rhs.second; });
cout << min_it->first << " ate the least: " << min_it->second << endl;
auto max_it = std::min_element(
eaten.begin(), eaten.end(),
[](auto const &lhs, auto const &rhs) { return lhs.second > rhs.second; });
cout << max_it->first << " ate the most: " << max_it->second << endl;
您的评论正确。
修订:创建一个结构S1;
具有三个值 - 人索引,汉堡计数和煎饼计数。
填充并将S1放入向量
使用std ::对汉堡计数进行排序。 - 打印您想要的值。
使用std :: pancake count上的排序 - 打印所需的值。
较大的计数将在一端(另一端最小)。
修订版5/14/2015:
是时候回答您的问题了 - 如何制作较小。
您只需识别模式...并将其匹配到for循环。
模式想法1-个性序列为1至10;
带领我们到:
for (int personId = 1; personId <= 10; ++personId)
{
}
对于每个人,您的代码提示计数 - 煎饼,然后是汉堡包:
for (int personId = 1; personId <= 10; ++personId)
{
cout << "How many pancakes did p" << personId << " eat?" << endl;
int p1 = 0;
cin >> p1;
cout << "How many hamburgers did p" << personId << " eat?" << endl;
int p2 = 0;
cin >> p2;
// perhaps these should be joined into a single prompt to get 2 numbers
// example:
cout << "Enter pancake count, then hamburger count: " << endl;
int p1 = 0;
int p2 = 0;
cin >> p1 >> p2;
// error handling is somewhat easier in the 1st style.
}
确定哪个是最少的,或者我建议您将这些项目放入向量进行排序。
struct S1 {
int personId;
int pancakeCount;
int hamburgerCount;
// define ctor
S1(void) : personId(0), pancakeCount(0), hamburgerCount(0)
{
}
};
vector<S1> s1Vec;
for (int personId = 1; personId <= 10; ++personId)
{
cout << "How many pancakes did p" << personId << " eat?" << endl;
int p1 = 0;
cin >> p1;
cout << "How many hamburgers did p" << personId << " eat?" << endl;
int p2 = 0;
cin >> p2;
{
S1 s1; // declare an instance, and fill in
s1.personId = personId;
s1.pancackecount = p1;
s1.hamburgerCount = p2;
s1Vec.push_back(s1); // default cpy-ctor will capture into vec
} // THIS brace triggers the dtor of s1 ... currently does nothing,
// but sometimes the intermediate structs / classes need cleaning
}
// now you have all ids and counts captured to the vector
// sort s1Vec by pancakeCount, and
// print out s1Vec.begin() and .end()
// sort s1Vec by hamburgerCount, and
// print out s1.begin() and .end()
我建议您将显示方法添加到S1 ...
struct S1 {
int personId;
int pancakeCount;
int hamburgerCount;
// define ctor to initialize values
S1(void) : personId(0), pancakeCount(0), hamburgerCount(0)
{
}
std::string show(void)
{
stringstream ss;
ss << "person: " << personId
<< " p1: " << pancakeCount
<< " p2: " << hamburgerCount << endl;
return (ss.str());
}
std::string showP1(void)
{
stringstream ss;
cout << "person: " << personId
<< " p1: " << pancakeCount << endl;
return (ss.str());
}
std::string showP2(void)
{
stringstream ss;
cout << "person: " << personId
<< " p2: " << hamburgerCount << endl;
return (ss.str());
}
};
祝你好运。
这通过使用循环和结构来降低代码的大小。
您的代码可以被视为"展开"循环,这是您在性能问题上可能会考虑的。但是,嘿,用户I/O(提示和响应)永远不可能是高性能。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++中高效的大型稀疏块压缩线性方程
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 如何在C++中创建高效的多线程任务计划程序
- 我如何使该程序更小,更高效
- 使用const_iterator如何编译更高效的程序
- c++堆栈高效的多核应用程序