PAT B brush road title of 1064 the number of friends (20 points)

1064 number of friends (20 points)

If two integer numbers and everybody is the same, it is called the "number of friends", and that the public and that their "friend card number." 51 and 123, for example, is the number of friends, because 1 + 2 + 3 = 5 + 1 = 6, and 6 is their friends card number. Given some of the integer requiring you statistics about how many different license number of friends they have.

Input format:
input of the first row is given a positive integer N. Then given N positive integers line, separated by a space between the numbers. Title ensure that all numbers less than 10 ^ 4.

Output Format:
First, a first output line given the number of different digital certificate number of friends; friends card number and then outputs a row in ascending order, a digital interval space, and the end of the row may not have extra space.

Sample input:
. 8
1,238,995,199,827,333,612

Output Sample:
. 4
. 3. 6. 9 26 is

#include <iostream> 
#include <algorithm>

using namespace std;

int main()
{
	int N;
	cin>>N;
	int i;
	char A[N][5];
	for(i=0;i<N;i++){
		cin>>A[i];
	}
	int j,sum[N]={0};
	int cnt[37]={0};
	for(i=0;i<N;i++){
		for(j=0;A[i][j]!='\0';j++){
			sum[i]+=A[i][j]-'0';
		}
	}
	sort(sum,sum+N);
	for(i=0;i<N;i++){
		cnt[sum[i]]++;
	}
	int count=0;
    //因为不超过10的4次方故最大也是36
	for(i=0;i<37;i++){
		if(cnt[i]>0){
			count++;
		}
	}
	cout<<count<<endl;
	for(i=0;i<37;i++){
		if(cnt[i]>0){
			cout<<i;
			if(i<sum[N-1]){
				cout<<" ";
			}
		}
	}
	return 0;
}
Published 73 original articles · won praise 0 · Views 524

Guess you like

Origin blog.csdn.net/derbi123123/article/details/103827994