Pomoc z programem w c++

0

Witam Panów, mam takie zadanie: napisz program, który wylosuje z przedziału od 100 do 200 10 liczb niepowtarzalnych i wyświetli najmniejszą i największą liczbę z wylosowanych.
Na razie mam takie coś, nie wiem jak się zabrać za dalszą część. Poprawi ktoś?
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
int m1=201,m2=201;
int main()
{
srand(time(NULL));
int n=10;

for(int i=1;i<=n;i++)
{
    int x=rand()%101+100;
    cout<<x<<" ";
    if(x<m1)
    {
        m2=m1;
        m1=x;
    }
    else if(x<m2)m2=x;
 
}
  return 0;
0

@Patryk1963: No ogólnie to zadanie masz prawie gotowe, tylko nie rozumiem za bardzo co się dzieje w bloku "if-else".

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));
    
    int n=10;
    int min=201, max=100;

    for(int i=1; i<=n; i++)
    {
        int x=rand()%101+100; 
        
        cout<< x <<" ";

        if(x < min)
            min = x;
        else if(x > max)
            max = x;
    }
    
    cout << endl;
    cout << min << endl;
    cout << max << endl;
    
  return 0;
}
1
Eldorad O. napisał(a):

@Patryk1963: No ogólnie to zadanie masz prawie gotowe, tylko nie rozumiem za bardzo co się dzieje w bloku "if-else".

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));
    
    int n=10;
    int min=201, max=100;

    for(int i=1; i<=n; i++)
    {
        int x=rand()%101+100; 
        
        cout<< x <<" ";

        if(x < min)
            min = x;
        else if(x > max)
            max = x;
    }
    
    cout << endl;
    cout << min << endl;
    cout << max << endl;
    
  return 0;
}

Dzięki wielkie, trzeba się uczyć na błędach pozdrawiam :)

5
#include <set>
#include <random>
#include <iostream>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(100, 200);
    std::set<int> numbers;
    while (numbers.size() < 10) {
        numbers.insert(dis(gen));
    }
    std::cout << *numbers.begin() << " " << *numbers.rbegin() << std::endl;
}
0
#include <iostream>
#include <algorithm>
#include <random>
#include <vector>

using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T> & v)
{
    if(!v.empty())
    {
        typename vector<T>::const_iterator it = v.begin();
        for(;(it!=v.end())&&(os<<*it);(++it),((it<v.end())?(os<<", "):(os<<" ")) ){}
    }
    return os;
}

int main()
{
    constexpr int first{1}, last{20}, n{10};
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dist(first, last);
    vector<int> v;
    v.reserve(n);
    std::vector<int>::iterator it, itf, itl;

    for(int i{0}; i<n;)
    {
        auto el = dist(gen);
        it = std::find(v.begin(), v.end(), el);
        if(it==v.end()){v.push_back(el);++i;}

    }
    std::sort(v.begin(), v.end());
    cout<<v<<'\n';
    itf=v.begin(); itl=v.end()-1;
    cout<<"min = "<<*itf<<'\n';
    cout<<"max = "<<*itl<<'\n';
    return 0;
}
// Copyright (c) 2022 by OfEl. All Rights Reserved.
// Permission for personal, educational or non-profit use is granted provided
// this copyright and notice are included in its entirety and remains unaltered.
// All other uses must receive prior permission in written of the author of OfEl
2
#include <iostream>
#include <random>
#include <vector>
#include <numeric>
#include <algorithm>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());

    std::vector<int> v(101);
    std::iota(v.begin(), v.end(), 100);
    std::shuffle(v.begin(), v.end(), gen);

    auto r = std::minmax_element(v.begin(), v.begin() + 10);
    
    std::cout << *r.first << " " << *r.second << std::endl;
}

https://godbolt.org/z/jh53o8jnc

1 użytkowników online, w tym zalogowanych: 0, gości: 1