码迷,mamicode.com
首页 > 其他好文 > 详细

2016华为上机题二(数字频率)

时间:2015-09-29 22:07:40      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

2.统计数字字符串中数字的出现频率,输入字符串最多有255个,字符串中只有数字字符,没有其它字符。统计其中出现次数最多的字符,当出现次数相当时,则输出较小的字符。输出格式为:字符+,+次数。
例如
412444
4,3

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cmath>
 4 #include <string>
 5 #include <algorithm>
 6 #include <memory.h>
 7 
 8 using namespace std;
 9 
10 const double PI = acos(-1.0);
11 const double eps = 1e-6;
12 const double INF = 1e9 + 7;
13 
14 
15 
16 int a[10];
17 
18 
19 int main()
20 {
21     freopen("data.in", "r", stdin);
22 
23     
24     memset(a, 0, 10);
25     string str;
26     int maxNum = 0;
27 
28     cin >> str;
29 
30 
31     for (int i = 0; i < str.size(); i++)
32     {
33         a[str[i] - 0]++;
34         if (a[str[i] - 0]>maxNum)
35         {
36             maxNum = a[str[i] - 0];
37         }
38     }
39     for (int i = 0; i < 10; i++)
40     {
41         if (a[i] == maxNum)
42         {
43             cout << i<<","<<maxNum<< endl;
44             break;
45 
46         }
47 
48     }
49 
50 
51     system("pause");
52     return 0;
53 }

 

2016华为上机题二(数字频率)

标签:

原文地址:http://www.cnblogs.com/lishichao/p/4847210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!