標籤:整數 std 之間 strlen include ems children str 文本
17:文字排版
描述
給一段英文短文,單詞之間以空格分隔(每個單詞包括其前後緊鄰的標點符號)。請將短文重新排版,要求如下:
每行不超過80個字元;每個單詞居於同一行上;在同一行的單詞之間以一個空格分隔;行首和行尾都沒有空格。
輸入第一行是一個整數n,表示英文短文中單詞的數目. 其後是n個以空格分隔的英文單詞(單詞包括其前後緊鄰的標點符號,且每個單詞長度都不大於40個字母)。輸出排版後的多行文本,每行文本字元數最多80個字元,單詞之間以一個空格分隔,每行文本首尾都沒有空格。範例輸入
84One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile.
範例輸出
One sweltering day, I was scooping ice cream into cones and told my fourchildren they could "buy" a cone from me for a hug. Almost immediately, the kidslined up to make their purchases. The three youngest each gave me a quick hug,grabbed their cones and raced back outside. But when my teenage son at the endof the line finally got his turn to "buy" his ice cream, he gave me two hugs."Keep the changes," he said with a smile.
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 char m[50]; 5 int n,tot,x; 6 int main() 7 { 8 scanf("%d",&n); 9 while(++x<=n)10 {11 scanf("%s",m);12 tot+=strlen(m);13 if(tot!=80) tot++;14 if(tot>80)15 {16 printf("\n");17 tot=strlen(m)+1;18 }19 printf("%s",m);20 if(tot!=80&&x!=n) printf(" ");21 memset(m,0,sizeof(m));22 }23 return 0;24 }
1.13.17