Wednesday, 13 April 2016

Solution of URI 1158 :: Sum of Consecutive Odd Numbers III


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, d, e;
    scanf("%d", &N);
    for(a=1; a<=N; a++)
    {
        scanf("%d %d", &X, &Y);
        if(X%2==1)
        {
            c=0;
            for(b=1; b<=Y; b++)
            {
                c+=X;
                X+=2;
            }
            printf("%d\n", c);
        }
        else
        {
            X++;
            c=0;
            for(b=1; b<=Y; b++)
            {
                c+=X;
                X+=2;
            }
            printf("%d\n", c);
        }
    }
    return 0;
}

5 comments:

  1. #include
    int main()
    {
    long int t,X,Y,sum=0,i,j,p;

    scanf("%ld",&t);
    for(i=1; i<=t; i++)
    {
    scanf("%ld %ld",&X,&Y);
    if(X%2 == 0)
    {
    p=X+1;
    for(j=1 ; j<=Y ; j++)
    {

    sum+=p;
    p+=2;
    }
    printf("%ld\n",sum);

    }
    else if(X% 2 != 0)
    {
    for(j=1; j<=Y ; j++)
    {
    sum+=X;
    X+=2;
    }
    printf("%ld\n",sum);
    }
    }
    return 0;
    }

    ReplyDelete
  2. what is the error ? i am getting 100% WA

    ReplyDelete
  3. #include
    int main()
    {
    int n,i,j,k,l,a,b,sum1=0,sum2=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    scanf("%d %d",&a,&b);
    if(a%2==0)
    {
    j=a+1;
    for(k=1;k<=b;k++)
    {
    sum1=sum1+j;
    j=j+2;
    }
    printf("%d\n",sum1);
    }
    else
    {
    j=a;
    for(l=1;l<=b;l++)
    {
    sum2=sum2+j;
    j=j+2;
    }
    printf("%d\n",sum2);
    }
    }
    return 0;
    }
    What is the error?????
    The Oj shows that it is 100% wrong...
    Help me plz......

    ReplyDelete
  4. I've done it in Java. It can find right answer. But It showed 5% error. Could you find it.

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int x, y, n, i, j, tot,t;
    n = in.nextInt();
    for (i = 0; i < n; i++) {
    x = in.nextInt();
    y = in.nextInt();
    t=0;
    tot=0;
    for(j=x;;j++){
    if(j%2==1){
    tot+=j;
    t++;
    if(t==y)
    break;
    }
    }
    System.out.println(tot);
    }
    }
    }

    ReplyDelete
  5. #include
    int main()
    {
    int i,N,j,X,Y,sum=0;
    scanf("%d",&N);
    for(i=1;i<=N;i++)
    {
    scanf("%d %d",&X,&Y);

    if(X%2==0)
    X=X+1;
    else
    X=X;

    for(j=1;j<=Y;j++)
    {
    sum+=X;
    X=X+2;
    }
    printf("%d\n",sum);
    X=0;
    Y=0;
    sum=0;

    }
    return 0;
    }



    reply.......please

    ReplyDelete

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