程序生成的迭代 [英] Program to generate iterations

查看:129
本文介绍了程序生成的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,设置自己$ C $一个任务c这个算法,然而,由于我还没有在C充分的经验,我希望有可能是一个更简单的方法。

I have set myself a task to code this algorithm, however, since i haven't had a full experience in C, I was hoping there might be an easier method.

好吧,拿2号,
    A,B

Ok, Take 2 numbers, a , b

我们利用这些的总和,并添加到序列,然后添加在第二任期。

We take the sum of these and add to the sequence, then add on the second term.

所以,我们得到
    A,B,A + B,B

So we get a , b , a+b , b

有关下学期,我们采取的第二和第三值(我们所做的第一和第二前),并再次这样做。

For the next term, we take the 2nd and 3rd values (Before we did 1st and 2nd) and do it again.

所以我们现在得到

a , b , a+b , b , a +2b , a+b 

(续) A + 2B,B,A + 3b中,一个+ 2b中,式2a + 3b中,A + B

这将继续,直到何时何地,它没有必须有一个输入。

This continues on until whenever, it does not have to have an input.

基本上我的算法当属:下一个是previous B,下一步b是previous A +的previous b

Basically my algorithm comes as: The next a was the previous b , the Next b is the previous a + the previous b.

不过,我不能code。使用一般方法,因为它会从给出的顺序附加价值的工作,而不是第一,第二,第三值在C等。

However, i cannot code this in c using general methods, since it will work from the appended values given to the sequence, rather than the 1st , 2nd , 3rd values etc.

我在想,这可能是由线为第n个号码写入到文件和扫描行,而不是完成。不过,我觉得我一路过来这复杂

I was thinking that this could be done instead by writing to a file and scanning line by line for the nth number. However, i think i am way over complicating this.

有没有办法,我可以从这个顺序采取n个值的方法吗?

Is there a way that i can take an nth value from this sequence?

输入为:a = 1,B = 1。预期输出:1,1,2,1 3,2,3,1,4 3,5,2

Input: a = 1 , b =1 . Expected output: 1, 1 ,2 ,1 3, 2 , 3 ,1 ,4 3, 5 , 2

推荐答案

您应该使用数组,它并不难,它会比使用文件要好得多。看了一些资料,你应该能在5分钟使用它们。

You should use arrays, it is not difficult and it will be much better than using files. Read some documentation and you should be able to use them in 5min.

数组仅仅是一个东西,你的情况整数序列。
你也许可以做这样的事情:

An array is just a sequence of things, integers in your case. You probably can do something like this:

int main () {
    int array[MAX];
    array[0] = 1;
    array[1] = 1;
    // Print the starting values:
    printf("%d\n%d\n", array[0], array[1]);

    // The loop is controlled by i, increased in steps of 2.
    // j keeps the sequence of 1,2; 2,3; 3,4... used for the sum
    for (int i = 2, j = 0; i < MAX; i+=2, j++) {
        array[i] = array[j] + array[j+1];
        array[i+1] = array[j+1];
        printf("%d\n%d\n", array[i], array[i+1]);
    }
    return 0;
}

这篇关于程序生成的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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