GCD2
in spoj though seems to e bit of tedious and boring question , its really small and has some good coding concept behind it which can be used when we encounter a situation where need a a big string to be handled and at the same time we need to find the gcd of it.
solu
we can take the bigger num input as a string and while converting it number be do %a all the time , and apply the normal procedure. :P
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int gcd(int a,int b){
- if(b==0)
- return a;
- else
- gcd(b,a%b);
- }
- int main(){
- int t;
- while(t--){
- int a,i,len,b=0;
- char n[250];
- len=strlen(n);
- if(a==0){
- continue;
- }
- for(i=0;i<len;i++)
- b=(b*10+(n[i]-'0'))%a;
- }
- return 0;
- }
No comments:
Post a Comment