Re: 424 - Integer Inquiry RE
Posted: Thu Sep 23, 2010 9:00 pm
The memory limits are enough why use so small limits on arrays. Use 1000.
Code: Select all
#include<stdio.h>
#include<string.h>
void str_rev(char str[]);
int main()
{
char str[1001][1001],str3[1002];
int i,j,k,m,x=0,carry=0,max=0;
for(i=0;i<101;i++)
{
gets(str[i]);
if(str[i][0]=='0'&&str[i][1]=='\0')
break;
if(strlen(str[i])>=max)
max=strlen(str[i]);
}
j=i;
for(i=0;i<j;i++)
{
str_rev(str[i]);
for(k=strlen(str[i]);k<max;k++)
{
str[i][k]='0';
}
str[i][k]='\0';
str_rev(str[i]);
}
for(k=max-1,m=0;k>=0,m<max;k--,m++)
{
x=0;
for(i=0;i<j;i++)
{
x+=(str[i][k]-'0')+carry;
carry=0;
}
if(x>9)
{
carry=x/10;
str3[m]=x%10+'0';
}
else
str3[m]=x+'0';
}
str3[max]=carry+'0';
if((str3[max]-'0')==0)
str3[max]='\0';
else
str3[max+1]='\0';
str_rev(str3);
puts(str3);
return 0;
}
void str_rev(char str[])
{
char a;
int i,j,n;
n=strlen(str);
for(i=0,j=n-1;i<n/2&&j>=0;i++,j--)
{
a=str[i];
str[i]=str[j];
str[j]=a;
}
str[n]='\0';
}
Code: Select all
import java.util.*;
import java.math.*;
import javax.swing.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
BigInteger a,sum;
sum= BigInteger.ZERO;
while(true){
a= sc.nextBigInteger();
sum= sum.add(a);
if(a.intValue()==0)break;
}
System.out.print(sum) ;
}
}
Code: Select all
#include<stdio.h>
int main()
{
char inp[101],out[110];
int i,j,c,tempc;
for(i=0;i<110;i++)
out[i]='0';
while(scanf("%s",inp)==1)
{
for(i=0;inp[i]!='\0';i++);
for(c=0,j=109,i--;i>=0;i--,j--)
{
tempc=c;
c=(c+out[j]+inp[i]-96)/10;
out[j]=(tempc+out[j]+inp[i]-96)%10+48;
}
if(c)
out[j]=c+out[j];
if(inp[0]=='0'&&inp[1]=='\0')
{
for(i=0;out[i]=='0';i++);
j=i;
for(;i<110;i++)
printf("%c",out[i]);
printf("\n");
for(;j<110;j++)
out[j]='0';
}
}
return 0;
}
Code: Select all
#include<cstdio>
Code: Select all
code removed after AC
Thanks dude , I got the point . It was my first ever forum post , and it was really helpful . ACedbrianfry713 wrote:100
1
0