微软在线笔试题:FontSize

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fingthinking/article/details/51079132

时间限制: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.

样例输入

2
1 10 4 3
10
2 10 4 3
10 10

样例输出

3
2

直接贴代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int caseNum = 0;
        while(sc.hasNext()){
            caseNum = sc.nextInt();
            int N,P,W,H;
            int[] size = new int[caseNum];      // 总的字符数
            for(int i=0; i<caseNum; i++){
                N = sc.nextInt();
                P = sc.nextInt();
                W = sc.nextInt();
                H = sc.nextInt();
                int[] pChar = new int[N];   // 每一段的字符数
                for(int j=0; j<N; j++){
                    pChar[j] = sc.nextInt();    // 每一段的字符数
                }
                /////////此处将所有数据已经读入完毕
                boolean flag = false;
                int s = 1;
                while(!flag){
                    int onePageRow = H / s; // 每页可以有的行数
                    int onePageCol = W / s; // 每行可以有的列数
                    int allRow = onePageRow * P;    // 总共的行数
                    int[] col = new int[N]; //每一段可以有的行数
                    for(int k=0; k<N; k++){
                        col[k] = (pChar[k] - 1) / onePageCol+1; //每段需要的行数 
                    }
                    int sum = 0;
                    for(int num : col){
                        sum+=num;
                    }
                    if(allRow < sum){
                        flag = true;
                    }else{
                        s++;
                    }
                }
                size[i] = s-1;
            }
            for(int maxSize : size){
                System.out.println(maxSize);
            }
        }
        sc.close();
    }
}

懒得写了,Game Over

展开阅读全文

2012微软暑期实习生试题

07-10

