zoj 1745 Are We There Yet?

本文介绍了一个使用C++实现的游戏寻物逻辑代码示例,通过输入游戏角色的位置和目标物品的位置来判断接近目标的状态(更暖、更冷、相同位置或找到目标)。代码中包含了主要的逻辑处理流程及状态判断方法。
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
string status(int*, int Pos, int i);
const int end = 5280;  // no more input if the first input is end
const int size =  21;
int main() {
  int cookiePos, a[size], isBetween = -1;
  while(cin >> cookiePos && cookiePos != end) {
  	isBetween++;
  	if(isBetween)
  	cout << endl;
    for (int i = 0; i < size; ++i)
    {
        a[i] = 0;
    }  // init value
    int step = 1;
    while (cin >> a[step] && a[step] != cookiePos) {
        step++;
    }   //a[step] == cookiepos when after loop
    for (int i = 1; i <= step; ++i)
    {
        cout << "Moving from "<< a[i-1] << " to " << a[i] << ": " << status(a, cookiePos, i) << endl;
    }
  }
    return 0;
}
string status(int* a, int Pos, int i){
    int last = abs(a[i-1] - Pos);
    int now = abs(a[i] - Pos);  // todo cast!!
    // string res = "";
    if (now == 0  )
    {
        return  "found it!";
    }
    else if (now == last)
    {
        return "same.";
    }
    else if (now > last)
    {
        return "colder.";
    }
    else 
    {
        return "warmer.";
    }
}



  always got a presentation error.  Fortunately,  referring to other's code, then I know I shoulda give a space between two blocks,  neither can be at  the beginning nor the end of the whole thing


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值