牛客网NOIP赛前集训营-普及组(第七场) A 循环

题目链接:https://www.nowcoder.com/acm/contest/171/A
本题比较简单(我竟然好意思说)主要是把c分成出c>0,c=0,c<0三类。不要忘记c=0和c<0(我因此改了半天 哭)尤其是op=3,a=b,c=0;另外c=0时,不要对c取余和除。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int main()
{

     long long a,b,c,m;
     int op;
     scanf("%lld%d%lld%lld",&a,&op,&b,&c);
     if(op==1)
     {
          if(a>b)
            {
                printf("0\n");
                return 0;
            }
         if(c>0)
         {
              m=(b-a)/c+1;
             printf("%lld\n",m);
         }
         if(c<=0)
         {
             printf("-1\n");
            return 0;
         }
     }
     if(op==2)
     {
         if(a<b)
         {
              printf("0\n");
              return 0;
         }
         if(c>=0)
        {
             printf("-1\n");
            return 0;
         }
         else
         {
             c=-1*c;
              m=(a-b)/c+1;
              printf("%lld\n",m);
         }

     }
     if(op==3)
     {
         if(c==0&&a!=b)
            {
             printf("-1\n");
            return 0;
         }
         if(a==b)
         {
             printf("0\n");
             return 0;
         }
         if((b-a)%c!=0||(b-a>0&&c<0)||(b-a<0&&c>0))
         {
             printf("-1\n");
             return 0;
         }
         if(c>0)
          m=(b-a)/c;
             else
               {
                   c=-1*c;
                   m=(a-b)/c;
               }
                 printf("%lld\n",m);

     }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaoshazheng/article/details/83449494
今日推荐