阅读程序 · 答案 1 32017 11 321 · 考点:输入输出、位运算、分支结构、循环结构
← 整套速查表 NOIP 提高组初赛 全部真题#include <iostream>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
int x = 1;
int y = 1;
int dx = 1;
int dy = 1;
int cnt = 0;
while (cnt != 2)
{
cnt = 0;
x = x + dx;
y = y + dy;
if (x == 1 || x == n)
{
++cnt;
dx = -dx;
}
if (y == 1 || y == m)
{
++cnt;
dy = -dy;
}
}
cout << x << " " << y << endl;
return 0;
} 输入 1:4 3 输出 1:_________(2 分) 输入 2:2017 1014 输出 2:_________(3 分) 输入 3:987 321 输出 3:_________(3 分)
看到反弹计数 cnt==2 停止 → 先想小球撞墙反弹逻辑
误判同时撞两墙才计数;每步先移动再判断边界
考点:程序阅读(反弹模拟) 程序在做什么:小球从 (1,1) 出发沿 45° 移动,撞墙反弹,撞两次角停止输出坐标。