You are not logged in. Please login at www.codechef.com to post your questions!

×

GCD2 : Code working fine , but getting wrong answer error from judge.Please Help

#include<bits/stdc++.h>
using namespace std;

int gcd(int a, int b)
{
if(b==0)
    return a;
else
    return gcd(b,a%b);
}

int main(){
int t,a,i,l; char n[251],b;
cin>>t;
while(t--){

    b=0;

    scanf("%d%s",&a,n);
    l=strlen(n);
    if(a==0){
        printf("%s\n",n);
        continue;
    }
    for(i=0;i<l;i++)
    b=(b*10+(n[i]-'0'))%a;
    cout<<gcd(a,b)<<endl;
}
return 0;
}
This question is marked "community wiki".

asked 12 Jun '15, 03:09

shubham99's gravatar image

2★shubham99
2403932
accept rate: 5%


Yes it will give you wrong answer since you have taken b as a char(in your main function) whereas you are supposed to treat it as a integer. Rest of your code is just perfect. You can see your corrected code here.

link

answered 12 Jun '15, 07:40

flappy's gravatar image

4★flappy
5154
accept rate: 36%

edited 12 Jun '15, 07:43

Ohh boy , I missed that b variable being declared under char data type... Thanks

(12 Jun '15, 23:41) shubham992★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,718
×1,911
×1,477
×849
×12

question asked: 12 Jun '15, 03:09

question was seen: 821 times

last updated: 12 Jun '15, 23:41