16,471
社区成员
发帖
与我相关
我的任务
分享
#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] );
}
}