Problem Type :Number Theory
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100000
char * multiply(char [],char[]);
int main()
{
char a[MAX];
char b[MAX];
char *c;
int la,lb;
int i,j,coun;
while(scanf("%s %s",a,b)==2){
c = multiply(a,b);
int len = strlen(c);
for(i=0;i<len;i++)
{
if(c[i]!='0')
break;
}
coun=0;
//printf("%d\n",i);
for(j=i;j<len;j++)
{
printf("%c",c[j]);
coun =1;
}
if(coun ==0)
{
printf("0");
}
printf("\n");
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
memset(c,'\0',sizeof(c));
}
return 0;
}
char * multiply(char a[],char b[])
{
static char mul[MAX];
memset(mul,'\0',sizeof(mul));
char c[MAX];
char temp[MAX];
int la,lb;
int i,j,k=0,x=0,y;
long int r=0;
long sum = 0;
la=strlen(a)-1;
lb=strlen(b)-1;
for(i=0; i<=la; i++)
{
a[i] = a[i] - 48;
}
for(i=0; i<=lb; i++)
{
b[i] = b[i] - 48;
}
for(i=lb; i>=0; i--)
{
r=0;
for(j=la; j>=0; j--)
{
temp[k++] = (b[i]*a[j] + r)%10;
r = (b[i]*a[j]+r)/10;
}
temp[k++] = r;
x++;
for(y =0; y<x; y++)
{
temp[k++] = 0;
}
}
k=0;
r=0;
for(i=0; i<la+lb+2; i++)
{
sum =0;
y=0;
for(j=1; j<=lb+1; j++)
{
if(i <= la+j)
{
sum = sum + temp[y+i];
}
y += j + la + 1;
}
c[k++] = (sum+r) %10;
r = (sum+r)/10;
}
c[k] = r;
j=0;
for(i=k-1; i>=0; i--)
{
mul[j++]=c[i] + 48;
}
mul[j]='\0';
return mul;
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100000
char * multiply(char [],char[]);
int main()
{
char a[MAX];
char b[MAX];
char *c;
int la,lb;
int i,j,coun;
while(scanf("%s %s",a,b)==2){
c = multiply(a,b);
int len = strlen(c);
for(i=0;i<len;i++)
{
if(c[i]!='0')
break;
}
coun=0;
//printf("%d\n",i);
for(j=i;j<len;j++)
{
printf("%c",c[j]);
coun =1;
}
if(coun ==0)
{
printf("0");
}
printf("\n");
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
memset(c,'\0',sizeof(c));
}
return 0;
}
char * multiply(char a[],char b[])
{
static char mul[MAX];
memset(mul,'\0',sizeof(mul));
char c[MAX];
char temp[MAX];
int la,lb;
int i,j,k=0,x=0,y;
long int r=0;
long sum = 0;
la=strlen(a)-1;
lb=strlen(b)-1;
for(i=0; i<=la; i++)
{
a[i] = a[i] - 48;
}
for(i=0; i<=lb; i++)
{
b[i] = b[i] - 48;
}
for(i=lb; i>=0; i--)
{
r=0;
for(j=la; j>=0; j--)
{
temp[k++] = (b[i]*a[j] + r)%10;
r = (b[i]*a[j]+r)/10;
}
temp[k++] = r;
x++;
for(y =0; y<x; y++)
{
temp[k++] = 0;
}
}
k=0;
r=0;
for(i=0; i<la+lb+2; i++)
{
sum =0;
y=0;
for(j=1; j<=lb+1; j++)
{
if(i <= la+j)
{
sum = sum + temp[y+i];
}
y += j + la + 1;
}
c[k++] = (sum+r) %10;
r = (sum+r)/10;
}
c[k] = r;
j=0;
for(i=k-1; i>=0; i--)
{
mul[j++]=c[i] + 48;
}
mul[j]='\0';
return mul;
}
No comments:
Post a Comment