Is it true that any variable accepted as a char string and then scanned as int performs more quick mathematics operations such as pow() or log() function than a usual int input ?
A small doubt
Started by wiwbiz, Dec 25 2009 05:00 AM
2 replies to this topic
#1
Posted 25 December 2009 - 05:00 AM
|
|
|
#2
Posted 28 December 2009 - 09:47 AM
No, because to operate they all have to be converted to binary representation anyway. Besides, taking a string and converting it into an integer takes more time, so I'd say it's actually the opposite.
sudo rm -rf /
#3
Posted 29 December 2009 - 11:33 AM
TY for reply ( someone's listening ...)
Here's why I put this ;
There are 2 code for same problem:
2nd one:
To elaborate, both program convert a given int to sum of its half, one-third and one-forth if it is greater than the int itself.The half and one third and one forth are again put under same constrain as the number to convert them before adding.
The former works much faster than the later. I doubted it for scanning string for int or use of malloc function. I haven't gone through malloc yet. Is something else the reason for faster run of this program ? Please response soon this time. You might have guessed correctly, the later one is my code.
Here's why I put this ;
There are 2 code for same problem:
#include<iostream>
using namespace std;
long long *arr = NULL;
long long findTotalProfit(long long num)
{
if((num < 1000000) && (arr[num] != 0))
return arr[num];
long long dividedProfit = findTotalProfit(num / 2) + findTotalProfit(num / 3) + findTotalProfit(num / 4);
//cout << dividedProfit << endl;
if(dividedProfit > num)
{
if(num < 1000000)
arr[num] = dividedProfit;
return dividedProfit;
}
else
{
if(num < 1000000)
arr[num] = num;
return num;
}
}
int main()
{
long long num;
arr = (long long *)malloc(10000000);
for(long long i = 1; i < 12; i++)
arr[i] = i;
char s[20];
while(cin.getline(s, 20))
{
sscanf(s, "%lld", &num);
long long ans = findTotalProfit(num);
printf("%lld\n", ans);
}
return 0;
}
2nd one:
// Bytelandian Gold Coins
#include<iostream>
#include<ctime>
using namespace std;
long long *arr=NULL;
long long sum(long long n)
{
if((n<100000) && (arr[n]!=0))
return arr[n];
long long m=sum(n/2)+sum(n/3)+sum(n/4);
if(m>n)
{
if(n < 10000)
arr[n] = m;
return m;
}
else
{
if(n < 10000)
arr[n]=n;
return n;}
}
int main()
{
long long n;
int t,u;
arr =(long long *)malloc(1000000);
for(int i=0;i<12;i++)
arr[i]=i;
while(cin>>n)
if(n!=0)cout<<sum(n);
else cout<<0<<endl;
return 0;
}
To elaborate, both program convert a given int to sum of its half, one-third and one-forth if it is greater than the int itself.The half and one third and one forth are again put under same constrain as the number to convert them before adding.
The former works much faster than the later. I doubted it for scanning string for int or use of malloc function. I haven't gone through malloc yet. Is something else the reason for faster run of this program ? Please response soon this time. You might have guessed correctly, the later one is my code.


Sign In
Create Account


Back to top









