无C不欢

2015 5.9 星期六 晴


Function:将多个空格用一个空格替代



int c,flag; //The answer of me
while((c = getchar()) != EOF) {
if(c != ' ')
if(flag == 0)
putchar(c);
else {
putchar(c);
flag = 0;
}
if( c == ' ')
if(flag == 0) {
putchar(c);
flag = 1;
}
}


int c,lastc; //The answer of answer
while((c = getchar()) != EOF) {
if(c != ' ')
putchar(c);
if( c == ' ')
if(lastc != ' ') // else if(lastc != ' ')
putchar(c);
lastc = c;
}

Function: 换行输出单词



int c,lastc,state;
state = OUT;
while((c = getchar()) != EOF) {
if(c == ' ' || c == '\t' || c == '\n') {
if(state == IN){
putchar('\n');
state = OUT;
}
}
else if(state == IN)
putchar(c);
else if(state == OUT) {
putchar(c);
state = IN;
}
}


int c,lastc,state;
state = OUT;
while((c = getchar()) != EOF) {
if(c == ' ' || c == '\t' || c == '\n') {
if(state == IN){
putchar('\n');
state = OUT;
}
}
else if(state == OUT) {
putchar(c);
state = IN;
}
else
putchar(c);
}

2015 5.13 星期三 晴


Function: quick sort


include

include

define MAX 1000

int qsor[1000];
int main()
{
int T,n;
void qsort(int [], int, int);
scanf("%d",&T);
while(T > 0)
{
int i;
scanf("%d",&n);
memset(qsor, 0, sizeof(qsor));
for(i = 0;i scanf("%d", &qsor[i]);
}
qsort(qsor, 0, n-1);
for(i = 0;i printf("%d ", qsor[i]);
}
printf("\n");
T--;
}
}



void qsort(int v[], int left, int right) {
int i,last;
void swap(int [], int, int);
if(left <= right)
return;
swap(v, left, (left + right)/2);
last = left;
for(i = left + 1; i <= right; i++)
if(v[i] < v[left])
swap(v, ++last, i);
swap(v, last, left);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
void swap(int v[], int i, int j) {
int temp;

temp = v[i];
v[i] = v[j];
v[j] = temp;

}

你可能感兴趣的:(无C不欢)