Uva 424-Integer Inquiry
/*
My First Big Integer Code in C++.
Verdict :: Accepted and Time: 0.000
*/
#include<bits/stdc++.h>
using namespace std;
int num[1009], numlen;
string x_y(string a, string b)
{
int alen=a.size(), blen=b.size(), sum, carry=0, i, j;
numlen=0;
i=alen-1;
j=blen-1;
for(; i>=0; i--)
{
sum = (a[i] - 48);
if(j>=0)
{
sum+=(b[j] - 48);
j--;
}
sum+=carry;
if(sum > 10)
{
num[numlen]=sum%10;
carry=sum/10;
}
else
{
num[numlen]=sum%10;
carry=sum/10;
}
numlen++;
}
if(carry)
{
num[numlen++]=carry;
}
string s="";
for(i=numlen-1; i>=0 ; i--)
{
//cout << num[i];
s+=(num[i]+48);
//cout << s << " ";
}
//cout << s << endl;
return s;
}
string eql(string a, string b)
{
int alen=a.size(), blen=b.size(), sum, carry=0, i, j;
numlen=0;
i=alen-1;
j=blen-1;
for(; i>=0 and j>=0; i--,j--)
{
sum = (a[i]-48) + (b[j]-48);
sum+=carry;
if(sum > 10)
{
num[numlen]=sum%10;
carry=sum/10;
}
else
{
num[numlen]=sum%10;
carry=sum/10;
}
numlen++;
}
if(carry)
{
num[numlen++]=carry;
}
string s="";
for(i=numlen-1; i>=0 ; i--)
{
s+=(num[i]+48);
//cout << s << " ";
}
//cout << s << endl;
return s;
}
int main()
{
string x,y="0";
//cin >> x;
while( cin >> x and x != "0")
{
if(y.size() > x.size())
{
swap(x,y);
}
//cout << "x = " << x << endl;
//cout << "y = " << y << endl;
if(x.size() > y.size())
{
y = x_y(x,y);
}
else
{
y = eql(x,y);
}
}
cout << y << endl;
return 0;
}
My First Big Integer Code in C++.
Verdict :: Accepted and Time: 0.000
*/
#include<bits/stdc++.h>
using namespace std;
int num[1009], numlen;
string x_y(string a, string b)
{
int alen=a.size(), blen=b.size(), sum, carry=0, i, j;
numlen=0;
i=alen-1;
j=blen-1;
for(; i>=0; i--)
{
sum = (a[i] - 48);
if(j>=0)
{
sum+=(b[j] - 48);
j--;
}
sum+=carry;
if(sum > 10)
{
num[numlen]=sum%10;
carry=sum/10;
}
else
{
num[numlen]=sum%10;
carry=sum/10;
}
numlen++;
}
if(carry)
{
num[numlen++]=carry;
}
string s="";
for(i=numlen-1; i>=0 ; i--)
{
//cout << num[i];
s+=(num[i]+48);
//cout << s << " ";
}
//cout << s << endl;
return s;
}
string eql(string a, string b)
{
int alen=a.size(), blen=b.size(), sum, carry=0, i, j;
numlen=0;
i=alen-1;
j=blen-1;
for(; i>=0 and j>=0; i--,j--)
{
sum = (a[i]-48) + (b[j]-48);
sum+=carry;
if(sum > 10)
{
num[numlen]=sum%10;
carry=sum/10;
}
else
{
num[numlen]=sum%10;
carry=sum/10;
}
numlen++;
}
if(carry)
{
num[numlen++]=carry;
}
string s="";
for(i=numlen-1; i>=0 ; i--)
{
s+=(num[i]+48);
//cout << s << " ";
}
//cout << s << endl;
return s;
}
int main()
{
string x,y="0";
//cin >> x;
while( cin >> x and x != "0")
{
if(y.size() > x.size())
{
swap(x,y);
}
//cout << "x = " << x << endl;
//cout << "y = " << y << endl;
if(x.size() > y.size())
{
y = x_y(x,y);
}
else
{
y = eql(x,y);
}
}
cout << y << endl;
return 0;
}
/*************************** Java Code ****************************************/
import java.util.*; import java.math.*; class Main { public static void main (String[] args) { Scanner in = new Scanner(System.in); BigInteger s=BigInteger.ZERO; while(in.hasNext()) { BigInteger n=in.nextBigInteger(); if(n.equals(BigInteger.valueOf(0))) { break; } s=s.add(n); //System.out.println(s); } System.out.println(s); } }
Comments
Post a Comment