卡牌游戏:随机抽出了 N 张卡片,数出这 N 张卡片上每种数字(长度1到5位之间)各出现了多少次。例如,有5张卡片:15、22、17、82、5。 就会输出: 1 2 2 3 5 2 7 1 8 1
现在我们为了更直观表示各个数字的多少,如果某个数字出现K次,就打印K个此数字。上面的例子输出为: 11 222 55 7 8
//数随机数各位数出现次数-卡片游戏
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int number,a[10],n,s;
memset(a,0,sizeof(a));//初始化数组为0
cout<<"Please enter the counts of random number and random numbers:";//提醒用户输入要产生随机数数量及随机数
cin>>n; for(int i=0;i<n;i++){
cin>>number;
while(number&&number<100000){
s=number%10;
a[s]++;
number/=10; } }
//输出结果
for(int i=0;i<10;i++){
while(a[i]){
cout<<i;
a[i]--;
if(!a[i])cout<<" "; }
``` } return 0;}