84 One 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.
样例输出
1 2 3 4 5 6
One 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.
思路
用 string str; 定义字符串
用 int n; 定义整数
用 cin >> n; 输入整数
用 int summaryLine = 0; 定义行字符数
用 for 循环接收字符串,这里用了 cin >> str; 只能接收一个单词,如果有标点符号则会被分开的特性。
用 if 判断是否超过 80 个字符,是则换行,否则输出字符串。注意:str.size()+1 是字符串长度加上一个空格的长度,此时设 summaryLine 为字符串长度。
用 else if 判断是否为第一个字符串,是则输出字符串,否则输出空格和字符串,并更新 summaryLine。
用 else 输出空格和字符串,并更新 summaryLine。
Code
C++ STL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<bits/stdc++.h> usingnamespace std;
intmain(){ string str; int n; cin >> n; int summaryLine = 0; for(int i = 0; i < n; i++) { cin >> str; if(str.size()+1 + summaryLine > 80) { // str.size()+1 is the length of str and backspace cout << endl; cout << str; summaryLine = str.size(); } elseif(summaryLine == 0){cout << str; summaryLine += str.size();} else {cout << " " << str; summaryLine += str.size()+1;} } }
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<stdio.h> #include<string.h>
intmain() { char str[41]; int n; scanf("%d", &n); int summaryLine = 0; for(int i = 0; i < n; i++) { scanf("%s", str); if(strlen(str)+1 + summaryLine > 80) { // strlen(str)+1 is the length of str and backspace printf("\n"); printf("%s", str); summaryLine = strlen(str); } elseif(summaryLine == 0){printf("%s", str); summaryLine += strlen(str);} else {printf(" %s", str); summaryLine += strlen(str)+1;} } }
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<bits/stdc++.h> usingnamespace std;
intmain(){ char str[41]; int n; scanf("%d", &n); int summaryLine = 0; for(int i = 0; i < n; i++) { scanf("%s", str); if(strlen(str)+1 + summaryLine > 80) { // strlen(str)+1 is the length of str and backspace printf("\n"); printf("%s", str); summaryLine = strlen(str); } elseif(summaryLine == 0){printf("%s", str); summaryLine += strlen(str);} else {printf(" %s", str); summaryLine += strlen(str)+1;} } }
测试用例
测试用例 1
输入
1 2
84 One 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.
输出
1 2 3 4 5 6
One 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.
测试用例 2
输入
1 2
84 One 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.
输出
1 2 3 4 5 6
One 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.
测试用例 3
输入
1 2
84 One 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.
输出
1 2 3 4 5 6
One 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.