Re: 10013 - Super Long Sums
Posted: Fri Jun 08, 2012 10:03 pm
On the sample I/O, you are missing the newline at the end of the last line of the output.
Code: Select all
#include <stdio.h>
long a[1000001];
long b[1000001];
long sum[1000001];
int main(){
int count;
int digit[1000];
digit[0] = 0;
int size = 0;
scanf("%d", &count);
printf("\n");
for(int i = 0; i < count; i++){
scanf("%d", &digit[i+1]);
digit[i+1] += digit[i];
for(int j = digit[i]; j < digit[i+1]; j++){
scanf( "%d %d", &a[j+1], &b[j+1] );
}
digit[i+1] += 1;
printf("\n");
}
for(int i = 0; i < count; i++){
int j = digit[i+1]-1;
int temp;
do
{
if(sum[j+1] == 10){
sum[j+1] = 0;
sum[j] += 1;
}
if( a[j] + b[j] >= 10 ){
temp = (a[j] + b[j]) % 10;
sum[j] += temp;
sum[j -1] = 1;
}
else{
sum[j] += a[j] + b[j];
}
j--;
}while (j != digit[i]);
}
for(int i = 0; i < count; i++){
for(int j = digit[i]; j <digit[i+1]; j++){
if(j == digit[i] && sum[j] == 0){
j++;
}
printf("%d", sum[j]);
}
if(i != count - 1){
printf("\n\n");
}
else{
printf("\n");
}
}
return 0;
}
Code: Select all
#include<stdio.h>
#include<string.h>
int num1[1000010],num2[1000010],sum[1000015];
int main()
{
long tc;
scanf("%ld",&tc);
while(tc--)
{
//printf("\n");
long i,j,k,m,n,rem;
scanf("%ld",&n);
for(i=0; i<n; i++)
scanf("%d %d",&num1[i],&num2[i]);
rem=0;
for(i=n-1; i>=0; i--)
{
m=num1[i]+num2[i]+rem;
sum[i]=m%10;
rem=m/10;
}
/*if(rem!=0)
{
printf("%ld",rem);
}*/
for(i=0; i<n; i++)
printf("%d",sum[i]);
if(tc)printf("\n");
}
return 0;
}
Code: Select all
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner inp = new Scanner(System.in);
long n,m,x,y,i;
n = inp.nextLong();
while(n>0)
{
String a = new String();
String b = new String();
m = inp.nextLong();
for(i =0; i<m; i++)
{
x = inp.nextLong();
y = inp.nextLong();
a += ""+x;
b += ""+y;
}
BigInteger aa = new BigInteger(a);
BigInteger bb = new BigInteger(b);
System.out.println(aa.add(bb));
n--;
if(n>0)
System.out.println();
}
}
}
Code: Select all
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner inp = new Scanner(System.in);
long n,m,x,y,i;
n = inp.nextLong();
while(n>0)
{
String a = new String();
String b = new String();
m = inp.nextLong();
for(i =0; i<m; i++)
{
x = inp.nextLong();
y = inp.nextLong();
a += ""+x;
b += ""+y;
}
BigInteger aa = new BigInteger(a);
BigInteger bb = new BigInteger(b);
System.out.println(aa.add(bb));
n--;
if(n>0)
System.out.println();
}
}
}
Code: Select all
#include <stdio.h>
short int sum[1000001],crry[1000001];
unsigned char numstr[1000001];
int main()
{
int i,t,n,c,f=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
i=n;
while(i--)
{
scanf("%d",&c);sum[i]=c;scanf("%d",&c);sum[i]+=c;
if(sum[i]>9){sum[i]-=10;crry[i]=1;}
else crry[i]=0;
}
for(i=0;i<n;i++)
{
if(i>0) sum[i]+=crry[i-1];
if(sum[i]>9)
{
sum[i]-=10;
crry[i]++;
}
numstr[n-i-1]=sum[i]+48;
}
numstr[i]=0;
if(f) printf("\n%s\n",numstr);
else {printf("%s",numstr);f=1;}
}
return 0;
}