C ++如何打印1-10之后,如果我输入其中之一,数字将消失

时间:2018-09-27 12:53:30

标签: c++ for-loop while-loop do-while

如果我输入其中之一,则如何打印1-10,该数字将消失。

ex:
Output: 1 2 3 4 5 6 7 8 9 10
Input:1
Output:2 3 4 5 6 7 8 9 10
Input:5
Output:2 3 4 6 7 8 9 10

(仅使用whiledo-whilefor)->(不使用数组)

3 个答案:

答案 0 :(得分:2)

SELECT * FROM student WHERE sID = (SELECT sID FROM result where subMARKS=MAX(subMARKS))

输出将是:

//let's say that the variable x contains the inputted number, 5 in this case

for (int i = 1; i <= 10; i++){
    if (i != x)
        printf("%d ", i);
}

答案 1 :(得分:2)

#include <bitset>
#include <iostream>

...

constexpr int N = 10;
std::bitset<N+1> mask {-1ul};

while (true) {
    int inp;
    cin >> inp;
    if (inp < 1 || inp > N)
        continue;

    mask.reset(inp);
    for (int i = 1; i < N; ++i) {
        if (mask.test(i)) {
            std::cout << i << '\n';
        }
    }
}

答案 2 :(得分:0)

我已经在VS2017上测试了此代码。我相信您会知道如何做的。当然,您可以提高源代码的效率。

#include "stdafx.h"
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    constexpr int iBitNum = 10;
    std::bitset<iBitNum + 1> mask;
    mask.set();
    int _size = mask.count();
    for (int i = 0; i < _size; ++i)
    {
        mask[i] = 0;
    }

    int inp = 0;    
    int b = 0;
    while (true)
    {
        cout << "Enter the number which you do not want to display" << endl;
        cin >> inp;
        cout << "Here is the result" << endl;
        for (size_t i = 0; i < iBitNum; i++)
        {
            if (i+1==inp)
            {
                continue;
            }
            b = mask[i] | i+1;
            cout << b << " ";
        }
        cout << endl;
    }

    return 0;
}