Shake it (C++) kkmd66

error prone

When inputting three numbers to judge the output, it should be noted that the number at this time may still be within the range of the maximum value, so a space is output, but after adding the step size, it exceeds the maximum range, and then you have output a space, so There will be formatting errors.

Description:

Are you afraid to check and recite English texts? At the beginning, there were 35 students in our class, with student numbers ranging from 1 to 35. The teacher will select several of them according to certain rules:

If the teacher only says a number n, such as 10, it means that students with student numbers from 1 to 10 (including 1 and 10) won the prize.
If the teacher says two numbers m and n, such as 10 and 20, it means that 11 students with student numbers from 10 to 20 are lucky. Note: m can be larger than n. For example, if the teacher says 20 and 10, start from the 20th classmate and decrease to the 10th class in turn. You cannot escape!
If the teacher says three numbers m, n and s, such as 5, 10 and 1, it means starting from student No. 5, with one student “interval” in the middle each time, that is, three students 5, 7, and 9 need to endorse. You may notice that the second case is just s equal to 0, that is, there is no gap in between.
Now you need to develop a program that simulates the teacher's roll call: the teacher inputs one, two or three numbers at will, and the student number of the student who needs to be endorsed will be output.

Input:

There are multiple sets of inputs.
One line for each set of input.
The number of numbers in each row is indeterminate, it may be one, or two, or three.
The numbers are separated by a space.
All numbers are greater than or equal to 0 and less than or equal to 35.
The program ends when there is only one digit 0 on a line.

Output:

Corresponding to each group of input, output the student number of the student who needs to endorse on the same line.
Separate student numbers with a space.
There cannot be a space after the last student number.
Student numbers start from 1!

Sample Input:

10
15 12
5 35 9
0

Sample Output:

1 2 3 4 5 6 7 8 9 10
15 14 13 12
5 15 25 35

#include <iostream>
#include "string"
#include "vector"

using namespace std;

/**
 * kkmd66
 * @return
 */

int main() {
    
    

    string str;


    while (true) {
    
    
        getline(cin, str);
        if (str == "0") {
    
    
            break;
        }

        //找输入了几个数
        vector<int> vector;
        int count = 0;
        for (int i = 0; i < str.size(); ++i) {
    
    
            if (str[i] == ' ') {
    
    
                count++;
            }
        }

        //存储数
        for (int i = 0; i < count + 1; ++i) {
    
    
            int temp = atoi(str.substr(0, str.find(' ')).c_str());
            vector.push_back(temp);
            str.erase(0, str.find(' ') + 1);
        }

        //判断输出
        if (vector.size() == 1) {
    
    
            for (int i = 1; i <= vector[0]; ++i) {
    
    
                if (i != vector[0])
                    cout << i << " ";
                else
                    cout << i << endl;
            }
        }

        //判断输出
        if (vector.size() == 2) {
    
    
            int a = vector[0], b = vector[1];
            if (a > b) {
    
    
                for (int i = a; i >= b; --i) {
    
    
                    if (i != b)
                        cout << i << " ";
                    else
                        cout << i << endl;
                }
            } else {
    
    
                for (int i = a; i <= b; ++i) {
    
    
                    if (i != b)
                        cout << i << " ";
                    else
                        cout << i << endl;
                }
            }
        }

        //判断输出
        if (vector.size() == 3) {
    
    
            int a = vector[0], b = vector[1], c = vector[2];

            if (a > b) {
    
    
                for (int i = a; i >= b;) {
    
    
                    cout << i;
                    i -= c + 1;
                    if (i >= b)
                        cout << " ";
                }
                cout << endl;
            } else {
    
    
                for (int i = a; i <= b;) {
    
    
                    cout << i;
                    i += c + 1;
                    if (i <= b)
                        cout << " ";
                }
                cout << endl;
            }

        }
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324140626&siteId=291194637