Odwracanie liczb

0

Witam, robię zadanie http://pl.spoj.com/problems/TABLICA/
Według mnie mój program odwraca bez zarzutu, ale pewnie tak nie jest, bo spoj odrzuca.

#include <iostream>
using namespace std;
int main()
{
    string a;
    getline (cin, a);
    int b=a.length();

for (int i=b-1; i>=0; i--)
    cout<<a[i];
    return 0;
}
 
3

Dla 13 23 33 powinno zwrócić 33 23 13, ale twój program zwróci 33 32 31.
Słowo klucz: liczby. Chodzi o ciąg liczbowy, a nie prosty ciąg znaków.

//EDIT: Udostępniam gotowca, tyle na spoju przesiedziałem, niech się przynajmniej rozwiązania nie kurzą

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main() {
	vector<int> vec;
	copy(istream_iterator<int>(cin),
		 istream_iterator<int>(),
		 back_inserter(vec));
		 
	for(auto it = rbegin(vec); it != rend(vec); ++it){
		cout << (*it) << " ";
	}
	return 0;
}
3

@spartanPAGE skoro wczytanie przez copy, to czemu wypisywanie przez pętlę?

vector<int> numbers;
copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(numbers));
copy(crbegin(numbers), crend(numbers), ostream_iterator<int>(cout, " "));
0

Nie rozumiem twojego sposobu(nie ogarniam jeszcze tych funkcji), napisałem coś takiego, ale nie wiem jak ogarnąć tę "pewna nieokreślona, ale niewielka ilość liczb całkowitych". Wpadłem na pomysł żeby dać duży rozmiar tablicy i jeśli nadusi się enter pętla się przerywa i wypisuje w odwróconej kolejności te liczby, ale nie wiem czy spoj to przyjmie i jak to zrobić?

 #include <iostream>

using namespace std;

int main()
{
    string a[1000];
    for(int i=0; i<1000; i++)

        cin>>a[i];
for (int j=999; j>=0; j--)
        cout<<a[j]<<" ";

    return 0;
}
0

Dlaczego string? To są zwykłe liczby.
Dlaczego nie użyjesz std::vector?
Dlaczego nie sprawdzisz wyników, jakie daje twój program?

0
Garniturek napisał(a):

ale nie wiem jak ogarnąć tę "pewna nieokreślona, ale niewielka ilość liczb całkowitych"

Przecież dopiero co dostałeś odpowiedź: http://4programmers.net/Forum/1179321

2

@Garniturek żebyś nie musiał błądzić w ciemności, przygotowałem dla Ciebie drobne środowisko uruchomieniowe dla twoich programów;

Tutaj przykład z moim:

#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cstdlib>
#include <string>
#include <initializer_list>
using namespace std;

namespace program{
	void main(istream &cin, ostream &cout){
        vector<int> vec;
        copy(istream_iterator<int>(cin),
             istream_iterator<int>(),
             back_inserter(vec));
 
        for(auto it = rbegin(vec); it != rend(vec); ++it){
            cout << (*it) << " ";
        }
    }
}

template<typename T>
void test_program_with(const T &sequence){
    using input_el_t = typename T::value_type;
    stringstream input, output;
	
	auto convert_to_input = [](auto &cont){
		ostringstream result;
		copy(begin(cont), end(cont), ostream_iterator<input_el_t>(result, " "));
        return result.str();
	};
    
    auto run_program = [&]{
        program::main(input, output);
    };
    
    auto pass_test = [](){
        cout << "Passed." << endl;
    };
    
    auto fail_test_wrong_value = [](auto x, auto y){
        cout << "Failed with x != y, where x=" << x << ", y=" << y << endl;
        exit(-1);
    };
    
    auto fail_test_different_sizes = [](auto expected, auto result){
        cout << "Failed with different sizes; expected size=" << expected << ", result size=" << result << endl;
        exit(-2);
    };
    
    auto dump_container = [](const auto &container){
        cout << "{ "; for(auto x : container) cout << x << " "; cout << "}\n";
    };
	
    input << convert_to_input(sequence);
    run_program();

    vector<string> expected;
    transform(begin(sequence), end(sequence), back_inserter(expected), [](auto val){ return to_string(val); });
    reverse(begin(expected), end(expected));
    
    vector<string> result{istream_iterator<string>(output), istream_iterator<string>()};
    
    dump_container(expected);
    dump_container(result);

    if(expected.size() != result.size())
        fail_test_different_sizes(result.size(), result.size());

    for(size_t i = 0; i < result.size(); ++i){
        if(expected[i] != result[i])
            fail_test_wrong_value(expected[i], result[i]);
    }
    pass_test();
}


int main() {
    using seq = vector<int>;
    
	test_program_with(seq{1, 2, 3});
    test_program_with(seq{-13, -23, 33});
    test_program_with(seq{});
}

stdout

{ 3 2 1 }
{ 3 2 1 }
Passed.
{ 33 -23 -13 }
{ 33 -23 -13 }
Passed.
{ }
{ }
Passed.

Link: http://melpon.org/wandbox/permlink/5x9nyThT7GOh6ilR

Jedyne co musisz zrobić, to wrzucić flaki swojego programu do program::main
Powodzenia.

//EDIT:
Z twoim pierwszym kodem (http://melpon.org/wandbox/permlink/UalBRKmf6s9mv5V9):
stdout

{ 3 2 1 }
{ 3 2 1 }
Passed.
{ 33 -23 -13 }
{ 33 32- 31- }
Failed with x != y, where x=-23, y=32-

Natomiast twój drugi przechodzi ;) Zarówno mój test, jak i na spoju. Jednakże jest to bardzo brzydkie rozwiązanie.

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