hello,
this is my exam's Question and it should run in 1 min for 1000000000000000(15 zeros) but It takes a long time what should i do? (the Question is: how many numbers does (1 - N) have if we cin any N between 1 and 1o^15.for example n=11: 1234567891011 has 13 numbers this program should run in less than 1 min for 10^15! )
#include<iostream>
int numof_num (long long int);
using namespace std;
int main()
{
int i, sum=0;
long long int n;
cin>>n;
for (i=1 ; i<=n ; i++)
sum+=numof_num (i);
cout<<sum<<endl;
return 0;
}
int numof_num (long long int i)
{
int count=0;
while (i>0)
{
i/=10;
count++;
}
return count;
}
It looks to me like he is trying to get the sum of the digits of all numbers from 1 to N. If I am right this is impossible to do in a reasonable amount of time by testing every number. You need to think of a solution that doesn't rely on brute force.
You can threads to improve the performance, the task is well suited to parallel computation.
I highly doubt the OP could do that. There is probably a way to complete this in well under a minute. There is also probably a way to complete this in well under a second.
Figure it out.
Actually the problem was to count the number of digits displayed when a program counts from 1 to N.
I've solved it for O(logN), so tell me if you get it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "stdio.h"
#include "stdlib.h"
longlong tenchart[20]={0,1};
inlinelonglong ten(int x){return tenchart[x+1];}
longlong ndct(longlong n,int x){
if(n<10)return n*(x+1)*ten(x);//number of digits
elsereturn ndct(n/10,x+1)//major tens
-(ten(x+1)-1)//the 1 to 10^X-1 have one less digit
+n%10*(x+2)*ten(x);//remainder
}
int main(){
char c[100];
int i=1;
longlong n;
while(++i < 20)tenchart[i]=tenchart[i-1]*10;
sscanf(fgets(c,100,stdin),"%Ld",&n);
printf("%lld\n",ndct(n,0));
}
thank you so much guys! I didnt have access to internet today something went wrong in university. but I am reading the answers now.and I am a she not he! I just thougth of something while I was at university what do you think about this code ?
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, int *argv[])
{
long long int x, y, w;
int m[40], k[40];
int a = 0;
int b = 0;
int i=0;
cout << "enter number";
cin >> y;
while ( i < 30)
{
m[i] = 0;
k[i] = 0;
i++;
}
w = y/3000;
x = 2101500* w;
for (int i = 0; i < 30; i++)
{
m[i] = x % 10;
x /= 10;
}
a = 0;
b = 0;
for (int i = 0; i < 30; i++)
{
b = (k[i] + m[i] + a) / 10;
k[i] = (k[i] + m[i] + a) % 10;
a = b;
}
for (int i = 0; i < 30; i++)
m[i] = 0;
x = ((w * (w - 1)) / 2) * 14 * 3;
for (int i = 0; i < 30; i++)
{
m[i] = x % 10;
x/= 10;
}
while (int i = 29 && i >= 5)
{
m[i] = m[i - 5];
i--;
}
for (int i = 0; i < 5; i++)
m[i] = 0;
a = 0;
b = 0;
for (int i = 0; i < 30; i++)
{
b = (k[i] + m[i] + a) / 10;
k[i] = (k[i] + m[i] + a) % 10;
a = b;
}
for (int i = 0; i < 30; i++)
m[i] = 0;
x= 0;
for (long long int i = 3000* w + 1; i <= y; i++)
{
if (i % 3 == 0 || i % 5 == 0)
x += i;
}
for (int i = 0; i < 30; i++)
{
m[i] = x % 10;
x /= 10;
}
a = 0;
b = 0;
for (int i = 0; i < 30; i++)
{
b = (k[i] + m[i] + a) / 10;
k[i] = (k[i] + m[i] + a) % 10;
a = b;
}
for (int i = 0; i < 30; i++)
m[i] = 0;
for (int i = 29; i >= 0; i--)
{
if (k[i] != 0)
{
for (int j = i; j >= 0; j--)
cout << k[j];
cout << endl;
break;
}
}
@kbw: sry sir he is not doing my home work its problem I couldnt solve and I asked for help !and Im working on my homework myself too. thanks for help any way