达到了极限 [英] limits reached

查看:89
本文介绍了达到了极限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试以下问题:

问:考虑一个函数,对于给定的整数n,返回

写出0到n之间的所有数字时所需的数量。对于

例如:f(13)= 6。请注意f(1)= 1。


下一个最大的n是什么,f(n)= n。


要解决这个,我编写了以下程序:


#include< limits.h>

#include< stdio.h>

int main(void)

{

unsigned long long int i,n,count;

for(i = 1; i< = ULLONG_MAX ; i ++)

{

count = 0;

while((n =(i%10))> 10)

{

if(n == 1)

count ++;

i / = 10;

}

if(n == 1)

count ++;

if(i == count)

printf("%llu",i);

}

}


但是,程序(a.out)跑了超过45分钟!现在没有

任何输出。

尝试这样的程序是否可行?我应该如何处理这个

否则?


问候,

Senthil

http://sarovar.org/projects/uthcode/

Hi all,
I am trying with the following Question:
Q: Consider a function which, for a given whole number n, returns the
number of ones required when writing out all numbers between 0 and n. For
eg: f(13)=6. Notice that f(1)=1.

What is the next largest n such that f(n) =n.

To solve this, I wrote the following program:

#include<limits.h>
#include<stdio.h>
int main(void)
{
unsigned long long int i,n,count;
for(i=1;i<=ULLONG_MAX;i++)
{
count=0;
while((n=(i%10))>10)
{
if(n==1)
count++;
i/=10;
}
if (n == 1)
count++;
if( i == count)
printf("%llu",i);
}
}

But, the program (a.out) is running for more than 45 minutes! now without
any output.
Is it feasible to try out such a program? How should I approach this
otherwise?

Regards,
Senthil

http://sarovar.org/projects/uthcode/

推荐答案

ORSenthil Kumaran写道:
O.R.Senthil Kumaran wrote:
大家好,
我正在尝试以下问题:
问:考虑一个函数对于给定的整数n,返回在写出0到n之间的所有数字时所需的数量。对于
例如:f(13)= 6。注意f(1)= 1。

f(n)= n的下一个最大的n是什么。

为了解决这个问题,我编写了以下程序:

#include< limits.h>
#include< stdio.h>
int main(void)
{
unsigned long long int i, n,count;
for(i = 1; i< = ULLONG_MAX; i ++)
{= / = = 0;
而((n =(i%10))> ; 10)
{
if(n == 1)
count ++;
i / = 10;
}
if(n == 1)
count ++;
if(i == count)
printf("%llu",i);
}
}

但是,程序(a.out)运行超过45分钟!现在没有任何输出。
尝试这样的程序是否可行?我应该如何处理呢?
Hi all,
I am trying with the following Question:
Q: Consider a function which, for a given whole number n, returns the
number of ones required when writing out all numbers between 0 and n. For
eg: f(13)=6. Notice that f(1)=1.

What is the next largest n such that f(n) =n.

To solve this, I wrote the following program:

#include<limits.h>
#include<stdio.h>
int main(void)
{
unsigned long long int i,n,count;
for(i=1;i<=ULLONG_MAX;i++)
{
count=0;
while((n=(i%10))>10)
{
if(n==1)
count++;
i/=10;
}
if (n == 1)
count++;
if( i == count)
printf("%llu",i);
}
}

But, the program (a.out) is running for more than 45 minutes! now without
any output.
Is it feasible to try out such a program? How should I approach this
otherwise?



提示:ULLONG_MAX在您的实施中的价值是多少?


HTH,< br $> b $ b - g

-

Artie Gold - 德克萨斯州奥斯汀


如果你认为不重要,你就不会注意了。


Hint: What is the value of ULLONG_MAX on your implementation?

HTH,
--ag

--
Artie Gold -- Austin, Texas

"If you don''t think it matters, you''re not paying attention."


On Sun,2004年10月24日15:39:43 - 0500,Artie Gold写道:
On Sun, 24 Oct 2004 15:39:43 -0500, Artie Gold wrote:
提示:ULLONG_MAX在您的实现中的价值是多少?
Hint: What is the value of ULLONG_MAX on your implementation?




最大值 - 无符号long long int: 18446744073709551615

好​​吧,我最初发布的程序是错误的。对于上面的

问题,这是正确的程序:


/ *考虑一个函数,对于给定的整数n,返回

写出0到n之间的所有数字时所需的数量。对于

例如:f(13)= 6。请注意f(1)= 1。

下一个最大的n是什么,f(n)= n * /


