UVa 414 - Machined Surfaces
阿新 • • 發佈:2018-04-15
IT () col str color iostream item set while https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=355
題目大意:
有一個數字圖像設備有N行,每行有25個字符。第一列和第25列都是X,最左和最右都可以是連續的多個X,中間是空格。 當把最右往左平移到X相遇的時候,計算整個平面的空格數。註意
輸入:
行數N,然後是N行的字符;
註意:sample 中用B表示空格是為了看的方便,真實的輸入是ASCII的空格字符;
輸出:
平面的空格數;
Sample Input
4
XXXXBBBBBBBBBBBBBBBBXXXXX
XXXBBBBBBBBBBBBBBBXXXXXXX
XXXXXBBBBBBBBBBBBBBBBXXXX
XXBBBBBBBBBBBBBBBBBXXXXXX
2
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXBBBBBBBBBBBBBBXX
0
Sample Output
4
0
0
思路分析:這道題的主要是讀懂題目意思。
每行固定25個字符,當平移結束的時候是有某一行中間沒有了空格,這一行為X字符個數最多的一行,其中X字符數記為max,則其他行的空格數為max減去該行的X字符個數;
假設記錄每i行的X個數為aa[i],其中aa[i]最大為max;則 max - aa[i] 為每行的空格數,平面的空格數為 sum(max - aa[i])
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int main() { // freopen("input.txt","r",stdin); string s; int count,n,i,max,j; int aa[100]; while(cin>>n&&n) { getchar(); max=0; memset(aa,0,sizeof(aa)); for(j=0;j<n;j++) { count=0; getline(cin,s); for(i=0;i<s.length();i++) if(s[i]==‘X‘) count++; aa[j]=count; if(max<count) max=count; } count=0; for(i=0;i<n;i++) { count+=max-aa[i]; } cout<<count<<endl; } return 0; }
UVa 414 - Machined Surfaces