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.
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
代码如下:
#include <iostream> using namespace std; int main() { int N; while (cin>>N) { int a[10000],n=1,i,j; for (i=0;i<N;i++) cin>>a[i]; for (i=1;i<N;i++) { for (j=i-1;j>=0;j--) { if (a[j]==a[i]) break; if (j==0) n++; } } cout<<n<<endl; } return 0; }
运行结果:
学习心得:
精英赛第二题,想的时间不长,但是实现起来还是有些困难的,本来的思路是以第一个正向去比较,如果碰到不同的就加1,再依次以后面的数去比较其之后的数,如果相同就减一,但是结果好像就是不对,修改了很久还是没通过,想睡觉的时候灵感来了,既然正向不行就反向去比较如果从第二个开始往前面比较,如果这个数之前出现过,就直接跳出循环,如果到第一位结束后还没有碰到那么就n++。。。有时候的确得从多个方向去思考问题啊。
原文地址:http://blog.csdn.net/liuchang54/article/details/44935575