| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

问题点数：0 回复次数：1

#5 问题描述：[找出旋转有序数列的中间值]
------------------

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
------------------

------------------
#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;
}
------------------

• 2
• 1/1页
• 1