Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรมหา N เลข Iccanobif แรกใน C++


ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมหา N เลข Iccanobif

เราจะได้รับจำนวนเต็ม งานคือหาเลข Iccanobif ณ ตำแหน่งนั้น เลข Iccanobif คล้ายกับเลขฟิบอนัชชี แต่ต่างกันที่เราบวกสองตัวเลขก่อนหน้าหลังจากกลับหลักเลขแล้ว

ตัวอย่างโค้ด

#include <bits/stdc++.h>
using namespace std;

int reverse_digits(int num) {
    int rev_num = 0;
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}

void icanobifNumbers(int N) {
    int first = 0, second = 1;
    if (N == 1) cout << first;
    else if (N == 2) cout << first << " " << second;
    else {
        cout << first << " " << second << " ";
        for (int i = 3; i <= N; i++) {
            int x = reverse_digits(first);
            int y = reverse_digits(second);
            cout << x + y << " ";
            int temp = second;
            second = x + y;
            first = temp;
        }
    }
}

int main() {
    int N = 12;
    icanobifNumbers(N);
    return 0;
}

เอาต์พุต

0 1 1 2 3 5 8 13 39 124 514 836