在 C++ 中顯示餐館的食品訂單表


假設我們有一個數組 orders,它代表顧客在餐廳下的訂單。因此,orders[i]=[cust_namei, table_numi, food_itemi],其中 cust_namei 是顧客姓名,table_numi 是顧客的桌號,food_itemi 是顧客訂購的菜品。

我們必須返回餐廳的“顯示錶”。這裡的“顯示錶”是一個表格,其行條目表示每張桌子訂購了多少種食品。第一列將是桌號,其餘列對應於按字母順序排列的每種食品。第一行應該是標題,其第一列為“桌子”,後跟食品名稱。

因此,如果輸入類似於 orders = [["Amal","3","Paratha"],["Bimal","10","Biryni"],["Amal","3","Fried Chicken"],["Raktim","5","Water"],["Raktim","5","Paratha"],["Deepak","3","Paratha"]],則輸出將為 [["Table","Biryni","Fried Chicken","Paratha","Water"],["3","0","1","2","0"],["5","0","0","1","1"],["10","1","0","0","0"]]

為了解決這個問題,我們將遵循以下步驟:

  • 定義一個對映 m

  • 定義一個集合 names

  • 定義一個集合 t

  • 對於訂單列表中的每個元素 it

    • 將 it[1] 插入到 t 中

    • ok := true

    • 如果 ok 為 false,則:

      • v = 使用空格分割 it[2] 後的陣列

      • 對於 v 中的每個元素 x,執行:

        • (將 m[it[1], x] 增加 1)

        • 將 x 插入到 names 中

    • 否則

      • (將 m[it[1], it[2]] 增加 1)

      • 將 it[2] 插入到 names 中

  • 定義一個二維陣列 ret

  • 定義一個數組 temp 並從 names 中複製元素

  • 將 "Table" 作為第一個元素插入到 temp 中

  • 將 temp 插入到 ret 的末尾

  • 對於 t 中的每個元素 it,執行:

    • 定義一個數組 te

    • 將 it 插入到 te 的末尾

    • 對於 names 中的每個元素 x,執行:

      • 將 m[it, x] 作為字串插入到 te 的末尾

    • 將 te 插入到 ret 的末尾

  • 對陣列 ret 進行排序

  • 返回 ret

示例

讓我們看看下面的實現來更好地理解:

線上演示

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<vector<string> > v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << "[";
      for(int j = 0; j <v[i].size(); j++){
         cout << v[i][j] << ", ";
      }
      cout << "],";
   }
   cout << "]"<<endl;
}
typedef long long int lli;
class Solution {
public:
   vector <string> split(string& s, char delimiter){
      vector <string> tokens;
      string token;
      istringstream tokenStream(s);
      while(getline(tokenStream, token, delimiter)){
         tokens.push_back(token);
      }
      return tokens;
   }
   static bool cmp(vector <string>& a, vector <string>& b){
      lli an = stol(a[0]);
      lli bn = stol(b[0]);
      return an < bn;
   }
   vector<vector<string>> displayTable(vector<vector<string>>& o) {
      map <string, map < string, int> >m;
      set <string> names;
      set <string> t;
      for(auto &it : o){
         t.insert(it[1]);
         bool ok = true;
         if(!ok){
            vector <string> v = split(it[2], ' ');
            for(auto& x : v){
               m[it[1]][x]++;
               names.insert(x);
            }
         }
         else{
            m[it[1]][it[2]]++;
            names.insert(it[2]);
         }
      }
      vector < vector <string> > ret;
      vector <string> temp(names.begin(), names.end());
      temp.insert(temp.begin(), "Table");
      ret.push_back(temp);
      for(auto& it : t){
         vector <string> te;
         te.push_back(it);
         for(auto& x : names){
            te.push_back(to_string(m[it][x]));
         }
         ret.push_back(te);
      }
      sort(ret.begin() + 1, ret.end(), cmp);
      return ret;
   }
};
main(){
   Solution ob;
   vector<vector<string>> v = {{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried
Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}};
   print_vector(ob.displayTable(v));
}

輸入

{{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}

輸出

[[Table, Biryni, Fried Chicken, Paratha, Water, ],[3, 0, 1, 2, 0, ],[5, 0, 0, 1, 1, ],[10, 1, 0, 0, 0, ],]

更新於:2020年11月17日

295 次檢視

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.