题解 | #明明的随机数#
明明的随机数
http://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
#spend 3 hours,写的我自己都不知道怎么通过的,有懂的大佬帮忙解释一下。
int main() {
int n;
int i, j, k = 0;
int rand_n[1000] = {0};
int rand_n1[1000] = {0};
int temp = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &rand_n[i]);
if (i >= 0) {
for (j = 0; j < i; j++) {
if (rand_n[j] == rand_n[i]) {
break;
}
}
}
if (j == i) {
rand_n1[k++] = rand_n[i];
}
}
for (i = 0; i < k; i++) {//冒泡排序这部分需要好好看一下
for (j = 0; j < k - i; j++) {
if (rand_n1[j] >
rand_n1[j+1]) { //不用新变量和实现两数交换
// rand_n1[j+1] = rand_n1[j] + rand_n1[j+1];
//rand_n1[j] = rand_n1[j+1] - rand_n1[j];
// rand_n1[j+1] = rand_n1[j+1] - rand_n1[j];
temp = rand_n1[j+1];
rand_n1[j+1] = rand_n1[j];
rand_n1[j] = temp ;
}
}
}
for (i = 1; i <= k; i++) {
printf("%d\n", rand_n1[i]);//**?这块我真没理解为啥**
}
}