第一题:
求数列的前n项,给定一个数字字典,比如{2,1,3,1},构造一个新数列,新数列满足以下:
1:字典里的数循环重复,如果把重复的数记为一组,以每一组的个数构成新数列,新数列和原数列一样。
以上面字典为例:生成数组为:2,2,1,1,3,1,2,2,2。。。
思路:假设需要生成的数组为a,第一个数肯定就是字典第一个,同时他还代表了这个数出现的次数。所以数组向后重复,然后数组的第二个数在上次扩展中已经有了,他还代表第二个数应该重复的次数,所以继续向后面重复字典的循环下一个数,重复次数就为第二个数的值,后面一样的了,,直到到达题目要求的前n个数为止,其实把原数组分为两个一样的数组比较好理解,第一个数组代表数组,第二个数组代表次数,第二个数组的每次增长一个,第一个数组每次增长第二个数组里相应值的重复次数个。所以第二个增长慢一点。根据第二个数组来填充第一个数组。
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n =30 ,m = 4;
cin>>n>>m;
int a = m;
vector<int> re;
while(a--) {
int b;
cin>>b;
re.push_back(b);
}
vector<int> index(n,0);
int i = 0, j= 0; //i代表数组填充到什么位置了,也就是前n项的位置,j代表数字字典里面下一个重复的数,
int f = 0; // f代表虚拟的第二个数组中的值,根据该值来确定下一个数的重复次数。
while(i<n){ //j每次循环+1,f每次+1,i每次+index[f]的值,
if(index[f] == 0){
index[i] = re[j];
int count = index[i];
while(count--){
index[i] = re[j];
i++;
if(i == n){
for(auto val:index) cout<<val<<endl;
return 0;
}
}
j = (j+1)%m;
f++;
} else {
int count = index[f] ;
while(count--){
index[i] = re[j];
i++;
if(i == n){
for(auto val:index) cout<<val<<endl;
return 0;
}
}
j = (j+1)%m;
f++;
}
}
return 0;
}
第二题:
一家工厂生产的产品被包装在一个正方形的包装盒中,产品具有相同的高度h,大小规格为1*1、2*2、3*3、4*4、5*5和6*6。这些产品是用和产品具有相同高度h,大小规格为6*6的正方形邮包交付给客户的。由于费用问题,工厂和客户都要使将订购的物品从工厂发送给客户的邮包的数量最小化。请编写一个程序,对于按照订单发送给定的产品,找出最少的邮包数量,以节省费用。
输入:
输入由若干行组成,每行描述一份订单,每份订单由6个整数组成,整数之间用一个空格分开,连续的整数表示从最小的1*1到最大的6*6每种大小的包装盒的数量,输入以包含6个0的一行结束。
输出:
对每行输入,输出一行,给出邮包的最小数量。对于输入的最后一行“空输入”没有输出。
思路:思路就是 4,5,6大小的每个必须都需要一个单独的箱子,6大小的无法填充,5大小的只能用1来填,所以5的箱子可以包含 11*(5箱子的个数)个1箱子,同理;4箱子先装2箱子的,每个能装5*(4箱子个数)的2箱子,剩下的装1;3箱子可以完美装4个,先看能装几个,最多剩下一个装不满的,如果还有剩下的1,2箱子装进去,最后再装1,2的。 不知道哪儿错了,自己跑的是对的,就是提交 case 0;不到位啊!!!!感觉是输入输出有问题,爆炸!
#include <iostream>
#include <vector>
using namespace std;
int main() {
int a,b,c,d,e,f;
vector<vector<int>> re;
while(cin>>a>>b>>c>>d>>e>>f) {
if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0) break;
int count = f+e+d;
int count1 = (a-e*11)<=0?0:(a-e*11);
int count2 = b-f*5;
int cost3 = b/4;
int count3 = b%4;
int remin2 = 0;
int cost2 = 0;
if(count2<0) {
count1 = count1+count2;
} else {
cost2 = count2/9;
remin2 = count2%9;
}
count1 = count1-remin2;
if(count1<0){
count +=count%36;
}
count = count + cost3 +cost2;
cout<<count;
}
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
while (1)
{
string line; //利用string读取一行
getline(cin, line); //getlian函数读取到line中
stringstream ss(line); // 转化为stringstream类型
vector<int> blocks(6, 0);
for (int i = 0; i < 6; i++) //利用block保存;
{
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;
}