code is working fine in code::blocks but not in hackerblocks
Form biggest number
I am confused as to how you are writing the comparator function.
Let’s do some basic math and look at these statements.
int d1=log10(a);
int p1=pow(10,d1);
int fd1=a/p1;
Now a should be equal to be p1 if you assume that proper log exists and it’s not in decimals.
So fd1 in that case is 1.
Similarly fd2 may come out to be 1.
Now run this part
else if(fd1==fd2)
{
a=a%p1;
b=b%p2;
return comp(a,b);
}
So you are sending a=0 and b=0 in the comp function.
and again you will calculate log there and log (0) is not defined , so your code throws error.
If you still don’t get it try this test case
1
2
100 100
Please use this simpler comparator function
int myCompare(string X, string Y)
{
// first append Y at the end of X
string XY = X.append(Y);
// then append X at the end of Y
string YX = Y.append(X);
// Now see which of the two formed numbers is greater
return XY.compare(YX) > 0 ? 1: 0;
}
and store the numbers as an array or vector of strings.
cin>>n;
vector<string> arr;
for(int i=0;i<n;i++) {
string temp;
cin>>temp;
arr.push_back(temp);
}
sort(arr.begin(), arr.end(), myCompare);
for(int i=0;i<n;i++) {
cout<<arr[i];
}
see now i have modified my code
Please Share the link