各位大哥,我有一道笔试编程题,请大家帮我解答一下,谢谢! 整理啤酒酒箱子问题:输入一数列代表每一个箱子中的酒瓶数如下:12,32,34,54,52A B C D E并给出装酒的箱子最大能装的数量如:100现要求写一程序整理酒箱使得尽可能多的酒箱装满 例子:输入文件input.txt数据如下:第一行为箱子的容量,第二行为箱子的编号,第三行为对应编号中的啤酒数量100A, B, C, D, E12,32,34,54,52 要求从程序当前目录下读入input.txt文件, 输出为整理的每一步过程和最后的结果到当前目录下的output.txt: B -> A 32 C -> A 34 D -> A 22 D -> B 32 E -> B 52 100,84,0,0,0 (要求代码有合适的注释,程序中的注释应能体现思路演变的关键,程序使用标准c++代码实现,并能通过VC7.1及其以上版本的编译)
使用Unicode?
#include <stdio.h>int main()
{
int box[5] = {12, 32, 34, 54, 52};
for (int i = 0; i < 5; i++)
{
for(int j = i + 1; j < 5; j++)
{
int newbox = box[i] + box[j];
if (newbox >= 100)
{
printf ("%c->%c : %d\n", (char)('A' + j), (char)('A' + i), 100 - box[i]);
box[i] = 100;
box[j] = newbox - 100;
break;
}
else if(box[j] != 0)
{
printf ("%c->%c : %d\n", (char)('A' + j), (char)('A' + i), box[j]);
box[j] = 0;
box[i] = newbox;
}
}
} for (int i = 0; i < 5; i++)
{
printf("%c : %d\n", (char)('A'+i), box[i]);
}
return 0;
}
#define BOX_NUM 10
#define MAX_BOTTLE 100
void FillBottle()
{
char chFlg[BOX_NUM];
for( int i = 0; i < BOX_NUM; i++ )
{
chFlg[i] = i + 'A';
}
int nNum[BOX_NUM] = {89, 12, 56, 29, 12, 12, 14, 67, 25, 52}; int nCount = 0;
for( int i = 0; i < BOX_NUM; i++ )
{
nCount += nNum[i];
} //初始化
int nFillBoxCount = nCount / MAX_BOTTLE; //计算出一共能填满多少个瓶子, int nRevIndx = BOX_NUM - 1;//反向序号
int nIndex = 0; //正向序号
while( nIndex < nFillBoxCount ) //顺序填满前nFillBoxCount - 1个瓶子
{
TRACE("%c-->%c, %d\n", chFlg[nRevIndx], chFlg[nIndex], nNum[nRevIndx]);
nNum[nIndex] += nNum[nRevIndx];
if( nNum[nIndex] > MAX_BOTTLE ) //瓶子有剩余
{
nNum[nRevIndx] = nNum[nIndex] - MAX_BOTTLE;
nNum[nIndex] = 100; nIndex++; //移到下一个
}
else //瓶子刚好或者不足
{
nNum[nRevIndx] = 0;
nRevIndx--; //移动到前一个
}
}
while( nFillBoxCount != nRevIndx ) //剩下的瓶子全部添到第 nFillBoxCount 个中
{
TRACE("%c-->%c, %d\n", chFlg[nRevIndx], chFlg[nFillBoxCount], nNum[nRevIndx]);
nNum[nFillBoxCount] += nNum[nRevIndx];
nNum[nRevIndx--] = 0; }
for(int i = 0; i < BOX_NUM; i++ ) //输出最后结果
{
TRACE("%c=%d ", chFlg[i], nNum[i] );
}
}