如何使这个程序更小,更高效?

时间:2015-05-13 23:39:04

标签: c++ processing-efficiency

我正在做一个像这样的练习题:

编写一个程序,要求用户输入吃过的汉堡包的数量 由十个不同的人(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;
}

3 个答案:

答案 0 :(得分:2)

煎饼数量向量

试试这个:

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";

答案 1 :(得分:0)

您的评论是正确的。

修订: 创建一个struct s1;

有三个值 - 人物指数,汉堡包数和煎饼数。

填写并将s1&s放入矢量

在汉堡包计数上使用std :: sort。 - 打印您想要的值。

在煎饼数上使用std :: sort - 打印你想要的值。

较大的计数将在一端(而最小的计数在另一端)。

2015年5月14日修订:

时间回答你的问题 - 如何缩小规模。

您只需识别模式...并将其与for循环匹配。

模式构思1 - personId序列是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添加一个show方法......

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());
    }

};
祝你好运。

这通过使用循环和结构减少了代码的大小。

您的代码可以被视为已展开的&#39;循环,这是您可能考虑的性能问题。但是,嗨,用户I / O(提示和响应)永远不会高性能。

答案 2 :(得分:0)

我不明白为什么答案必须如此复杂。

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;