zoukankan      html  css  js  c++  java
  • codeforces 500A. New Year Transportation

    题目链接:http://codeforces.com/problemset/problem/500/A

    题目意思:给出 n-1 个 cell,每个 cell 有一个值 ai,表示在这个编号为 i 的 cell,能到达i + ai 的cell,但不能反过来,即从 i+ai 到达 i 这个 cell。问从第一个cell 开始,是否可以到达 t 这个cell。

      第一次过不了pretest 是因为没有考虑到,如果 t = 1的情况,后来被人 hack 之后就不知道原因了。。。原来是因为第 n 个 cell,默认是 0,应该赋予一个很大的数值!!注意题目只给出 1 ~ n-1 cell 的 ai , 是没有给出 第 n 个的!!!所以要设值,否则代码中的while 会变成死循环的。

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int maxn = 3e4 + 10;
     9 
    10 int p[maxn];
    11 
    12 int main()
    13 {
    14     #ifndef ONLINE_JUDGE
    15         freopen("in.txt", "r", stdin);
    16     #endif // ONLINE_JUDGE
    17     int n, t;
    18     while (scanf("%d%d", &n, &t) != EOF)
    19     {
    20         for (int i = 1; i <= n-1; i++)
    21             scanf("%d", &p[i]);
    22         p[n] = maxn;         // 这个很关键!
    23         bool flag = false;
    24         int i = 1;
    25         int step = 1;        // 初始化为1方便进入while循环的if判断(有可能t==1)
    26         while (step <= n)
    27         {
    28             if (step == t)
    29             {
    30                 flag = true;
    31                 break;
    32             }
    33             step += p[i];
    34             i = step;
    35         }
    36         printf("%s
    ", flag ? "YES" : "NO");
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    判断图片是否存在,不存在则显示默认图片
    移动端开发时代理手机http访问查看效果(iphone)
    VS Code搭建TypeScript开发环境
    map,foreach和for的使用
    记一些常用的git命令
    js中创建对象的5种方法
    vuex简单使用
    记一下Slot的使用
    ES6中的箭头函数和普通函数有什么区别?
    计算属性、方法、侦听属性的区别
  • 原文地址:https://www.cnblogs.com/windysai/p/4196857.html
Copyright © 2011-2022 走看看