【问题标题】:How do I make this program smaller, more efficient? [closed]如何使这个程序更小、更高效? [关闭]
【发布时间】:2015-07-25 10:59:40
【问题描述】:

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

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

【问题讨论】:

  • 使用p1p10的数组,然后使用for循环查找最高和最低的元素。
  • herehere
  • 使用for 循环作为输入。
  • 由于代码审查,我投票结束这个问题作为题外话。
  • 我会给你一些建议,让你发布一些看起来至少有效并显示出逻辑思维的东西。

标签: c++ processing-efficiency


【解决方案1】:

薄饼数量向量

试试这个:

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

【讨论】:

    【解决方案2】:

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

    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;
    

    【讨论】:

      【解决方案3】:

      你的评论是正确的。

      修订: 创建一个结构 s1;

      具有三个值 - 人员索引、汉堡包计数和煎饼计数。

      将 s1 填充并放入向量中

      在汉堡计数上使用 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());
          }
      
      };
      

      祝你好运。

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

      您的代码可以被视为“展开”循环,您可能会考虑将其用于性能问题。但是,嘿,用户 I/O(提示和响应)永远不会高性能。

      【讨论】:

      • 您的解决方案失去了人(索引)与吃的煎饼或汉堡包数量之间的相关性。这就是我没有对答案进行排序的原因。
      • @ThomasMatthews - 所以我在这里添加了关于如何减少代码大小以及为什么不应该为“性能问题”而烦恼的讨论。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      相关资源
      最近更新 更多