C++:计算有n种情况a,b,c中任意一个使之成为最大数需要额外加多少

题目概述:
有n种情况,向三人投票,总票数分别为a,b,c。如果使其中一人票数超过其他人,则需要多少票。
编程:
#include< iostream>
using namespace std;
class shu
{
public:
void get();
void input();
int n, a, b, c;
};
void shu::get()
{
int max;
if (a != b && a != c && b != c)
{
if (a > b)
max = a;
else
max = b;
if (max > c)
max = max;
else
max = c;
if (max == a)
{
a = 0;
b = max - b + 1;
c = max - c + 1;
}
if (max == b)
{
a = max - a + 1;
b = 0;
c = max - c + 1;
}
if (max == c)
{
a = max - a + 1;
b = max - b + 1;
c = 0;
}
}
if (a == b && a == c && b == c)
{
a = 1;
b = 1;
c = 1;
}
cout << a << " " << b << " " << c << endl;
}
void shu::input()
{
cin >> a >> b >> c;
}
int main()
{
shu str[10000];
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
str[i].input();
}
for (int i = 0; i < n; i++)
{
str[i].get();
}
return 0;
}
上机实践:
在这里插入图片描述
感悟:
想要同时输出N种情况的话用C++类还是方便一点,如果用C的话不好将每组数记忆,存储的话相比C++太过于繁琐。

Supongo que te gusta

Origin blog.csdn.net/qq_50426849/article/details/121234459
Recomendado
Clasificación