۱_الگوریتمی بنویسید که یک عدد در مبنای b را دریافت و آن را به مبنای 01 ببرد. - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

۱_الگوریتمی بنویسید که یک عدد در مبنای b را دریافت و آن را به مبنای 01 ببرد.

0 امتیاز
۱_الگوریتمی بنویسید که یک عدد در مبنای b را دریافت و آن را به مبنای 01 ببرد.

۲_الگوریتمی بنویسید که ۵۰ جمله از دنباله زیر موسوم به دنباله فیبوناچی را چاپ کند.

۳_الگوریتی بنویسید که یک عدد طبیعی را از ورودی دریافت و توان آن را به روش زیر محاسبه کند.
توان دوم عدد n = مجموع n عدد فرد از ۱
سوال شده آذر 2, 1399  بوسیله ی E rezaie (امتیاز 9)   1 1 1

1 پاسخ

0 امتیاز

1-الگوریتم تبدیل یک عدد از پایه b به پایه 2:

#include <iostream>
#include <vector>

std::vector<int> convert_to_base_2(int number, int base) {
    std::vector<int> binary_representation;
    while (number > 0) {
        binary_representation.push_back(number % 2);
        number /= 2;
    }
    return binary_representation;
}

int main() {
    int number, base;
    std::cout << "Enter the number: ";
    std::cin >> number;
    std::cout << "Enter the base: ";
    std::cin >> base;

    std::vector<int> binary_representation = convert_to_base_2(number, base);

    std::cout << "The binary representation of " << number << " in base " << base << " is: ";
    for (auto it = binary_representation.rbegin(); it != binary_representation.rend(); ++it) {
        std::cout << *it;
    }
    std::cout << std::endl;

    return 0;
}

2-الگوریتم چاپ 50 جمله از دنباله فیبوناچی:

#include <iostream>

int main() {
    int first = 0, second = 1, next;
    for (int i = 0; i < 50; ++i) {
        std::cout << "Fibonacci sequence " << i + 1 << ": " << first << std::endl;
        next = first + second;
        first = second;
        second = next;
    }

    return 0;
}

3-الگوریتم محاسبه توان مربع یک عدد n:

#include <iostream>

int calculate_square_power(int n) {
    int sum = 0;
    for (int i = 1; i <= 2 * n - 1; i += 2) {
        sum += i;
    }
    return sum;
}

int main() {
    int n;
    std::cout << "Enter a natural number: ";
    std::cin >> n;

    int square_power = calculate_square_power(n);
    std::cout << "The square power of " << n << " is " << square_power << std::endl;

    return 0;
}

 

پاسخ داده شده بهمن 13, 1401 بوسیله ی Ali_GH (امتیاز 368)   4 14 19
...