我:
/*
A B C (3个编号的杯子) A 倒入B B倒入C C倒入A 循环倒100次 第100次的时候 A B C分别有多少果汁量。
输入三行 每行代表的是 杯子的容量和 有的果汁量。
最后输出结果。
*/
int a[3][2] = { 0 };
for (int i = 0; i <3; i++)
{
int x, y;
cin >> x >> y;
a[i][0] = x;
a[i][1] = y;
}
int count=0;
for (int i = 0; i < 100; i++)
{
int currentIndex = i % 3;
int nextIndex = 0;
if (i%3 == 2) {
nextIndex = 0;
}
else {
nextIndex = i%3 + 1;
}
int tempNextContainer = a[nextIndex][0];
int tempNextJuice = a[nextIndex][1];
int tempJuice = a[currentIndex][1];//原有容量
int result = tempNextContainer - tempNextJuice - tempJuice;
a[currentIndex][1] = result >= 0 ? 0 : -result;
a[nextIndex][1] = result >= 0 ? (tempNextJuice + tempJuice) : tempNextContainer;
}
for (int i = 0; i < 3; i++)
{
cout<<a[i][1]<<endl;
}
大佬:
int ar[3] = {0}; // 表示杯子容量
int br[3] = {0}; // 当前杯子中的果汁量
int cnt = 0;
int tp;
for(int i = 0; i < 3; i++) {
cin >> ar[i] >> br[i];
}
// 把地 i 号杯子中的果汁,导入第 j 个杯子
// 起始时,把地第 0 号杯子的果汁,导入第 1 号杯子
for(int i = 0, j = 1; i < 3, j < 3; i++, j++) {
// 第 j 个杯子未满且第 i 个杯子中有果汁
if(br[j] < ar[j] && br[i] > 0) {
tp = br[j]; // 暂存第 j 个杯子的果汁量 --> 目前有这些
br[j] = ar[j]; // 第 j 个杯子中,最多存 ar[j] 的果汁
// ar[j] - tp --> 在地 j 个杯子中,最多能存这么多
// 现有果汁 - 最多能存多少
br[i] = br[i] - (ar[j] - tp);
if(br[i] < 0) { //
br[j] = br[j] + br[i];
br[i] = 0; // 把第 i 个杯子中的果汁,全部倒入第 j 个杯子
}
cnt++;
}
if(100 == cnt) {
break;
}
if(2 == i) {
i = -1;
}
if(2 == j) {
j = -1;
}
}
for(int i = 0; i < 3; i++) {
cout << br[i] << endl;
}