#include< limits。 h>

#include< stdio.h>

unsigned long long int countones(unsigned long long int); int main(void)

{

unsigned long long int i,cn;


for(i = 1; i< = ULLONG_MAX; ++ i)

{

cn = countones(i);

if(i == cn)

printf("%d",i);

}


返回0;

}


unsigned long long int countones(unsigned long long int i){

static unsigned long long int count = 0; int digit;


while((i / 10)> = 1)

{

digit = i%10;


if(digit == 1)

count ++;

i / = 10;

}

if(i == 1)

count ++;


返回计数;

}

问题仍然存在,我理解无符号长整数是一个

巨大的数字,但我想尝试直到极限获得

解决这个问题。这需要很长时间,而且还没有达到执行结束时的
!(单独留下结果)。你有没有尝试过这个?b / b
Senthil

http://sarovar.org/projects/uthcode/


或Senthil Kumaran写道:
O.R.Senthil Kumaran wrote:
On Sun,2004年10月24日15:39:43 -0500,Artie Gold写道:
On Sun, 24 Oct 2004 15:39:43 -0500, Artie Gold wrote:
提示:ULLONG_MAX的价值是多少?关于你的实现?

最大值 - 无符号长long int:18446744073709551615
Hint: What is the value of ULLONG_MAX on your implementation?

Maximum value - Unsigned long long int : 18446744073709551615




这是(如下所述)一个巨大的数字。<嗯,我最初发布的程序是错误的。对于上面的问题,这是正确的程序:

/ *考虑一个函数,对于给定的整数n,返回写出时所需的数量的函数0到n之间的所有数字。对于
例如:f(13)= 6。请注意,f(1)= 1。
下一个最大的n是什么,f(n)= n * /

#include< limits.h>
#include< ; stdio.h>
unsigned long long int countones(unsigned long long int); int main(void)
{
unsigned long long int i,cn;

for(i = 1; i< = ULLONG_MAX; ++ i)
{
cn = countones(i);
if(i == cn)
printf("%d",i);
尝试:

{

printf("%d",i);

fflush(stdout);

}

否则你将看不到输出 - 并且可能会发生坏事

优先。

}

返回0;
}

unsigned long long int countones(unsigned long long int i){
static unsigned long long int count = 0; int digit;

while((i / 10)> = 1)
{
digit = i%10;

if(digit = = 1)
count ++;
i / = 10;
}
if if(i == 1)
count ++;

返回计数;
}
问题仍然存在,我理解unsigned long long integer是一个很大的数字,但是我想尝试直到极限才能得到
解决方案问题。这需要很长时间,而我还没有达到执行结束的程度!(单独留下结果)。你有没有其他方法试试这个?



This is (as you state below) a HUGE number.
Well, the program I posted initially was a wrong one. For the above
problem, here is the correct program:

/*Consider a function which, for a given whole number n, returns the
number of ones required when writing out all numbers between 0 and n. For
eg: f(13)=6. Notice that f(1)=1.
What is the next largest n such that f(n) =n */

#include<limits.h>
#include<stdio.h>
unsigned long long int countones(unsigned long long int); int main(void)
{
unsigned long long int i,cn;

for(i = 1; i<= ULLONG_MAX; ++i)
{
cn = countones(i);
if( i == cn)
printf("%d",i); Try:
{
printf("%d ", i);
fflush(stdout);
}
otherwise you will see no output -- and Bad Things are likely to happen
first.
}

return 0;
}

unsigned long long int countones(unsigned long long int i) {
static unsigned long long int count = 0; int digit;

while((i/10) >= 1)
{
digit = i % 10;

if(digit == 1)
count++;
i /= 10;
}
if( i == 1)
count++;

return count;
}
The problem still remains,I understand unsigned long long integer is a
HUGE number, but I would like to try out till the limits to get the
solution to this problem. It is taking a long time and I have not
reached the end of execution yet!(leave alone the results). Do you have
any alternative to try out this?




对。


你正在执行循环(40亿x 40亿)次。

40亿秒约为120 *年*。


你看到了问题吗?


HTH,

- g

-

Artie Gold - 德克萨斯州奥斯汀


如果你认为不重要,你就不会注意了。



Right.

You are executing the loop approximately (4 billion x 4 billion) times.
4 billion seconds is approxiamtely 120 *years*.

Do you see the problem?

HTH,
--ag
--
Artie Gold -- Austin, Texas

"If you don''t think it matters, you''re not paying attention."


这篇关于达到了极限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