HDOJ - 1004 - Let the Balloon Rise(字符串)
来源:互联网 发布:淘宝卖家开通直播条件 编辑:程序博客网 时间:2024/05/01 19:52
方法1:边输入边查找,存在次数加1,不存在插入这个颜色。
AC代码
#include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <ctime>using namespace std; const int maxn = 1000+10;struct Ballon{char color[20];int t;};Ballon ballon[maxn];int n = 0, m = 0;void Search(char s[]){int i = 0;for (i = 0; i < m; i++){if (!strcmp(s, ballon[i].color)){ballon[i].t++;return;}}strcpy(ballon[m].color, s);ballon[m].t = 1;m++;return ;}int main(){#ifdef Local freopen("a.in", "r", stdin); #endifint i = 0;while (cin >> n && n){memset(ballon, '\0', sizeof(ballon[0]));m = 0;int max = 0, num = 0;while (n--){char s[20];cin >> s;Search(s);}for (i = 0; i < m; i++){if (ballon[i].t > max){max = ballon[i].t, num = i;}}cout << ballon[num].color << endl;}return 0;}
方法2:快排后找出现次数最多的。
(未通过的)
#include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <ctime>using namespace std; char ballon[1000][20];int cmp (const void *a, const void *b){return *(char *)a - *(char *)b;}int main(){#ifdef Local freopen("a.in", "r", stdin); #endifint n = 0, i = 0;while (cin >> n && n){char ans[20];int count = 1, max = 0;memset(ballon, '\0', sizeof(ballon[0]));for (i = 0; i < n; i++)cin >> ballon[i];qsort(ballon, n, sizeof(ballon[0]), cmp);for (i = 0; i < n; i++){if (!strcmp(ballon[i], ballon[i+1]))count++;else{if (count > max){max = count;strcpy(ans, ballon[i]);}count = 1;}}cout << ans << endl;}}
0 0
- HDOJ 1004 Let the Balloon Rise(字符串)
- HDOJ - 1004 - Let the Balloon Rise(字符串)
- hdoj 1004 Let the Balloon Rise(字符串比较问题)
- hdoj 1004 Let the balloon rise (字符串处理问题)
- [字符串][第二阶段-字符串处理][HDOJ-1004]Let the Balloon Rise
- HDOJ 1004 Let the Balloon Rise (map)
- HDOJ-1004 Let the Balloon Rise
- HDOJ 1004 Let the Balloon Rise
- HDOJ 1004 Let the Balloon Rise
- hdoj 1004 Let the Balloon Rise
- hdoj.1004 Let the Balloon Rise 20140726
- HDOJ Let the Balloon Rise 1004
- HDOJ 1004 Let the Balloon Rise
- HDOJ 1004 Let the Balloon Rise
- HDOJ 1004 Let the Balloon Rise(水题)
- 【HDOJ】【1004】Let the Balloon Rise
- HDOJ 1004 Let the Balloon Rise
- HDoj 1004 Let the Balloon Rise
- 进程隐藏与进程保护(SSDT Hook 实现)(一)
- Spring事务的传播行为和隔离级别
- DirectX10一向量代数(一)
- 第一个Servlet程序
- 对HelloWrod的认识
- HDOJ - 1004 - Let the Balloon Rise(字符串)
- WMD驱动安装两种方法
- 《自己动手写操作系统》 第三章 pmtest6源码解析——开启分页机制
- ubuntu12.04——温度测量
- 四、Linux常用命令之文件操作篇(cat、more、head、tail、ln、chmod、chown、chgrp、umask)
- 两台电脑共用键盘鼠标
- leetcode Integer to Roman
- Date、Calendar和StringBuffer的综合使用
- Autorelease的疑问