Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) B——Personalized Cup

Whiteying

2018-11-26 22:41:03

Personal

# 题目链接: http://codeforces.com/contest/1079/problem/B # 题意: 给一个字符串,将其分成等长度的字符串,但是分的行数不能超过5行,每行的长度不得超过20,如果无法构成等长度的,则可以用“ * ”来补足长度,但是行与行之间的 “ * ”的个数差值不能超过1。当有多中情况,输出其中一个结果即可。 # 思路: 先计算出每20个字符可以分的行数a,再计算a行中每行应有多少个字符;最后计算a*b-减去字符串的长度的值即为需要补足“ * ”的个数p;然后利用循环,当p>0时;则每行先输出一个“ * ”;然后在输出b-1个字符;否则,直接输出b个字符。 # AC代码: ```cpp #include<iostream> using namespace std; string text; int row,line,star; int main() { cin>>text; int len=text.length(); for(int i=1;i<=5;i++) if(i*20>=len) { row=i; break; } for(int i=1;i<=20;i++) if(i*row>=len) { line=i; break; } star=row*line-len; int now=0; cout<<row<<" "<<line<<endl; for(int i=1;i<=row;i++) { if(star>0) { star--; cout<<"*"; for(int j=1;j<line;j++) { cout<<text[now]; now++; } cout<<endl; } else { for(int j=0;j<line;j++) { cout<<text[now]; now++; } cout<<endl; } } return 0; } ```