解题思路:分析题意,只要加上某一字符宽度使得整行的字符宽度大于100,则另起一行写该字符。因此采用两个整数n,m分别记录行数和最新一行的字符宽度,当m等于100且字符串中还有其他字符的情况下,行数加一,m置0;当m大于100时,行数加一,m为此时写入的字符宽度。
import java.util.*;
public class test2{
public static void main(String arg[]){
Scanner scan=new Scanner(System.in);
int[] a=new int[26];
for(int i=0;i<26;i++){
a[i]=scan.nextInt();
}
String str=scan.next();
int n=1;
int m=0;
for(int i=0;i<str.length();i++){
m=m+a[str.charAt(i)-'a'];
if(m==100&&i<str.length()-1){
m=0;
n++;
}
else if(m>100){
m=a[str.charAt(i)-'a'];
n++;
}
}
System.out.print(n);
System.out.print(' ');
System.out.print(m);
}
}
京公网安备 11010502036488号