首页
学习
活动
专区
圈层
工具
发布
29 篇文章
1
PAT (Basic Level) Practice (中文)1047 编程团体赛
2
PAT (Basic Level) Practice (中文)1083 是否存在相等的差
3
PAT (Basic Level) Practice (中文)1082 射击比赛
4
PAT (Basic Level) Practice (中文)1081 检查密码
5
PAT (Basic Level) Practice (中文)1077 互评成绩计算
6
PAT (Basic Level) Practice (中文)1076 Wifi密码
7
PAT (Basic Level) Practice (中文)1064 朋友数
8
PAT (Basic Level) Practice (中文)1063 计算谱半径
9
PAT (Basic Level) Practice (中文)1057 数零壹
10
PAT (Basic Level) Practice (中文)1056 组合数的和
11
PAT (Basic Level) Practice (中文)1042 字符统计
12
PAT (Basic Level) Practice (中文)1041 考试座位号
13
PAT (Basic Level) Practice (中文)1023 组个最小数
14
PAT (Basic Level) Practice (中文)1022 D进制的A+B
15
PAT (Basic Level) Practice (中文)1019 数字黑洞
16
PAT (Basic Level) Practice (中文)1007 素数对猜想
17
PAT (Basic Level) Practice (中文)1091 N-自守数
18
PAT (Basic Level) Practice (中文)1026 程序运行时间
19
PAT (Basic Level) Practice (中文)1061 判断题
20
PAT (Basic Level) Practice (中文)1086 就不告诉你
21
PAT (Basic Level) Practice (中文)1016 部分A+B
22
PAT (Basic Level) Practice (中文)1012 数字分类
23
PAT (Basic Level) Practice (中文)1013 数素数
24
PAT (Basic Level) Practice (中文)1011 A+B 和 C
25
PAT (Basic Level) Practice (中文)1009 说反话
26
PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题
27
PAT (Basic Level) Practice (中文)1006 换个格式输出整数
28
PAT (Basic Level) Practice (中文)1004 成绩排名
29
PAT (Basic Level) Practice (中文)1002 写出这个数

PAT (Basic Level) Practice (中文)1064 朋友数

1064 朋友数

如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”。例如 123 和 51 就是朋友数,因为 1+2+3 = 5+1 = 6,而 6 就是它们的朋友证号。给定一些整数,要求你统计一下它们中有多少个不同的朋友证号。

输入格式:

输入第一行给出正整数 N。随后一行给出 N 个正整数,数字间以空格分隔。题目保证所有数字小于 104。

输出格式:

首先第一行输出给定数字中不同的朋友证号的个数;随后一行按递增顺序输出这些朋友证号,数字间隔一个空格,且行末不得有多余空格。

输入样例:

8 123 899 51 998 27 33 36 12

输出样例:

4 3 6 9 26

代码:

代码语言:javascript
复制
#include<stdio.h>

int fun1(int n)   //对输入的arr[i].n各位数求和
{
    int sum=0;
    while(n)
    {
        sum+=(n%10);
        n/=10;
    }
    return sum;
}

int fun2(int *temparr1,int *temparr2,int t)  //去重
{
    int i,j;
    int *temp;
    int ll=0;
    temp=(int*)malloc(t*sizeof(int));
    for(i=0;i<t;i++)
        temp[i]=0;
    int l;
    for(i=0;i<t-1;i++)
    {
        for(j=i+1;j<t;j++)
        {
           if(temp[i]==0&&temparr1[j]==temparr1[i]){
            temp[j]=1;
           }
        }
    }
    for(i=0;i<t;i++)
    {
        if(temp[i]==0) temparr2[ll++]=temparr1[i];
    }
    return ll;
}

void fun3(int * temparr2,int n)  //排序
{
    int i,j;
    int index,temp;
    for(i=0;i<n-1;i++)
    {
        index=i;
        temp=temparr2[i];
        for(j=i+1;j<n;j++)
        {
            if(temparr2[j]<=temparr2[index]) index=j;
        }
       temparr2[i]=temparr2[index];
       temparr2[index]=temp;
    }
}
struct num
{
    int n;
};
struct num arr[10001];
int main()
{
    int N;
    scanf("%d",&N);
    int i;
    int temparr1[10001];
    int temparr2[10001];
    int t=0;
    for(i=0;i<N;i++)
    {
        scanf("%d",&arr[i].n);
        int temp=fun1(arr[i].n);
        temparr1[t++]=temp;
    }
    int temp=fun2(&temparr1,&temparr2,t);
    fun3(&temparr2,temp);
    printf("%d\n",temp);
    for(i=0;i<temp;i++)
    {
        if(i==temp-1) printf("%d\n",temparr2[i]);
        else printf("%d ",temparr2[i]);
    }
    return 0;
}
下一篇
举报
领券