其他分享
首页 > 其他分享> > PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) 凌宸1642

PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) 凌宸1642

作者:互联网

PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) 凌宸1642

题目描述:

Two integers are called "friend numbers" if they share the same sum of their digits, and the sum is their "friend ID". For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID's among them.

译:如果两个整数共享相同的数字总和,则称为“朋友号码”,该总和就是他们的“朋友 ID”。 例如,123 和 51 是朋友编号,因为 1+2+3 = 5+1 = 6,而 6 是他们的朋友 ID。 给定一些数字,您应该计算其中不同朋友 ID 的数量!


Input Specification (输入说明):

Each input file contains one test case. For each case, the first line gives a positive integer N. Then N positive integers are given in the next line, separated by spaces. All the numbers are less than 104.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数 N。然后在下一行给出 N 个正整数,用空格分隔。 所有的数字都小于 104


output Specification (输出说明):

For each case, print in the first line the number of different friend ID's among the given integers. Then in the second line, output the friend ID's in increasing order. The numbers must be separated by exactly one space and there must be no extra space at the end of the line.

译:对于每种情况,在第一行打印给定整数中不同朋友 ID 的数量。 然后在第二行,按升序输出好友 ID。 数字之间必须正好有一个空格分隔,并且行尾不能有多余的空格。


Sample Input (样例输入):

8
123 899 51 998 27 33 36 12

Sample Output (样例输出):

4
3 6 9 26

The Idea:

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int n , t , te , ans = 0 ;
bool mp[40] ;
int sumOfDigits(int x){
	int res = 0 ;
	while(x){
		res += x % 10 ;
		x /= 10 ;
	}
	return res ;
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i ++){
		cin >> t ;
		te = sumOfDigits(t) ;
		if(!mp[te]) ans ++ ;
		mp[te] = true ;
	}
	cout << ans << endl ;
	for(int i = 0 , j = 0 ; i < 40 ; i ++){
		if(mp[i]) {
			if(j == 0) cout << i ;
			else cout << ' ' << i ;
			j ++ ;
		}
	}
	return 0 ;
}

标签:PAT,Level,int,1642,朋友,numbers,line,ID,friend
来源: https://www.cnblogs.com/lingchen1642/p/15139009.html