【问题描述】最近超市要准备促销活动,为增加销量,超市决定将两种商品进行捆绑销售。现在需要你在已有的购物清单中找出来哪两种商品同时出现的次数最多。
【输入形式】第一行输入一个数N (1<=N<=100) ,表示现有购物清单的数量;随后的N行分别输入每个客户购买的商品(商品数固定为5),商品用大写字母表示。
【输出形式】输出需要捆鄉销售的两个商品。(按照ASCLL码的升序进行排列)
c++找出同时出现的次数最多的两个字母
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- |__WhoAmI__| 2022-12-14 22:27关注
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = 100; const int MAX_K = 5; int n; // Quantity of shopping lists char items[MAX_N][MAX_K]; // Shopping lists int counts[26][26]; // Counts of each pair of items int main() { // Read input cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < MAX_K; j++) cin >> items[i][j]; // Count occurrences of each pair of items memset(counts, 0, sizeof(counts)); for (int i = 0; i < n; i++) for (int j = 0; j < MAX_K; j++) for (int k = j + 1; k < MAX_K; k++) counts[items[i][j] - 'A'][items[i][k] - 'A']++; // Find most frequent pair of items int max_count = 0; char item1 = 'A', item2 = 'B'; for (int i = 0; i < 26; i++) for (int j = i + 1; j < 26; j++) if (counts[i][j] > max_count) { max_count = counts[i][j]; item1 = i + 'A'; item2 = j + 'A'; } // Output result cout << item1 << item2 << endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥20 神经网络Sequential name=sequential, built=False
- ¥16 Qphython 用xlrd读取excel报错
- ¥15 单片机学习顺序问题!!
- ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
- ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
- ¥15 相敏解调 matlab
- ¥15 求lingo代码和思路
- ¥15 公交车和无人机协同运输
- ¥15 stm32代码移植没反应
- ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效