Posted: Thu Apr 13, 2006 2:35 pm
Yes, I know this just "BigInt arithmetic" problem, but I've end-minded about how to change it... This's actually my first approach on BigInt problem and it's so made me stress... 

Code: Select all
#include <stdio.h>
int Divide(char hasil[],int F,char out[])
{
int i, j, x;
x=0; i=0;
while(hasil[i])
{
x *= 10;
x += hasil[i++]-'0';
if(x>=F) break;
}
j=0;
do
{
if(x>=F)
{
out[j++] = (x/F)+'0';
x = x%F;
}
else out[j++]='0';
if(hasil[i]=='\0') break;
x *= 10;
x += hasil[i++]-'0';
}
while(1);
out[j]='\0';
return x;
}
int main()
{
char result[10];
int remainder;
remainder = Divide("123",7,result);
printf("result = %s\n",result);
printf("remainder = %d\n",remainder);
}
This input is not correct as the numbers should be at most 10^20.Jemerson wrote:Input:Code: Select all
1 16 17432479801732498109834891047810 1 15 17432479801732498109834891047810
Code: Select all
/*code has been removed
after accepted*/
Code: Select all
while(carry)
{
mod=carry%10;
add[i]=mod+'0';
carry/=10;
i++; // You missed this line
}
Code: Select all
#include <iostream>
#include <string>
using namespace std;
string sum(string s1, string s2)
{
string s3;
int l1 = s1.length();
int l2 = s2.length();
int ptr1 = l1 - 1, ptr2 = l2 - 1, q = 0;
while( ptr1 >= 0 || ptr2 >= 0 )
{
if(ptr1 >= 0)
q += s1[ptr1--] - '0';
if(ptr2 >= 0)
q += s2[ptr2--] - '0';
s3 = (char)( q % 10 + '0') + s3;
q /= 10;
}
while( q )
{
s3 = (char)( q % 10 + '0') + s3;
q /= 10;
}
return s3;
}
string div(string s, int n)
{
string s2;
int q = 0;
for(int i = 0;i < s.length();i++)
{
q = q * 10 + s[i] - '0';
s2 += (char)( q / n + '0');
q = q % n;
}
int ptr = 0;
while(ptr < s2.length() - 1 && s2[ptr] == '0')
ptr++;
s2 = s2.substr(ptr, s2.length() - ptr);
return s2;
}
int main()
{
int n, f, test = 1;
while(cin >> n >> f, n || f)
{
if( test > 1)
cout << endl;
string s1 = "0", s2;
for(int i = 0;i < n;i++)
{
cin >> s2;
s1 = sum(s1, s2);
}
string dv = div(s1, f);
cout << "Bill #"<<test++<<
" costs "<<s1<<": each friend should pay "
<<dv<<endl;
}
return 0;
}
You are printing a blank line between cases. Why?After each test case, you should print a blank line.