Are We There Yet?

本文介绍了一个名为FindtheCookie的游戏模拟程序,该游戏在一个长而狭窄的走廊中进行,玩家需找到隐藏的饼干。通过一系列的移动指令,程序会告诉玩家是否离饼干更近或更远,直至最终找到饼干。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FJNU.1298

Description
Laurie's little brother Joey has been playing Find the Cookie with her. However, after the 32,767th time, Laurie is tired of it. She wants to write a program to keep the boy occupied. Write a program to simulate a game of Find the Cookie. The game takes place in a long narrow hall, so Joey can only move forward and backward, not right or left. At the beginning of each game, a cookie is placed in the hall (not at the center of the hall) and Joey starts at the center of the hall. Joey attempts to find the cookie. He does this by moving to another point in the hall, whereupon the computer tells him whether he is "warmer" (he has moved closer to the cookie than his last position), "colder" (he has moved farther away from the cookie than his last position), "same" (he has not moved closer or farther away from the cookie), or he has reached the cookie. Joey continues until he exactly reaches the location of the cookie, which always happens within 20 moves.

Input
Each input line represents a new game. Each input line contains at least two and at most 21 integers separated by whitespace. The integers represent locations along the hall, expressed in units of feet. Joey begins each game at location 0 feet. The first integer on an input line is the location of the cookie. This integer is guaranteed to be different from 0. The remaining integers represent locations Joey moves to, in order. Joey will never move more than 5280 feet from his original location. Joey will always reach the cookie in each game, and this will be the last move on the input line. Your program should stop processing input lines when the cookie is located at 5280 feet (a mile from the center of the hall is way too far for Joey to go for only one cookie).

Output
For each location that Joey moves to, determine whether he is warmer, colder, the same, or has reached the cookie. Have a blank line between the output for different input lines. Follow the format in the Sample Output.

Sample Input
5  10 11 12  3 4 5
3   10  10 7  3
12  5  -3  1   4  6 7 8 9 12
5280 10

Sample Output
Moving from 0 to 10: same.
Moving from 10 to 11: colder.
Moving from 11 to 12: colder.
Moving from 12 to 3: warmer.
Moving from 3 to 4: warmer.
Moving from 4 to 5: found it!

Moving from 0 to 10: colder.
Moving from 10 to 10: same.
Moving from 10 to 7: warmer.
Moving from 7 to 3: found it!

Moving from 0 to 5: warmer.
Moving from 5 to -3: colder.
Moving from -3 to 1: warmer.
Moving from 1 to 4: warmer.
Moving from 4 to 6: warmer.
Moving from 6 to 7: warmer.
Moving from 7 to 8: warmer.
Moving from 8 to 9: warmer.
Moving from 9 to 12: found it!

Source
2000 ACM Southeast Regional

My Program 

#include<iostream>
#include
<cmath>
using namespace std;

int main()
{
    
int cookie;
    
int x,y,a,b;
    
while(cin>>cookie)
    
{
        x
=0;
        
if(cookie==5280)
            
break;
        
while(cin>>y)
        
{
            cout
<<"Moving from "<<x<<" to "<<y<<"";
            
if(y==cookie)
                
break;
            a
=abs(x-cookie);b=abs(y-cookie);
            
if(a==b)
                cout
<<"same."<<endl;
            
else
                
if(a>b)
                    cout
<<"warmer."<<endl;
                
else
                    cout
<<"colder."<<endl;
            x
=y;
        }

        cout
<<"found it!"<<endl<<endl;
    }

    
return 0;
}

YOYO‘s Note:
判断cookie更接近起点还是终点……不知道是什么数据,居然到了0.99s???
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值