题目1041:Simple Sorting
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1654
解决:630
-
题目描述:
-
You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.
-
输入:
-
For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.
-
输出:
-
For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.
-
样例输入:
-
6 8 8 7 3 7 7
-
样例输出:
-
3 7 8
-
来源:
- 2008年上海交通大学计算机研究生机试真题
-
注意:/********************************* * 日期:2013-2-19 * 作者:SJF0115 * 题号: 九度OJ 题目1041:Simple Sorting * 来源:http://ac.jobdu.com/problem.php?pid=1041 * 结果:AC * 来源:2008年上海交通大学计算机研究生机试真题 * 总结: **********************************/ #include<stdio.h> #include<string.h> #include<stdlib.h> //排序函数 int cmp(const void *a, const void *b) { return *((int*)a) > *((int*)b) ? 1: -1; } int Num[1001]; int key[1001]; int main() { int i,j,k,n,first; while(scanf("%d",&n) != EOF){ k = 0; first = 1; //输入数据 for(i = 0;i < n;i++){ scanf("%d",&Num[i]); } //排序 qsort(Num,n,sizeof(Num[0]),cmp); key[k] = Num[0]; //去掉重复数据 for(i = 0;i < n;i++){ if(key[k] != Num[i]){ key[++k] = Num[i]; } } //输出 for(i = 0;i <= k;i++){ //格式输出 if(first){ first = 0; } else{ printf(" "); } printf("%d",key[i]); } printf("\n"); } return 0; }
- 排序函数:
-
提交会Wrongint cmp(const void *a, const void *b){ return *(int *)a - *(int *)b; }
网友解释 : -
如果a=2147483647,b=-2;就会出现a-b>0,结果溢出了。所以使用return *(int *)a>*(int *)b?1:-1;就对了
测试数据:
2
2147483647 -2