HDOJ 1004 Let the Balloon Rise

来源:互联网 发布:淘宝宝箱在哪里 编辑:程序博客网 时间:2024/05/19 18:46

最近重新开始了刷题的道路,也开始搭建自己的博客,第一次写,写的不好多多见谅。


问题描述

题目比较简单,就是统计字符串的次数

算法

不需要什么特殊的算法,就是人工统计算法的代码实现,具体思想:每一次读取新的字符串时从之前所有的字符串里面进行搜索,如果已经存在,则该字符串次数加一,最后将次数最多的字符串输出即可

数据结构

一个二维数组保存字符串,一个一维数组保存出现次数

AC代码如下:

include <iostream>include <string.h>using namespace std;int main() {    int n;    char ballon[1000][16];    int count[1000];    //记录气球的次数    char temp[16];    while (1){        cin >> n;        if (n==0)            break;        memset(count,0,1000);        cin >> temp;        strcpy(ballon[0] , temp);        count[0] = 1;        for (int i = 1; i <n ; ++i) {            cin >> temp;            strcpy(ballon[i],temp);            for (int j = 0; j <i ; ++j) {                if (strcmp(ballon[j],temp)==0)                    count[i]++;            }        }        int max=0;    //保存最大的次数        for (int k = 0; k <n ; ++k) {            if (max < count[k])                max = count[k];        }        int t;  //找到最大次数的索引,并将原数组中的字符串输出        for (int i=0; i<n; i++){            if (count[i]==max)                t = i;        }        cout << ballon[t] << endl;    }    }

反思:学会掌握思路,弄清每一个变量的意义还有边界,Debug、Debug再Debug,最后就是不停的测试啦,大家加油!

1 0
原创粉丝点击