我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2014年4月26日 星期六

[Codeforce Round #242]A. Squats

題目網址: http://codeforces.com/contest/424/problem/A

題意: 給定n隻黃金鼠,n是偶數,給定長度為n的字串,包含'x','X','x'表示坐著的黃金鼠,'X'表示站著的黃金鼠,你能花1分鐘讓一隻黃金鼠站起或坐下,問最少需要幾分鐘讓一半的黃金鼠站著,一半的黃金鼠坐著。



解法: 水題,計算站著(或坐著)的黃金鼠跟n/2的差值即可。

TAG: 水題

程式碼:
#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;
}
view raw A. Squats.cpp hosted with ❤ by GitHub

沒有留言:

張貼留言

任何意見都樂意傾聽