题目
At many competitions that have a word «cup» in its official name the winner is presented with an actual cup. This time the organizers of one unusual programming competition have decided to please the winner even more and to add a nameplate to the cup with the handle of the winner.
The nameplate is to be rectangular and the text on it will be printed as a table of several rows and columns. Having some measurements done, the organizers have found out that the number a of rows cannot be greater than 5 while the number b of columns cannot exceed 20. Every cell of the table will contain either an asterisk («*») or a letter of user’s handle.
Furthermore, the organizers want the rows of the table to be uniform, which means that the number of asterisks used in different rows should differ by at most one (i.e. you can’t have two asterisks in the first row and none in the second). The main goal, however, is to obtain the winner’s handle precisely when reading the table from top to bottom and from left to right in every row (skipping asterisks).
The organizers want for the nameplate to have as few rows as possible and among all valid tables with the minimum number of rows they want to choose the one that has the minimum number of columns.
The winner is not yet determined so your task is to write a program that, given a certain handle, generates the necessary table.
Input
The only line contains one string s (1≤|s|≤100), comprised of uppercase and lowercase Latin letters, — the handle of the winner.
Output
In the first line output the minimum number a of rows in the table and the minimum number b of columns in an optimal table with rows.
The following a lines should contain b characters each — any valid table.
Examples
input
tourist
output
1 7
tourist
input
MyNameIsLifeIAmForeverByYourSideMyNameIsLife
output
3 15
MyNameIsLifeIAm
ForeverByYourSi
deMyNameIsL*ife
题解:给一个字符串,将其分成等长度的字符串,但是分的行数不能超过5行,每行的长度不得超过20,如果无法构成等长度的,则可以用“ * ”来补足长度,但是行与行之间的 “ * ”的个数差值不能超过1。当有多中情况,输出其中一个结果即可。
解题思路:先计算出每20个字符可以分的行数a,再计算a行中每行应有多少个字符;最后计算a*b-减去字符串的长度的值即为需要补足“ * ”的个数p;然后利用循环,当p>0时;则每行先输出一个“ * ”;然后在输出b-1个字符;否则,直接输出b个字符。
AC – Code
import java.util.Scanner;
public class CF1079B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
double l = s.length();
int a = (int)Math.ceil(l/20);
int b = (int)Math.ceil(l/a);
int p = a*b-(int)l;
int q=0;
System.out.println(a+" "+b);
for(int i = 0;i<a;i++)
{
if(p>0)
{
System.out.print("*");
p--;
for(int j = 0;j<b-1;j++)
{
System.out.print(s.charAt(q));
q++;
}
}
else
{
for(int j = 0;j<b;j++)
{
System.out.print(s.charAt(q));
q++;
}
}
System.out.println();
}
sc.close();
}
}