『代码』··By/蜜汁炒酸奶

统计输入整形数中每个数出现的次数-C++数组的简单操作

效果图: 现在输入这十个数,注意要递增 1 2 2 2 2 3 4 4 5 5 1出现1次 2出现4次 3出现1次 4出现2次 5出现2次 请按任意键继续…

/*
功能:统计输入整形数中每个数出现的次数-C++数组的简单操作
日期:2013-09-12
*/

#include<iostream>
using namespace std;

int main(void)
{
      int arr[10];
	  int num = 0;
      int i,j;

	  cout << "现在输入这十个数,注意要递增" <<endl;

	  for (i=0;i<10;i++)
	  {
           cin >> arr[i];
	  }

      for (i=0;i<10;i=j)
      {
		  for (j=i;j<10;j++)
		  {
			  if (arr[i]==arr[j])
			  {
				  num++;
				  if (9==j)
				  {
                         cout<<arr[i]<<"出现"<<num<<"次"<<endl;
				  }
			  }
			  else
			  {
				  cout<<arr[i]<<"出现"<<num<<"次"<<endl;
				  num = 0;
				  break;
			  }
		  }

      }

      system("pause");
	  return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

预览
Loading comments...
1 条评论
  • W

    这个也是c++

example
预览