Infinite Sequence

原创 2016年06月02日 11:08:46

Description
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that si = b. Of course, you are the person he asks for a help.

Input
The first line of the input contain three integers a, b and c ( - 109 ≤ a, b, c ≤ 109) — the first element of the sequence, Vasya’s favorite number and the difference between any two neighbouring elements of the sequence, respectively.

Output
If b appears in the sequence s print “YES” (without quotes), otherwise print “NO” (without quotes).

Sample Input
Input
1 7 3
Output
YES
Input
10 10 0
Output
YES
Input
1 -4 5
Output
NO
Input
0 60 50
Output
NO
正确代码:

#include<iostream>
using namespace std;
int main(void)
{
    int a, b, c;
    cin>>a>>b>>c;
    if(c == 0)
    {
        if(a == b)
        cout<<"YES";
        else
        cout<<"NO";

    }
    else
    {
        if(((b-a)%c == 0) && (((b-a)>= 0 && c > 0)||((b-a)<= 0 && c < 0)))
        cout<<"YES";
        else
        cout<<"NO";
    }


    return 0;
}

以下为错误代码段:

#include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
    long long a, b, c;
    cin>>a>>b>>c;
    if(c == 0 && a == b)
    {
        printf("YES\n");
    }
    else if(c == 0 && a != b)
    {
        printf("NO\n");
    }
    else
    {
        long long t;
        t = (b-a)%c;
        //cout<<t;
        if(t == 0)
        {
            if((a > b && c < 0) || (a < b && c > 0 ))
            {
                printf("YES");
            }
            else
            {
                printf("NO");
            }
        }
        else
        {
            printf("NO");
        }
    }
    return 0;
}

个人认为两段代码,逻辑无差别,但是第二个就不对,,,请个位路过的朋友帮忙看看吧~

CodeForces 622A--F - Infinite Sequence

F - Infinite Sequence Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6...
  • gui951753
  • gui951753
  • 2016年03月13日 20:32
  • 385

【CodeForces】675A - Infinite Sequence(易错)

题目链接:点击打开题目 A. Infinite Sequence time limit per test 1 second memory limit per test 256 megaby...
  • wyg1997
  • wyg1997
  • 2016年05月26日 13:01
  • 359

codeforces 540 E. Infinite Inversions (离散化 + 树状数组)

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes...
  • whai362
  • whai362
  • 2015年09月30日 11:51
  • 382

树状数组Codeforces Round #301 (Div. 2) E - Infinite Inversions

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes ...
  • u010660276
  • u010660276
  • 2015年05月06日 21:49
  • 502

Codeforces 920G - List Of Integers 【容斥+二分】

Codeforces 920G - List Of Integers 【容斥+二分】 Let's denote as L(x, p) an infinitesequence of integers y...
  • my_sunshine26
  • my_sunshine26
  • 2018年02月06日 13:40
  • 46

an infinite sequence

Problem StatementNOTE: This problem statement contains subscripts that may not display properly if v...
  • aalbertini
  • aalbertini
  • 2009年08月19日 09:24
  • 502

Infinite Sequence

Infinite Sequence Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:2621...
  • hpulw
  • hpulw
  • 2016年03月12日 19:36
  • 334

Nth Digit

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: ...
  • u013325815
  • u013325815
  • 2016年09月23日 13:56
  • 956

关于LeetCode中Nth Digit一题的理解

题目如下: Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, .....
  • zsy112371
  • zsy112371
  • 2016年09月19日 21:52
  • 348

linux下安装与配置MySql

安装Mysql    1、下载MySQL的安装文件    安装MySQL需要下面两个文件:    MySQL-server-4.0.16-0.i386.rpm       MySQL-client-4...
  • amwpynnwxf
  • amwpynnwxf
  • 2007年11月13日 14:38
  • 277
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Infinite Sequence
举报原因:
原因补充:

(最多只允许输入30个字)