2012微软暑期实习生笔试题rnrnIT笔试面试题整理rnrn2012 Microsoft Intern Hiring Written Testrnrn1. Suppose that a Selection Sort of 80 items has completed 32 iterations of the main loop. How many items are now guaranteed to be in their final spot (never to be moved again)?rn(A) 16 (B) 31 (C) 32 (D) 39 (E) 40rnrn2. Which Synchronization mechanism(s) is/are used to avoid race conditions among processes/threads in operating systems?rn(A) Mutex (B) Mailbox (C) Semaphore (D) Local procedure callrnrn3. There is a sequence of n numbers 1, 2, 3,.., n and a stack which can keep m numbers at most. Push the n numbers into the stack following the sequence and pop out randomly. Suppose n is 2 and m is 3, the output sequence may be 1, 2 or 2, 1, so we get 2 different sequences. Suppose n is 7 and m is 5, please choose the output sequences of the stack:rn(A) 1, 2, 3, 4, 5, 6, 7rn(B) 7, 6, 5, 4, 3, 2, 1rn(C) 5, 6, 4, 3, 7, 2, 1rn(D) 1, 7, 6, 5, 4, 3, 2rn(E) 3, 2, 1, 7, 5, 6, 4rnrn4. What is the result of binary number 01011001 after multiplying by 0111001 and adding 1101110?rn(A) 0001 0100 0011 1111rn(B) 0101 0111 0111 0011rn(C) 0011 0100 0011 0101rnrn5. What is output if you compile and execute the following code?rnrnvoid main()rnrnint i = 11;rnint const *p = &i;rnp++;rnprintf(“%d”, *p);rnrnrn(A) 11 (B) 12 (C) Garbage value (D) Compile error (E) None of abovernrn6. Which of following C++ code is correct?rn(A) int f()rnrnint *a = new int(3);rnreturn *a;rnrnrn(B) int *f()rnrnint a[3] = 1, 2, 3;rnreturn a;rnrnrn(C) vector f()rnrnvector v(3);rnreturn v;rnrn(D) void f(int *ret)rnrnint a[3] = 1, 2, 3;rnret = a;rnreturn;rnrnrn7. Given that the 180-degree rotated image of a 5-digit number is another 5-digit number and the difference between the numbers is 78633, what is the original 5-digit number?rn(A) 60918 (B) 91086 (C) 18609 (D) 10968 (E) 86901rnrn8. Which of the following statements are true?rn(A) We can create a binary tree from given inorder and preorder traversal sequences.rn(B) We can create a binary tree from given preorder and postorder traversal sequences.rn(C) For an almost sorted array, insertion sort can be more effective than Quicksort.rn(D) Suppose T(n) is the runtime of resolving a problem with n elements, T(n) = Θ(1) if n = 1; T(n) = 2T(n/2) + Θ(n) if > 1; so T(n) is Θ(n log n).rn(E) None of the above.rnrn9. Which of the following statements are true?rn(A) Insertion sort and bubble sort are not effcient for large data sets.rn(B) Quick sort makes O(n^2) comparisons in the worst case.rn(C) There is an array: 7, 6, 5, 4, 3, 2, 1. If using selection sort (ascending), the number of swap operation is 6.rn(D) Heap sort uses two heap operations: insertion and root deletion.rn(E) None of above.rnrn10. Assume both x and y are integers, which one of the followings returns the minimum of the two integers?rn(A) y ^ ((x ^ y) & ~(x < y))rn(B) y ^(x ^ y)rn(C) x ^ (x ^ y)rn(D) (x ^ y) ^ (y ^ x)rn(E) None of the abovernrn11. The Orchid Pavilion (兰亭集序) is well known as the top of “行书” in history of Chinese literature. The most fascinating sentence “Well I know it is a lie to say that life and death is the same thing, and that longevity and early death make no difference! Alas!” (“周知一死生为虚诞, 齐彭殇为妄作。“) By counting the characters of the whole content (in Chinese version), the result should be 391 (including punctuation). For these characters written to a text file, please select the possible file size without any data corrupt.rn(A) 782 bytes in UTF-16 encodingrn(B) 784 bytes in UTF-16 encodingrn(C) 1173 bytes in UTF-8 encodingrn(D) 1176 bytes in UTF-8 encodingrn(E) None of the abovernrn12. Fill the blanks inside class definitionrnrnclass Testrnrnpublic:rn____ int a;rn____ int b;rnpublic:rnTest::Test(int _a, int _b) : a(_a) b = _b;rn;rnrnint Test::b;rnrnint _tmain(int argc, __TCHAR *argv[])rnrnTest t1(0, 0), t2(1, 1);rnt1.b = 10;rnt2.b = 20;rnprintf(“%u %u %u %u”, t1.a, t1.b, t2.a, t2.b);rnrnrnRunning result: 0 20 1 20rnrn(A) static/constrn(B) const/staticrn(C) –/staticrn(D) const static/staticrn(E) None of the abovernrn13. A 3-order B-tree has 2047 key words, what is the maximum height of the tree?rn(A) 11 (B) 12 (C) 13 (D) 14rnrn14. In C++, which of the following keyword(s) can be used on both a variable and a function?rn(A) static (B) virtual (C) extern (D) inline (E) constrnrn15. What is the result of the following program?rnchar* f(char *str, char ch)rnrnchar *it1 = str;rnchar *it2 = str;rnwhile (*it2 != ‘\0′) rnwhile (*it2 == ch) rnit2++;rnrn*it1++ = *it2++;rnrnreturn str;rnrnrnvoid main(int argc, char *argv[])rnrnchar *a = new char[10];rnrnstrcpy(a, “abcdcccd”);rnrncout << f(a, ‘c’);rnrnrn(A) abdcccd (B) abdd (C) abcc (D) abddcccd (E) Access Violationrnrn16. Consider the following definition of a recursive function, power, that will perform exponentiation.rnrnint power(int b, int e)rnrnif (e == 0) return 1;rnif (e %2 == 0) return power (b * b, e / 2);rnreturn b * power(b * b, e / 2);rnrnrnAsymptotically (渐进地) in terms of the exponent e, the number of calls to power that occur as a result of the call power(b, e) isrn(A) logarithmic (B) linear (C) quadratic (D) exponentialrnrn17. Assume a full deck of cards has 52 cards, 2 black suits (spade and club) and 2 red suits (diamond and heart).rnIf you are given a full deck, and a half deck (with 1 red suit and 1 black suit), what’s the possibility for each one getting 2 red cards if taking 2 cards?rn(A) 1/2, 1/2 (B) 25/102, 12/50 (C) 50/51, 24/25 (D) 25/51, 12/25 (E) 25/51, 1/2rnrn18. There is a stack and a sequence of n numbers (i.e., 1, 2, 3, …, n). Push the n numbers into the stack following the sequence and pop out randomly. How many different sequences of the number we may get? Suppose n is 2, the output sequence may be 1, 2 or 2, 1, so we get 2 different sequences.rn(A) C_2n^nrn(B) C_2n^n – C_2n^(n + 1)rn(C) ((2n)!) / (n + 1)n!n!rn(D) n!rn(E) None of the abovernrn19. Longest Increasing Subsequence (LIS) means a sequence containing some elements in another sequence by the same order, and the values of elements keeps increasing.rnFor example, LIS of 2, 1, 4, 2, 3, 7, 4, 6 is 1, 2, 3, 4, 6, and its LIS length is 5.rnConsidering an array with N elements, what is the lowest time and space complexity to get the length of LIS?rn(A) Time: N^2, Space: N^2rn(B) Time: N^2, Space: Nrn(C) Time: NlogN, Space: Nrn(D) Time: N, Space: Nrn(E) Time: N, Space: Crnrn20. What is the output of the following piece of C++ code?rnrn#include rnusing namespace std;rnrnstruct Itemrnrnchar c;rnItem *next;rn;rnrnItem *Routine1(Item *x)rnrnItem *prev = NULL, *curr = x;rnwhile (curr) rnItem *next = curr->next;rncurr->next = prev;rnprev = curr;rncurr = next;rnrnreturn prev;rnrnrnvoid Routine2(Item *x)rnrnItem *curr = x;rnrnwhile (curr) rncout << curr->c << ” “;rncurr = curr->next;rnrnrnrnvoid _tmain(void)rnrnItem *x,rnd = ‘d’, NULL,rnc = ‘c’, &d,rnb = ‘b’, &c,rna = ‘a’, &b;rnrnx = Routine1(&a);rnRoutine2(x);rnrnrn(A) cbad (B) badc (C) dbca (D) abcd (E) dcba 论坛

没有更多推荐了,返回首页