Remove the transfer itself from the dual system to the 10th in c+



  • For example, when 1011 is injected, you need to remove the line "2^0 + 2^1 + 23" You still have to use the do while

    int temp;
    cin >> temp;
    int base = 1;
    int k = -1;
    int n = 0;
    while (temp) {
        int last = temp % 10;
        temp = temp / 10;
        decVal += last * base;
        base = base * 2;
        k += 1;
        cout << '2^', k;
    }
    


  • Here's your job, it's easy, I wrote the comments so you could figure it out.

    #include <iostream>
    

    using namespace std;

    int main() {
    int temp;
    cin >> temp; // Ввели число
    int power = 0; // Текущая степень
    do {
    if (temp % 10 == 1) { // Если последняя цифра = 1
    cout << "2^" << power; // то выводим на экран 2^(текущая степень)
    if (temp / 10 > 0) cout << " + "; // Если это не последняя единица, то выводим "+"
    }
    power++; // Увеличиваем степень не зависимо от условия
    temp /= 10; // Переходим к следующей цифре
    } while (temp > 0); // Выполнять пока число не стало 0
    }



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2