您的位置:首页 > 编程语言

搜狐2017校招编程题

2017-08-28 20:58 260 查看
题目1

输出序列题

通过率为100%

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main()
{

int n = 0;
int m = 0;
while (cin >> n && cin >> m)
{
if (0 == n || 0 == m) break;

vector<int> a(m, 0);
for (int i = 0; i < m; i++)
{
cin >> a[i];
}

vector<int > out(n, 0);

int tem1 = 0, tem2 = 0;
int j = 0;

for (int i = 0; i < n; i++)
{
out[i] = a[tem1];
tem2++;

if (tem2 == out[j])
{
tem2 = 0;
tem1 = (tem1 + 1) % m;
j++;
}

}

for (int i = 0; i < out.size(); i++)
{
printf("%d\n", out[i]);
}

}

return 0;
}


题目2

题目描述

工厂生产的包裹在相同高度h,尺寸为 1*1, 2*2, 3*3,4*4,5*5,6*6 的盒子。组装成 6 * 6的大盒子,问最少装几个盒子

输入

输入为几行,每一行代表一个订单,一行6个整数,分别为 1 * 1 至 6 * 6 这6个产品的数量,最后一行输入 6个0.

输出

每个订单输出一行最小包裹数目

代码如下,通过率100%

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
while (1)
{
string line;
getline(cin, line);
stringstream ss(line);

vector<int> blocks(6, 0);

for (int i = 0; i < 6; i++)
{
ss >> blocks[i];
}

int out = 0;

// 6
out += blocks[5];
blocks[5] = 0;

// 5
out += blocks[4];
while (blocks[4] > 0 && blocks[0] > 0)
{
blocks[4]--;
blocks[0] = (blocks[0] >= 11) ? (blocks[0] - 11) : 0;
}
blocks[4] = 0;

// 4
out += blocks[3];
while (blocks[3] > 0 && (blocks[0] > 0 || blocks[1] > 0))
{
blocks[3]--;
if (blocks[1] >= 5)
{
blocks[1] -= 5;
}
else if (blocks[1] > 0)
{
int tmp = 20 - blocks[1] * 4;
blocks[0] = (blocks[0] > tmp) ? blocks[0] - tmp : 0;
blocks[1] = 0;
}
else
{
blocks[0] = (blocks[0] > 20) ? blocks[0] - 20 : 0;
}
}
blocks[3] = 0;

// 3
while (blocks[2] >= 4)
{
out += 1;
blocks[2] -= 4;
}

if (blocks[2] > 0)
{
out += 1;
int tmp = 36 - 9 * blocks[2];

if (blocks[1] > 0)
{
int tmp1 = tmp / 4 - 1;
if (blocks[1] >= tmp1)
{
blocks[1] -= tmp1;
tmp -= tmp1 * 4;
}
else
{
tmp -= blocks[1] * 4;
blocks[1] = 0;
}
}

if (blocks[0] > 0)
{
blocks[0] = blocks[0] > tmp ? (blocks[0] - tmp) : 0;
}
}

// 2
while (blocks[1] >= 9)
{
out += 1;
blocks[1] -= 9;
}

if (blocks[1] > 0)
{
out += 1;
int tmp = 36 - blocks[1] * 4;
if (blocks[0] > 0)
{
blocks[0] = blocks[0] > tmp ? (blocks[0] - tmp) : 0;
}
}

// 1
while (blocks[0] > 36)
{
out += 1;
blocks[0] -= 36;
}

if (blocks[0] > 0)
{
out += 1;
}

if (0 == out)
{
break;
}

printf("%d\n", out);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: