Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

How to construct a vector from a line of an unknown number of int // c++

Usually the number of inputs is given and i can do it with a for loop but not there : https://codeforces.com/contest/469/problem/A

I am trying desesperately to construct a vector from a line of ints , but i just know than the number of ints cannot be higher than 100. There s 2 lines of inputs.

If i make an infinite loop of Cin , how can i break it when it reaches an end of line or the end of the inputs ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, if you read the question carefully, the number of integers on each line is given.

The next line contains an integer p (0?≤?p?≤?n) at first, then follows p distinct integers a 1,?a 2,?...,?a p (1?≤?a i?≤?n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n.

p = Number of integers on each line.

Have a look at the following code which has Accepted code verdict on Codeforces:

#include<iostream> 
#include<set> 
#include<iterator> 


int main(){

    int N;
    std::cin>>N;

    std::set<int> uniqueNumbers;

    int t1, t2;
    int x;

    std::cin>>t1;
    for(int i=0;i<t1;i++){
        std::cin>>x;
        uniqueNumbers.insert(x);
    }
    
    std::cin>>t2;
    for(int i=0;i<t2;i++){
        std::cin>>x;
        uniqueNumbers.insert(x);
    }

    std::cout<< ((uniqueNumbers.size()==N) ? "I become the guy." : "Oh, my keyboard!");

    return 0;
}

Verdict:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...