微软2016校园招聘4月在线笔试(一)Font Size
描述
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.
解题思路
本道主要考虑到一下几点:
- 每一行存放的的字符数目为下整即:Math.Floor(value)
- 每一段需要的行数为上整数即:Math.Ceiling(value)
- 每一页存放的行数为下整数即:Math.Floor(value)
- 所需要的页数为上整数即:Math.Ceiling(value)
综合以上可以得出整个公式(1):
由题意可知,S存在取值区间即:(0,Min(h,w)],所以采用for循环即可求得最大值。但也可以从该公式入手,获取S的最大值,而不是Min(h,w),即假设每一行可以存在非整数个字符,每一页也可以存在非整数的行,每一段也不用另起一行。那么公式(2)如下:
再取得最大值后,在一步步向下取值,知道满足公式(1)条件。
代码
using System;
using System.Collections.Generic;
using System.Text;namespace FontSize
{
class Program
{
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] res=new int[n];
int counter = 0;
while (counter<n)
{
res[counter] = PerCase();
counter++;
}
foreach (var re in res)
{
Console.WriteLine(re);
}
}static int PerCase()
{
string[] npwh = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(npwh[0]);
int p = Convert.ToInt32(npwh[1]);
int w = Convert.ToInt32(npwh[2]);
int h = Convert.ToInt32(npwh[3]);
int[] paras=new int[n];
string[] paraStrings = Console.ReadLine().Split(' ');
int total = 0;
for (int i = 0; i < n; i++)
{
paras[i] = Convert.ToInt32(paraStrings[i]);
total += paras[i];
}
int persudoS = (int) Math.Sqrt(((double)p*w*h)/total);
int s = persudoS;
while (true)
{
int ws = w/s;
int hs = h/s;
int lines = 0;
foreach (var para in paras)
{
lines += (int)Math.Ceiling((double) para/ws);
}
int pages = (int) Math.Ceiling((double) lines/hs);
if(pages<=p)
break;
--s;
}
return s;
}
}
}




浙公网安备 33010602011771号