首页 > 试题广场 >

一封奇怪的信

[编程题]一封奇怪的信
  • 热度指数:9258 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现在你需要用一台奇怪的打字机书写一封书信。信的每行只能容纳宽度为100的字符,也就是说如果写下某个字符会导致行宽超过100,那么就要另起一行书写
信的内容由a-z的26个小写字母构成,而每个字母的宽度均会事先约定。例如字符宽度约定为[1,2,3,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],那么就代表'a'到'd'四个字母的宽度分别是1,2,3,4,而'e'到'z'的宽度均为5
那么按照上述规则将给定内容S书写成一封信后,这封信共有几行?最后一行宽度是多少?

输入描述:
输入为两行:
第一行是存储了每个字符宽度的字符串,包含26个数字,以1个空格分隔,每个数字均小于等于10
第二行是存储了待输入字符的字符串S,字符串S的长度在1到1000之间


输出描述:
输出为信的行数以及最后一行字符宽度,中间以1个空格分隔
示例1

输入

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
helloworld

输出

1 50

说明

"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5"规定每个字符宽度为5
"helloworld"是输入的字符串S
由于S共包含10个字符,也即共占用50个字符宽度,因此可以写在同一行
示例2

输入

5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10 10 10 5 5 5 5 5 5 5 5
hahahahahahahaha

输出

2 20

说明

"5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10 10 10 5 5 5 5 5 5 5 5"规定了每个字符宽度
"hahahahahahahaha"是输入的字符串S
由于h宽度为10,a宽度为5,因此'hahahahahahah'占用100字符宽度可以写在第一行,‘aha’写在第二行也即最后一行。因此字符宽度为20

备注:
输入及输出均为字符串
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        String word = in.nextLine();
        String[] chars = line.split(" ");
        char[] chs = word.toCharArray();
        int maxLen= 100;
        int row = 1;//最后一行字符串
        int len = 0;//最后一行字符长度
        for(char ch : chs){
            int index = Integer.valueOf(ch-'a'); //通过计算ascii码值获取指定字符的长度
            int charLen = Integer.valueOf(chars[index]);
            len+=charLen;
            if(len>maxLen){
                len=charLen;
                row++;
            }
        }
        System.out.print(row+" "+len);

    }
}

发表于 2025-03-07 16:06:06 回复(0)
笨办法
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        Map<Character,Integer> map = new HashMap<>();//使用Map集合记录对应的字符与宽度
        int temp = 'a';
        for(int i = 0;i<26;i++){
            map.put((char)temp,s.nextInt());
            temp++;
        }
        String str = s.next();
        char[] ch = str.toCharArray();//字符数组
        int sum = 0;
        int count = 1;
        for(int i = 0;i<ch.length;i++){
            int value = map.get(ch[i]);
            sum+=value;
            if(sum==100){
                sum=0;
                count++;
            }
            if(sum>100){
                sum=0;
                i=i-1;
                count++;
            }
        }
        System.out.println(count+" "+sum);
    }
}
发表于 2020-06-11 14:23:15 回复(0)
/*
首先用一个长度为26的整型数组保存每个单词的长度;
当需要知道某个单词的长度时,只需要用该字符减去97,然后将差值作为下标即可
然后以100进行统计,当加上需要添加的字符使得总和大于100,则不添加,否则继续
*/

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int[] arr = new int[26];//26个字符对应的宽度
        for(int i = 0;i<26;i++)
            arr[i] = input.nextInt();
        String str = input.next();
        char[] ch = str.toCharArray();//字符数组
        
        int count = 1;//行数
        int sum = 0;//字符的总和
        int lastsum = 0;//最后一行的和
        for(int i = 0;i<ch.length;){
            int chlength = arr[ch[i]-97];
            if(sum + chlength <= 100){
                sum+=chlength;
                lastsum = sum;
                i++;//把这个放在这里的目的是,为了确保在刚好要超过100的那个字符能参与到下一行的计算
            }else{
                count++;
                sum = 0;//总和置0,重新统计
            }
        }
        
        System.out.print(count + " " + lastsum);
    }
}

发表于 2020-04-25 13:42:52 回复(0)
笨方法如我
import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        String str2 = br.readLine();
        int charcount = 0;
        int linecount = 1;
        int width = 0;
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0;i < 26;i++){
            char temp = (char)('a' + i);
            map.put(temp,Integer.parseInt(str[i]));
        }
        for(int j = 0;j < str2.length();j++){
            if(charcount + map.get(str2.charAt(j)) > 100){
                linecount++;
                charcount = map.get(str2.charAt(j));
            }else{
                charcount += map.get(str2.charAt(j));
            }
        }
        System.out.print(linecount + " ");
        System.out.print(charcount);
    }
}

发表于 2020-04-20 22:30:41 回复(0)