題意: 給定n隻黃金鼠,n是偶數,給定長度為n的字串,包含'x','X','x'表示坐著的黃金鼠,'X'表示站著的黃金鼠,你能花1分鐘讓一隻黃金鼠站起或坐下,問最少需要幾分鐘讓一半的黃金鼠站著,一半的黃金鼠坐著。
解法: 水題,計算站著(或坐著)的黃金鼠跟n/2的差值即可。
TAG: 水題
程式碼:
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 <iostream> | |
#include <cstdio> | |
#include <cstring> | |
using namespace std; | |
int n; | |
char s[250]; | |
int ans, cnt, nd2; | |
int ABS( int x ) | |
{ | |
return ( x<0 ? -x : x ); | |
} | |
int main( void ) | |
{ | |
int i; | |
while( scanf( "%d", &n ) == 1 ) { | |
scanf( "%s", s ); | |
nd2 = n/2; | |
cnt = 0; | |
for( i = 0; i < n; ++i ) if( s[i] == 'x' ) | |
++cnt; | |
ans = ABS( nd2-cnt ); | |
if( cnt != nd2 ) { | |
if( cnt > nd2 ) { | |
for( i = 0; i < n && cnt != nd2; ++i ) if( s[i] == 'x' ) { | |
s[i] = 'X'; | |
--cnt; | |
} | |
} else { | |
for( i = 0; i < n && cnt != nd2; ++i ) if( s[i] == 'X' ) { | |
s[i] = 'x'; | |
++cnt; | |
} | |
} | |
} | |
printf( "%d\n%s\n", ans, s ); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