一直不斷比較最後找到的D和R的間距是否為最小的即是解答。
(當然如果裡面有Z,那答案一定是0)
[C](0.356)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
int main() | |
{ | |
int L; | |
while( scanf( "%d", &L ) != EOF && L != 0 ) | |
{ | |
getchar(); | |
char road[2000005] = {0}; | |
gets( road ); | |
int i, j; | |
int min = 2147483647; | |
int Z = 0; | |
int D = -1, R = -1; | |
for( i = 0 ; i < L && !Z ; i++ ) | |
{ | |
if( road[i] == 'D' ) | |
D = i; | |
if( road[i] == 'R' ) | |
R = i; | |
if( road[i] == 'Z' ) | |
Z = 1; | |
if( D >= 0 && R >= 0 ) | |
min = ( abs(D-R) < min )? abs(D-R) : min; | |
} | |
if( Z ) | |
printf( "0\n" ); | |
else | |
printf( "%d\n", min ); | |
} | |
return 0; | |
} |
0 意見:
張貼留言