zoukankan      html  css  js  c++  java
  • PAT A1120 Friend Numbers (20 分)——set

    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 frind ID's among them.

    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​​.

    Output Specification:

    For each case, print in the first line the number of different frind 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.

    Sample Input:

    8
    123 899 51 998 27 33 36 12
    

    Sample Output:

    4
    3 6 9 26
    
     
     1 #include <stdio.h>
     2 #include <set>
     3 using namespace std;
     4 int main(){
     5   int n;
     6   scanf("%d",&n);
     7   set<int> s;
     8   for(int i=0;i<n;i++){
     9     int tmp;
    10     scanf("%d",&tmp);
    11     int num=0;
    12     while(tmp!=0){
    13       num+=tmp%10;
    14       tmp/=10;
    15       }
    16     s.insert(num);
    17   }
    18   int cnt=0;
    19   printf("%d
    ",s.size());
    20   for(auto it=s.begin();it!=s.end();it++){
    21     printf("%d",*it);
    22     cnt++;
    23     if(cnt!=s.size())printf(" ");
    24   }
    25 }
    View Code

    注意点:用set很方便,但set的指针迭代器只有加法操作没有减法操作,所以判断输出空格不能靠迭代器指针,还是要另外加一个变量

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    虚拟PC上网设置
    打造无线AP
    ftp密码修改
    桌面
    红蜘蛛
    C++ Zip压缩解压缩[支持递归压缩]
    C++通过访问注册表获取已安装软件信息列表
    IOS Simulator Create Keyboard Shortcut
    简单的silverlight切图程序
    silverlight 反射调用WebService
  • 原文地址:https://www.cnblogs.com/tccbj/p/10433620.html
Copyright © 2011-2022 走看看