PAT 1002 Hello World for U (20),pat1002

來源:互聯網
上載者:User

PAT 1002 Hello World for U (20),pat1002

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:

h  de  ll  rlowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N. 
輸入描述:
Each input file contains one test case.  Each case contains one string with no less than 5 and no more than 80 characters in a line.  The string contains no white space.


輸出描述:
For each test case, print the input string in the shape of U as specified in the description.

輸入例子:
helloworld!

輸出例子:
h   !e   dl   llowor


#include <iostream>#include <cstring>#include <string>using namespace std;int main(){int i1,i2,left_right,down,i,j,k;char s[1024];while(cin>>s){i1=(strlen(s)+2)/3;i2=(strlen(s)+2)%3;if(1==i2){left_right=i1;down=i1+1;}else if(2==i2){left_right=i1;down=i1+2;}else{left_right=down=i1;}for(i=0,k=0;i<left_right-1;i++){cout<<s[k];for(j=2;j<down;j++)cout<<" ";cout<<s[strlen(s)-1-k]<<endl;k++;}for(i=k;i<strlen(s)-k;i++)cout<<s[i];cout<<endl;}return 0;}


插個圖



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.