TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/************************************************************************/
- /*
- 24. 统计一个单位职工的年龄,要求把相同年龄最多的那个年龄找出来
- (可能有几个这样的年龄),并统计出现的次数。
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- struct ZG
- {
- int age;
- int times;
- };
-
- void main()
- {
- int arr[20]={0}; // 职工年龄
- bool iss[20]={0};
- struct ZG Szg[20]={0};
- int p=0;
- for (int i=0;i<20;i++)
- {
- arr=rand()%30; // 给所有职工年龄赋值
- }
- for (int i=0;i<20;i++)
- {
- printf("%5d",arr); // 输出
- }
- printf("\n");
- for (int i=0;i<20;i++)
- {
- if (iss==1) // iss表示已经处理的数字
- {
- continue;
- }
- int num=0;
- for(int j=0;j<20;j++) // 查找所有和i相同的年龄
- {
- if (arr==arr[j])
- {
-
-
- num++;
- iss[j]=1;
- }
- }
- Szg[p].age=arr; // 保存年龄
- Szg[p].times=num;
- p++;
-
- }
- int maxtime=0;
- for (int j=0;j<p;j++) // 查找最大次数
- {
- if (maxtime<Szg[j].times)
- {
- maxtime=Szg[j].times;
- }
- }
- for (int j=0;j<p;j++) // 打印最大次数
- {
- if (maxtime==Szg[j].times)
- {
- printf("年龄为:%d 次数为%d\n",Szg[j].age,maxtime);
- }
- }
-
- system("pause");
- }</pre><br><br><br></pre>
|
|