0 0 投票数

Lab10_01，EOJ3107。

void input ( int* p, int n ) {
for ( int i = 0; i < n; i++ ) {
scanf ( "%d", p++ );
}

return;
}
void output ( int* p, int n ) {
for ( int i = 0; i < n; i++ ) {
printf ( "%d ", *p++ );
}

return;
}

 int *min=p,*max=p;

for ( int i = 0; i < n; i++ ) {
if ( * ( p + i ) < *min ) {
min = p + i;

} else if ( * ( p + i ) > *max ) {
max = p + i;
}
}


int t;
//临时变量
t = *min;
*min = *p;
*p = t;
//交换min
t = *max;
*max = * ( p + n - 1 );
* ( p + n - 1 ) = t;
//交换max


void process ( int* p, int n ) {
int *min = p, *max = p;

for ( int i = 0; i < n; i++ ) {
if ( * ( p + i ) < *min ) {
min = p + i;

} else if ( * ( p + i ) > *max ) {
max = p + i;
}
}

int t;
t = *min;
*min = *p;
*p = t;
t = *max;
*max = * ( p + n - 1 );
* ( p + n - 1 ) = t;

return;
}

Debug后可以发现，这是因为比较过程中确定了max指向3的地址、min指向1的地址，而3和1恰恰又是数组的第一个元素和最后一个元素，也就是我们最终需要存放最大值和最小值的地方。所以，结束循环以后，3和1交换，1和3交换，最终的结果看上去就像没交换一样，事实上不是没交换，而是交换过头了。

if ( max == p ) {
t = *p;
*p = *min;
* ( p + n - 1 ) = t;

} else {
t = *min;
*min = *p;
*p = t;
t = *max;
*max = * ( p + n - 1 );
* ( p + n - 1 ) = t;
}


Lab10_02，EOJ3109。

strncpy(t,&s[m],strlen(s)-m);

for (int i=0;i<=N;i++) t[i]=0;

strncpy(t,&s[m],(strlen(s)-m+1)*sizeof(char));

char* strmcpy ( char* t, char* s, int m ) {
for ( int i = 0; i <= N; i++ ) t[i] = 0;

strncpy ( t, &s[m], ( strlen ( s ) - m ) *sizeof ( char ) );
}

char* strmcpy ( char* t, char* s, int m ) {
strncpy ( t, &s[m], ( strlen ( s ) - m + 1 ) *sizeof ( char ) );
}

char* strmcpy ( char* t, char* s, int m ) {
for ( int i = 0; i < strlen ( s ) - m + 1; i++ ) {
t[i] = s[m + i];
}
}

char* strmcpy ( char* t, char* s, int m ) {
for ( int i = 0; i < strlen ( s ) - m + 1; i++ ) {
t[i] = s[m + i];
}

return t;
}

_CRTIMP char *  __cdecl strncpy(char *, const char *, size_t);

char* strmcpy ( char* t, char* s, int m ) {
strncpy ( t, &s[m], ( strlen ( s ) - m + 1 ) *sizeof ( char ) );
return t;
}

char* strmcpy ( char* t, char* s, int m ) {
return strncpy ( t, &s[m], ( strlen ( s ) - m + 1 ) *sizeof ( char ) );
}

for ( int i = 0; i <= N; i++ )

char* strmcpy ( char* t, char* s, int m ) {
for ( int i = 0; i <= N; i++ ) {
t[i] = s[m + i];
}

return t;
}

Lab10_03，EOJ3019。

char*  CharsReplace ( char *p ) {
char*op = p;

while ( *p ) {
*p = *p + 1;

if ( *p > 'Z' ) *p = 'A';

p++;
}

return op;
}

char*  CharsReplace ( char *p ) {

while ( *p ) {
*p = *p + 1;

if ( *p > 'Z' ) *p = 'A';

p++;
}

return p;
}

Lab10_04，EOJ3108。

void rotate(int* p, int n, int m){
int b[N];
for (int i=n-m;i<n;i++){
b[i-(n-m)]=p[i];
}
for (int i=0;i<n-m;i++){
b[i+m]=p[i];
}
for (int i=0;i<n;i++){
p[i]=b[i];
}
}

0 0 投票数

1条留言

（可选）如果您也有个人网站，不妨分享一下

1 评论

sscjwdyzdw(@sscjwdyzdw)

2016年12月8日 15:18