关于小米(OJ)#5 的疑问,代码在自己电脑上正常输出,在OJ上不行,求大佬解答!!
小米OJ:code.#5 问题描述:[找出旋转有序数列的中间值]
------------------
给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2]。
假定数列中无重复元素,且数列长度为奇数。 求出旋转数列的中间值。如数列[4,5,6,7,0,1,2]的中间值为4。
INPUT: 4,5,6,7,0,1,2 OUTPUT: 4
输入样例:
1
1,2,3
4,5,6,7,0,1,2
12,13,14,5,6,7,8,9,10
输出样例:
1
2
4
9
------------------
我的答案: (main.c) [OJ 提示错误原因:运行超时]
------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FLAG int
#define MAX 100
/* clear array */
void clear(int *A, int len){
int i = 0;
for(i; i < len ; i++)
A[i] = 0;
}
/* merge to a complete int number */
int mergeint(int first, char second){
int temp = first * 10;
temp += (second - '0');
return temp;
}
void selectionSort(int *A, int len){
int min_index = 0;
for(int i = 0; i < len - 1 ; i++){
min_index = i;
for(int j = i + 1; j < len ; j++)
if (A[j] < A[min_index])
min_index = j;
/* swap A[i] and A[min_index] */
if (min_index != i){ /* Occur An Error Here without IF() ,at first time... */
A[min_index] = A[i] + A[min_index];
A[i] = A[min_index] - A[i];
A[min_index] = A[min_index] - A[i];
}
}
}
int main() {
int A[MAX] = {0};
int index = 0, len = 0;
char c = getchar();
FLAG new = 0;
while(c != EOF) {
while (c != '\n') {
if (c == ',') {
c = getchar();
new = 1;
continue;
} else {
if (c >= '0' && c <= '9') {
if (new) index++;
A[index] = mergeint(A[index], c);
new = 0;
}
c = getchar();
}
}
selectionSort(&A, index + 1);
printf("%d\n", A[(index + 1)/2]);
/* clear */
clear(&A, index+1);
index = 0;
new = 0;
/* new */
c = getchar();
}
return 0;
}
------------------