用C++求解Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number 11. Each person is looking through the circle's center at the opposite person.You don't know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number ais looking at the person with the number b (and vice versa, of course). What is the number associated with a person being looked at by the person with the number c? If, for the specified a, b, and c, no such circle exists, output -1.
时间: 2023-12-29 20:05:38 浏览: 52
C++数圈问题
这道题可以使用模拟的方法来解决。首先,我们可以假设人数为n,从11号开始逆时针编号为0, 1, 2, ..., n-1。那么第一个人a和第二个人b的距离为d1 = (b-a+n)%n,因为是逆时针,所以要加上n再取模。同理,第二个人b和第一个人a的距离为d2 = (a-b+n)%n。由题意可知,a和b相对位置对称,所以d1 = d2,即 (b-a+n)%n = (a-b+n)%n,整理得到 b = (2*a)%n。同理可得,c看到的人的编号为d = (2*c)%n,如果d等于b,则输出b,否则输出-1。下面是C++代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
int n = 2 * a;
if (b != (n % b)) {
cout << "-1" << endl;
} else {
int d = 2 * c % n;
if (d == b) {
cout << b << endl;
} else {
cout << "-1" << endl;
}
}
return 0;
}
```
阅读全文