题解 | #坠落的蚂蚁#

坠落的蚂蚁

https://www.nowcoder.com/practice/fdd6698014c340178a8b1f28ea5fadf8

按时间0.5s增加判断位置

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        double list[n][2], t, pos;
        int k, all = 0;
        for (int i = 0; i < n;  i++) {
            cin >> list[i][0] >> list[i][1];
            if (list[i][1] == 0) {
                k = i;
                pos = list[i][0];
            }
        }
        for (int i = 0; i < n;  i++) {
            if ((list[i][0] - pos)*list[i][1] < 0) {
                all += list[i][1];
            }
        }

        if (all == 0) {
            cout << "Cannot fall!" << endl;
        } else {
            for (t = 0; (!( list[k][0] == 100 || list[k][0] == 0)); t += 0.5) {
                int count = 0;
                for (int j = 0; j < n; j++) {
                    list[j][0] += list[j][1] * 0.5 ;
                    if (pos == list[j][0])
                        count++;
                }
                for (int i = 0; i < n; i++) {
                    if (count == 3 && list[i][0] == pos)
                        list[i][1] *= -1;
                    else
                        for (int j = i + 1; j < n; j++) {
                            if (list[j][0] == list[i][0]) {
                                int p = list[i][1];
                                list[i][1] = list[j][1];
                                list[j][1] = p;
                            }
                        }
                }
            }
            cout << t << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

吃就完事了:别找单做硬件的找做电源的,电源工程师
点赞 评论 收藏
分享
想玩飞盘的菠萝蜜在春...:上交✌🏻也拒?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务