打印的一系列数字优化部2的 [英] print a series of numbers optimization part 2

查看:144
本文介绍了打印的一系列数字优化部2的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

earlyer我发布的第1部分并得到了一些有趣的responces

earlyer i posted part 1 and got some interesting responces

<一个href="http://stackoverflow.com/questions/5767916/print-a-series-of-numbers-optimization-part-1/5768809#5768809">print一系列的数字优化的一部分1

下面是另一种方式,你可以有程序打印重复的一系列数字在屏幕上,这里的目标是让最efficiant /最快的算法

here is another way you could have the program print a repeating series of numbers to the screen, the goal here is to make the most efficiant/fastest algorithm

int series[] = [2,3,4,5,6,7,8,9,1]
int i = 9;

while(true)
{
    print(series[i])
    i = series[i] - 1;
} 

当然,忽视实际打印数量产生的任何额外的开销,因为这是没有问题的目的

of course ignore any extra overhead created by actually printing the number because that is not the purpose of the problem

一个布尔条件语句(而真)是必需的无限循环是必需的,不管用什么办法,你做的,所以你可以忽略,太多

the one boolean conditional statement (while true) is required for the infinite loop is required no matter what solution you do, so you can ignore that too

此解决方案使用的内存为11 INT变量,但除此之外,它只做一个简单的计算和每次迭代一个变量赋值。

this solution uses memory for 11 int variables, but otherwise it only does one simple computation and one variable assignment per iteration.

所以会变成这样大部分时间efficiant的方式来解决infiniate数列问题?

so would this be the most time efficiant way to solve the infiniate number series problem?

推荐答案

我会说这是不是最有效的方式。

I would say it's not the most efficient way.

有参与处理阵列相乘。它本质上是

There's a multiplication involved in addressing the array. It's essentially

destinationAddress = baseAddressOfArray + indexRequested * sizeof(elementOfArray)

我认为最有效的方法是缓存一个迭代的串并简单地吐出这串了一遍又一遍。我不是在我确切的C ++语法,这将会是像

I think the most efficient way would be to cache the string of one iteration and simply spit out that string over and over again. I'm not up on my exact C++ syntax, it'd be something like

string s = "123456789";
while(true) {
    print(s);
}

这篇关于打印的一系列数字优化部2的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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