B--Faultfinding

B--Faultfinding

时间: 1ms        内存:128M

描述:

Do you remember the game in which we find difference among several similar pictures? Now we change it into digital version. There are N digits, same or different. Please find how many different digits there are among them and output the number.

输入:

Each group of the first line is N (1<=N<=10000). The second line consists N integers.

输出:

The number of different digits.

示例输入:

2
1 1
3 
1 2 3

示例输出:

1
3

提示:

参考答案(内存最优[1092]):

#include <stdio.h>
int main()
{
    int s,a[10000],n=1,i,j;
    while (~scanf("%d",&s)&&s)
    {

        for (i=0; i<s; i++)scanf("%d",a+i);
        for (i=1; i<s; i++)
            for (j=i-1; j>=0; j--)
            {
                if (a[j]==a[i])break;
                if (j==0)n++;
            }
        printf("%d\n",n);
    }
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>

using namespace std;

int main()
{
    int N;
    int a[10000],b[10000];
    int num=1,different;
    while(cin>>N)
    {
        for(int i=0;i<N;i++)
        {
            cin>>a[i];
        }
        b[0]=a[0];
        num=1;
        for(int i=1;i<N;i++)
        {
             different=0;
             for(int j=0;j<num;j++)
             {
                 if(a[i]!=b[j])
                    ++different;
             }
             if(different==num)
             {
                 b[num]=a[i];
                 num++;
             }
        }
        cout<<num<<endl;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注