作业帮 > 综合 > 作业

C++程序设计 Output Positive Numbers

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/16 14:14:29
C++程序设计 Output Positive Numbers
Description
Please wirte a program to read integers from the keyboard and print all the positive numbers out.Each number is in the scope of the int type.
Input
The input contains two lines.The first line is a number that indicates how much numbers to be read.Then the numbers are listed in the second line separated by white spaces.There are no more than 1000 numbers to be read.
Output
Print out all the positive numbers in one line and there should be one white space between two numbers.
Note:there should be no white space in the end of the output.
Sample Input
3
12 13 -1
Sample Output
12 13
#include<iostream>
using namespace std;
int main()
{
    bool isFirst = true;
    int totalNum, item;
    
    //读入数量
    cin >> totalNum;
    while(totalNum--)
    {
        cin >> item;
        //做题只要输出和结果一致就可以了,不需要保证输入和输出分开
        if (item > 0)
        {
            //保证输出最后没有空格
            if (isFirst)
            {
                cout << item;
                isFirst = false;
            }else{
                cout << " " << item;
            }
        }
    }
    cout << endl;
    return 0;
}