who are still thinking how to solve this problem, i got a tips to them..
Take input as string..
Check the length if it is grater then 10 then no need to convert it to number..
If the length is less then or equal to 10 then convert it to number.. and do required checking...
And the number type long double worked well for me..
--> sample input given by mf is very important..
try_try_try_try_&&&_try@try.com
This may be the address of success.
Can anyone give me some critical cases for this problem?
can there be input like this: -000001234+ -200001 ?
both numbers will be non negative according to the problem statement...is it?
take the input as string.
don't use big int(unless you use java),parse the numbers using sscanf and use LONG DOUBLE. then do what they say. be careful about input like A*0 A+-1 etc. thats all.
Try this without big int
Let a+b is to be tested
either a or b is too big, result is so.
but if neither are too big,the result can be big.
result will big if 2147483647-a<b holds
Same process apply on a*b
But be careful in case of either a or b is 0(zero)
I tried to do this "right" using strings but even though it works with all examples I've found on the web I always get WA. Can someone see what's wrong with my code?
(Note: I've given up on it and rewritten it to use double, but still it'd be nice to know why it failed).
I keep getting Runtime Error even when i have tried all the tests that ive found on the board and got the correct outputs.
I'm using double and before ive tried using Big Int and i was getting the same error. Can somebody help me and tell me what is wrong? The code seems good to me.
Thank you in advance!!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line[];
double a, b;
try {
while ((line = in.readLine().split(" ")) != null) {
a = Double.parseDouble(line[0]);
b = Double.parseDouble(line[2]);
System.out.println(line[0] + " " + line[1] + " " + line[2]);
if (a > (double) Integer.MAX_VALUE) {
System.out.println("first number too big");
}
if (b > (double) Integer.MAX_VALUE) {
System.out.println("second number too big");
}
if (line[1].equals("+")) {
if ((a + b) > (double) Integer.MAX_VALUE)
System.out.println("result too big");
} else if (line[1].equals("*")) {
if ((a * b) > (double) Integer.MAX_VALUE)
System.out.println("result too big");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
EDIT: here is the code using Big Int, wich i got also RE.
I try my best to find any faults in my cides, it's always RE, please help me, thans in advace !
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
//#define LOCAL
#define fin cin
#define fout cout
using namespace std;
inline void multiply(const vector<int> &a, const vector<int> &b, vector<int> &result)
{
if ( a.back() == 0 || b.back() == 0) return ;
result.resize(a.size() + b.size() + 2,0);
int d, digit;
for (vector<int>::size_type i = 0; i < a.size(); ++i)
{
d = 0;
vector<int>::size_type j;
for (j = 0; j < b.size(); ++j)
{
digit = a.at(i) * b.at(j) + result.at(i+j) + d;
result.at(i+j) = digit % 10;
d = digit / 10;
}
if (d >= 1) result.at(i + j) += d;
}
vector<int>::size_type i = result.size();
while (i >= 1)
if (result.at(i - 1) != 0) break;
else {result.erase(result.begin() + i - 1); --i;}
}
inline void add(const vector<int> &a, const vector<int> &b, vector<int> &sum)
{
int d = 0, digit;
vector<int>::const_iterator a_it = a.begin(), b_it = b.begin();
while (a_it != a.end() && b_it != b.end())
{
digit = *a_it + *b_it +d;
sum.push_back(digit % 10);
d = digit / 10;
++a_it;
++b_it;
}
while (a_it != a.end())
{
digit = *a_it + d;
sum.push_back(digit % 10);
d = digit / 10;
++a_it;
}
while (b_it != b.end())
{
digit = *b_it + d;
sum.push_back(digit % 10);
d = digit /10;
++b_it;
}
if (d == 1) sum.push_back(d);
}
inline bool is_overflow(const vector<int> &m)
{
int array[11] = {7,4,6,3,8,4,7,4,1,2};
vector<int> max_int( array, array + 10);
if (m.size() > max_int.size()) return true;
else if (m.size() < max_int.size()) return false;
else for (vector<int>::size_type i = m.size(); i >= 1; --i)
if (m.at(i) > max_int.at(i)) return true;
else if (m.at(i) < max_int.at(i)) return false;
return false;
}
int main()
{
#ifdef LOCAL
ifstream fin("in.cpp");
ofstream fout("out.cpp");
#endif
string s1, s2;
vector<int> a, b, result;
char mark;
while (fin >> s1 >> mark >> s2)
{
fout << s1 << " " << mark << " " << s2 << endl;
a.clear();
for (string::size_type i = s1.size(); i >= 1; --i)
a.push_back(s1.at(i - 1) - '0');
b.clear();
for (string::size_type i = s2.size(); i >= 1; --i)
b.push_back(s2.at(i - 1) - '0');
if (mark == '*') {
multiply( a, b, result);
if (is_overflow(a)) fout << "first number too big" << endl;
if (is_overflow(b)) fout << "second number too big" << endl;
if (is_overflow(result)) fout << "result too big" << endl;
} else {
add( a, b, result);
if (is_overflow(a)) fout << "first number too big" << endl;
if (is_overflow(b)) fout << "second number too big" << endl;
if (is_overflow(result)) fout << "result too big" << endl;
}
result.clear();
}
return 0;
}