Rysowanie trapezu ze znaków X i ich sumowanie

0

Bardzo proszę o pomoc. Mam zadanie z informatyki z C++. Muszę zbudować trapez ze znaków "x" o podstawach, które będę podawać z klawiatury. Na końcu musi wyskoczyć mi ilość "x", z których składa się trapez.

Przykład:

Liczba 1: 2 <------ te liczby wpisuję
Liczba 2: 4

xx
xxx
xxxx

Suma wynosi: 9
Z góry dziękuję, bo męczę się z tym już 3 godziny. Do polecenia mogą być wykorzystane tylko komendy: cout, cin, int, using namespace std;, #include <iostream>, return 0; oraz for (I TYLKO FOR)

1

Pseudokod:


pobierasz a, b
for(int i = a; i <=b; i++) {
    for(int j = 0; j < i;j++) {
        print 'x';
    }
    print '\n'
}
1

Istnieje nieskończenie wiele trapezów o długościach podstaw a oraz b, zatem zadania nie da się wykonać poprawnie.

0

Nie wychodzi. Cały czas lecą piramidy z "x" i nie chcą się zatrzymać

0

Dostałeś gotowy kod rozwiązujący zadanie, zadaj sobie tyle trudu i wczytaj z klawiatury długości podstaw do zmiennych a i b.

0
#include <iostream>

int main()
{
  int a, b;
  int count = 0;
  
  std::cout << "Pass A: ";
  std::cin >> a;
  std::cout << "Pass B: ";
  std::cin >> b;
  
  for (int y = a; y <= b; y++)
  {
    for (int x = 0; x < y; x++)
      std::cout << "x";
      
    std::cout << "\n";
    count += y;
  }
  
  std::cout << "X count: " << count;
}

http://cpp.sh/7ais6

0

Przynudziłem wieczorem, więc prosz, wersja O(1), niestety z ograniczeniem max a i b (ustawione w mainie) (plus jak chcesz to dorób ogarnianie gdy max_a < max_b i zliczanie gwiazdek)

btw, ciekawie wygląda output na godbolcie (dałem scanf i printf, żeby nie zaśmiecać asma rzeczami ze strumieni)

Kod ogarnia case gdy wpiszesz a większe od b (ale jak mówiłem mieszczące się w max_a i max_b z kodu)
Żywe demo: https://ideone.com/OHED7g

#include <iostream>
#include <algorithm>

namespace algo
{
    template <typename it_t, typename d_it_t>
    constexpr auto copy(it_t first, it_t last, d_it_t dfirst)
    {
        while(first != last)
        {
            *dfirst++ = *first++;
        }
    }

    template <typename it_t, typename value_t>
    constexpr auto fill(it_t first, it_t last, const value_t& value)
    {
        while(first != last)
        {
            *first++ = value;
        }
    }
}

template <typename ty, size_t n>
class array
{
public:
    constexpr auto begin()
    {
        return m_arr;
    }

    constexpr auto end()
    {
        return m_arr + n;
    }

    constexpr auto begin() const
    {
        return m_arr;
    }

    constexpr auto end() const
    {
        return m_arr + n;
    }

    constexpr auto& operator[](size_t i)
    {
        return m_arr[i];
    }

    constexpr const auto& operator[](size_t i) const
    {
        return m_arr[i];
    }

    constexpr auto& operator=(const array& rhs)
    {
        algo::copy(rhs.begin(), rhs.end(), begin());
        return *this;
    }

private:
    ty m_arr[n]{};
};


template <size_t a, size_t b>
class trapeze
{
public:
    constexpr trapeze()
    {
        algo::fill(m_data.begin(), m_data.end(), ' ');
        m_data[a*b] = '\0';
    }

    constexpr auto& operator()(size_t i, size_t j)
    {
        return m_data[i*b + j];
    }

    constexpr auto data() const
    {
        return m_data.begin();
    }

private:
    array<char, a * b + 1u> m_data; //+1 for '\0'
};

template <size_t max_a, size_t max_b>
using trapeze_t = trapeze<std::max(max_a, max_b), std::max(max_a, max_b) + 1u>; //+1 for '\n'

template <size_t max_a, size_t max_b>
constexpr auto generate_trapeze(size_t a, size_t b)
{
    trapeze_t<max_a, max_b> trapeze;
    
    if(a <= b)
    {
        for(size_t i = a; i <= b; i++) {
            for(size_t j = 0; j < i ;j++) {
                trapeze(i - a, j) = '*';
            }
            trapeze(i, max_b) = '\n';
        }
    }
    else
    {
        for(size_t i = a; static_cast<int>(i) >= static_cast<int>(b); --i) {
            for(size_t j = 0; j < i; j++) {
                trapeze(a - i, j) = '*';
            }
            trapeze(i, max_b) = '\n';
        }
    }

    return trapeze;
}

template <size_t a, size_t b>
constexpr auto generate()
{
    constexpr auto max_a = a + 1u;
    constexpr auto max_b = b + 1u;
    using generated_trapezes_row_t = array<trapeze_t<max_a, max_b>, max_b>;
    using generated_trapezes_t = array<generated_trapezes_row_t, max_a>;

    generated_trapezes_t generated_trapezes;

    for(size_t i = 0u; i < max_a; ++i)
    {
        for(size_t j = 0u; j < max_b; ++j)
        {
            generated_trapezes[i][j] = generate_trapeze<max_a, max_b>(i, j);
        }
    }

    return generated_trapezes;
}

int main()
{
    constexpr auto max_a{ 10u };
    constexpr auto max_b{ 10u };
    constexpr auto trapezes = generate<max_a, max_b>();

    size_t a, b;

    std::cin >> a >> b;

    std::cout << trapezes[a][b].data();
}

EDIT
Nie doczytałem, że możesz używać tylko niektórych rzeczy, przepraszam.

0

No to żeby nie było nudno i ja... :-)

#include <iostream>
#include <string>

size_t trapeze(size_t start_size, size_t stop_size) {
    static size_t counter = 0;
    return start_size < stop_size ?
        counter += start_size,
        std::cout << std::string(start_size, '*') << '\n',
        trapeze(start_size + 1U, stop_size) :
        counter;
}

int main() {
    size_t a;
    size_t b;
    size_t count;
    std::cout << "Pass A: ";
    std::cin >> a;
    std::cout << "Pass B: ";
    std::cin >> b;
    count = trapeze(a, b);
    std::cout << "X count: " << count << '\n';
}

0

Mój sposób:

#include <iostream>
using namespace std;

int main()
{
    unsigned short a = 0;
    unsigned short b = 0;
    cout << "Podaj a: ";
    cin >> a;
    cout << "Podaj b: ";
    cin >> b;

    for (unsigned short i = a; i <= b; i++) {
        for (unsigned short j = 0; j < i; j++) {
            cout << "x";
        }
        cout << '\n';
    }
    unsigned short result = ((a + b) * (1 + b - a)) / 2;
    cout << "Total amount of X: " << result;
    return 0;
}

0

To w założeniu, że a >= b. Jeśli może zachodzić a < b to ten kod nie zadziała.

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