[Asm] 纯文本查看 复制代码
/*
/*23. 有10个两位整数,把这些数作以下变化,如果它是素数,则把它乘以2,若它是偶数则除以2,其余的数减1,请将变化后的10个数按从小到大的次序打印出来。
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int isit(int num);
void quicksort(int *a, int start, int back);
void show(int *a, int length);
void main() {
int a[10] = { 98,23,56,43,51,15,88,14,77,11 };
for (int i = 0; i < 10; i++) {
if (a[i] % 2 == 0) {
a[i] = a[i] / 2;
}
else if (isit(a[i])) {
a[i] = 2 * a[i];
}
else {
a[i] = a[i] - 1;
}
}
show(a,10);
quicksort(a, 0,9);
show(a, 10);
getchar();
}
int isit(int num) {
int flag = 1;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
flag = 0;
break;
}
}
return flag;
}
void show(int *a, int length) {
for (int i = 0; i < length; i++) {
printf("%d,", a[i]);
}
printf("\n-------\n");
}
void quicksort(int *a, int start, int back) {
printf("back=%d\n", back);
int j = start; //这一步很重要啊,不是0
if (start < back) {
for (int i = start + 1; i <= back; i++) {
if (a[i]<a[start]) {
j = j + 1;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int temp = a[j];
a[j] = a[start];
a[start] = temp;
quicksort(a, start, j - 1);
quicksort(a, j + 1, back);
}
} |