DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C++ 數字統計

11.13.2008
| 1045 views |
  • submit to reddit
        // description of your code here

#include <iostream>
using namespace std;

void main()
{
	int in[3], i, cnt[10] = {0}, re;
	printf("輸入三個6個位數的正整數(空白區隔)");
	for(i=0; i<3; i++)
		cin>>in[i];

	for(i=0; i<3; i++)
	{
		while(in[i])
		{
			re = in[i] % 10;
			in[i] = in[i] / 10;
			switch(re)
			{
			case 0:
				cnt[0]++;
				break;
			case 1:
				cnt[1]++;
				break;
			case 2:
				cnt[2]++;
				break;
			case 3:
				cnt[3]++;
				break;
			case 4:
				cnt[4]++;
				break;
			case 5:
				cnt[5]++;
				break;
			case 6:
				cnt[6]++;
				break;
			case 7:
				cnt[7]++;
				break;
			case 8:
				cnt[8]++;
				break;
			case 9:
				cnt[9]++;
				break;
			}
		}
	}

	cout<<"此三個輸入所統計的各個數字出現次數為下\n";
	for(i=0; i<10; i++)
		cout<<"數字"<<i<<">"<<cnt[i]<<"次\n";

	system("pause");
}