Friday, 18 March 2016

Solution of URI 1099 :: Sum of Consecutive Odd Numbers II


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int N,X,Y,A,b,c=0;
    scanf("%d", &N);
    for(A=1;A<=N;A++)
    {
        scanf("%d%d",&X,&Y);
        if(X==Y)
        {
            c=0;
            printf("%d\n",c);
        }
        else if(X<Y)
        {
            for(b=X+1,c=0;b<Y;b++)
            {
                if(b%2==1||b%2==-1)
                    c+=b;
            }
            printf("%d\n",c);
        }
        else
        {
            for(b=Y+1,c=0;b<X;b++)
            {
                if(b%2==1||b%2==-1)
                    c+=b;
            }
            printf("%d\n",c);
        }
    }
    return 0;
}

7 comments:

  1. vai sob kisu ki input dekhe dekhe likhle hobe logic die koren jeno sob input ei kaj kore

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. By JAVA

    int test;
    Scanner sc=new Scanner(System.in);
    test=sc.nextInt();
    int j;

    for(j=1;j<=test;j++)
    {

    int first,sec,i,counta=0;
    first=sc.nextInt();
    sec=sc.nextInt();
    if(first==sec)
    {
    counta=0;
    }
    else if(first<sec)
    {
    if(first%2!=0)
    {
    for(i=first+2;i<sec;i=i+2)
    {
    counta=counta+i;
    }

    }
    else{
    for(i=first+1;i<sec;i=i+2)
    {
    counta=counta+i;
    }


    }
    }
    else
    {
    if(sec%2!=0)
    {
    for(i=sec+2;i<first;i=i+2)
    {
    counta=counta+i;
    }


    }
    else{
    for(i=sec+1;i<first;i=i+2)
    {
    counta=counta+i;
    }


    }
    }

    System.out.println(counta);

    }

    ReplyDelete
  4. Nesse codigom a variavel b seria pra que ??

    ReplyDelete
    Replies
    1. Please ask in English..

      Delete
    2. short

      #include

      int main()
      {
      int n,x,y,i,j,temp,sum=0;
      scanf("%d",&n);

      for(i=0;iy)
      {
      temp=y;
      y=x;
      x=temp;
      }
      for(j=x+1;j<y;j++)
      {
      if(j%2!=0)
      {
      sum=sum+j;
      }
      }
      printf("%d\n",sum);

      sum=0;
      }
      return 0;
      }

      Delete
  5. #include
    using namespace std;
    int main()
    {
    int n,x,y,sum;
    cin>>n;
    while(n--)
    {
    cin>>x>>y;
    if(x==y)
    cout<<"0\n";
    else if(x<y)
    {
    sum=0;
    for(int i=x+1; i<y; i++)
    {
    if(i%2!=0)
    sum=sum+i;
    }

    cout<<sum<<endl;
    }
    else
    {
    sum=0;
    for(int i=y+1; i<x; i++)
    {
    if(i%2!=0)
    sum=sum+i;
    }
    cout<<sum<<endl;
    }
    }
    }

    ReplyDelete

Note: only a member of this blog may post a comment.