2016年微软在线笔试题之Font Size

来源:互联网 发布:淘宝流量钱包有什么用 编辑:程序博客网 时间:2024/06/05 10:27

今晚做了微软的笔试题,彻彻底底的被虐了。怀着沉重的心情,写下这篇博客。

(1) 题目

题目1 : Font Size时间限制:10000ms单点时限:1000ms内存限制:256MB描述Steven loves reading book on his phone. The book he reads now consists of N paragraphs and the i-th paragraph contains ai characters.Steven wants to make the characters easier to read, so he decides to increase the font size of characters. But the size of Steven's phone screen is limited. Its width is W and height is H. As a result, if the font size of characters is S then it can only show ?W / S? characters in a line and ?H / S? lines in a page. (?x? is the largest integer no more than x)  So here's the question, if Steven wants to control the number of pages no more than P, what's the maximum font size he can set? Note that paragraphs must start in a new line and there is no empty line between paragraphs.输入Input may contain multiple test cases.The first line is an integer TASKS, representing the number of test cases.For each test case, the first line contains four integers N, P, W and H, as described above.The second line contains N integers a1, a2, ... aN, indicating the number of characters in each paragraph.For all test cases,1 <= N <= 103,1 <= W, H, ai <= 103,1 <= P <= 106,There is always a way to control the number of pages no more than P.输出For each testcase, output a line with an integer Ans, indicating the maximum font size Steven can set.样例输入21 10 4 3102 10 4 310 10样例输出32

(2) 我的思路
根据输入的参数,字体大小size从1开始递增。对于每个size,判断该size对应的总页数是否大于给定的页数p。当第一次遇到大于的情况时,那么size-1就是允许的最大的size了。

(3) 我的实现

package com.liu;import java.util.Scanner;public class Main1{       public static void main(String[] args)    {        Scanner scanner = new Scanner(System.in);           while(scanner.hasNext())        {                   int countOfTestcase = scanner.nextInt();//测试用例的个数                   scanner.nextLine();            for(int k=0; k<countOfTestcase; k++)            {                               String info = scanner.nextLine();                String[] ss = info.split(" ");                int n = Integer.parseInt(ss[0]);                int p = Integer.parseInt(ss[1]);                int w = Integer.parseInt(ss[2]);                int h = Integer.parseInt(ss[3]);                String info2 = scanner.nextLine();                //每段的字符个数                String[] ss2 = info2.split(" ");                        int size;                for(size=1; size <= w && size <= h; size++)                {                    //每行可以显示几个字符                    int rowCharCount = w/size;                    //每页可以显示几行                    int pageRowsCount = h/size;                    //所有段落需要的行数                    int rowNum = 0;                    for(int i=0; i<ss2.length; i++ )                    {                        rowNum += (int)Math.ceil(Integer.parseInt(ss2[i])*1.0/rowCharCount);                                    }                    //所需页数                    int needPages = (int) Math.ceil(rowNum*1.0/pageRowsCount);                    if(needPages > p)                    {                        break;                    }                                       }                               System.out.println(size-1);                             }        }           }}

(4) 总结
一开始,我在两次向上取整的时候,忘记将里头的int转换成double在进行除法,导致向上取整没有用,导致结果只有部分正确。检查之后才发现,真是后悔死了。至于其他,就感觉没有什么难点了。当然,如果哪位大神有更好的实现,希望不吝赐教。

1 0
原创粉丝点击