Sortowanie słow w pary

0

Witam, próbuję zrobić jedno zadanie, ale sprawdzarka daje 2/6, próbowałem juz wszystkiego i nie wiem o co chodzi.
Zadanie: https://i.imgur.com/w5zyq0K.png

Próba pierwsza

#include <iostream>
using namespace std;
#include <string>

int main()
{
  int n;
  cin >> n;

  string *input = new string[n * 2];
  string temp;
  string tab;
  for (int i = 0; i < n * 2; i++)
  {
    cin >> tab;
    if(tab.length() <= 50) {
      input[i] = tab;
    }

  }

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n - i; ++j)
    {
      if (input[j] > input[j + 1])
      {
        temp = input[j];
        input[j] = input[j + 1];
        input[j + 1] = temp;
      }
    }
  }
  int count = 0;
  char min = '-';

  for (int i = 0; i < n * 2; i++)
  {
    cout << input[i] << endl;
    count++;
    if (count % 2 == 0 && count != 0)
    {
      for (int i = 0; i < 20; i++)
      {
        cout << min;
      }
      cout << endl;
    }
  }

  delete[] input;

  return 0;
}

Próba druga też 2/6

#include <string.h>
#include <stdio.h>

int main()
{
  int numPairs;
  scanf("%i", &numPairs);

  for (int i = 0; i < numPairs; ++i) {
    char first[50] = {0};
    char second[50] = {0};
    scanf("%s", first);
    scanf("%s", second);

    if (strcmp(first, second) < 0) {
      printf("%s\n%s\n--------------------\n", first, second);
    } else {
      printf("%s\n%s\n--------------------\n", second, first);
    }
  }
}
0

Sprawdź wynik dla testu:
1
Foo
bar

'b' jest wcześniej w alfabecie od 'f', więc bar powinno być pierwsze prawda?

0

strcmp rozróżnia wielkość liter, a w zadaniu jest, żeby nie rozróżniać.

Możesz spróbować z strcasecmp, ale nie wiem, czy to standardowa funkcja. W internetach każą przelatywać przez każdy znak i robić tolower.

2

Jednym stopniem bycia żałosnym jest zmuszać ludzi do używania jawnie dynamicznej alokacji, ale jeszcze innym jest zmuszać ich do tego tam, gdzie w ogóle żadna tablica nie jest potrzebna.

Edit - z tego co rozumiem, to ty sortujesz całą tablicę, a powinieneś posortować każdą parę oddzielnie, czyli jakby na wejściu by było

ciasto
banan
ananas
masło

to na wyjściu powinno być

banan
ciasto
--------------------
ananas
masło

edit2: also, miejże litość, sortowanie bąbelkowe powinno zginąć niedługo po powstaniu

0

Główny problem, to fakt, że wszystko masz w main.
Podziel kod na mniejsze funkcje. Będzie łatwiej.

https://dsp.krzaq.cc/post/176/ucze-sie-cxx-kiedy-uzywac-new-i-delete/

1

W sumie zadanie powinno być rozwiązane następująco:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;

string to_lower(string s)
{
	transform(begin(s),end(s),begin(s),::tolower);
	return s;
}

int main()
{
	string sep(20,'-');
	int count;
	for(cin>>count;count--;cout<<sep<<endl)
	{
		string a,b;
		cin>>a>>b;
		if(to_lower(a)<=to_lower(b)) cout<<a<<endl<<b<<endl;
		else cout<<b<<endl<<a<<endl;
	}
    return 0;
}

Ale w związku z brakiem wyobraźni prowadzącego powstało antydydaktyczne zadanie którego rozwiązanie jest jaskrawym przykładem jak nie należy programować.
Czyli następujące brzydactwo:

#include <iostream>
#include <cctype>
using namespace std;

bool inorder(char *a,char *b)
{
	for(;(*a)&&(*b)&&(tolower(*a)==tolower(*b));++a,++b) {}
	return tolower(*b)>tolower(*a);
}

int main()
{
	char *a=new char[51],*b=new char[51];
	int count;
	for(cin>>count;count--;cout<<endl)
	{
		cin>>a>>b;
		if(inorder(a,b)) cout<<a<<endl<<b<<endl;
		else cout<<b<<endl<<a<<endl;
		for(int i=0;i<20;++i) cout<<'-';
	}
	delete[] a;
	delete[] b;
    return 0;
}
2
#include <iostream>
#include <set>
#include <string>

void setupIO() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

//----
struct StrPairs {
    std::string a, b;
};

std::weak_ordering icompare(const std::string& a, const std::string& b)
{
    return std::lexicographical_compare_three_way(
        a.begin(), a.end(), 
        b.begin(), b.end(),
        [](auto a, auto b) { return std::toupper(a) <=> std::toupper(b); }
    );
}

std::weak_ordering operator<=>(const StrPairs& a, const StrPairs& b)
{
    auto cmpA = icompare(a.a, b.a);
    return cmpA != std::weak_ordering::equivalent ? cmpA : icompare(a.b, b.b);
}

//-------
auto readFrom(std::istream& in)
{
    std::set<StrPairs> data;
    size_t n;
    if (std::cin >> n) {
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        StrPairs p;
        while(n-- && std::getline(std::cin, p.a) && std::getline(std::cin, p.b)) {
            data.insert(p);
        }
    }
    return data;
}

std::ostream& printTo(std::ostream& out, const std::set<StrPairs>& data)
{
    for (const auto& a : data) {
        out << a.a << '\n';
        out << a.b << '\n';
        out << "--------\n";
    }
    return out;
}

//----
int main()
{
    setupIO();

    auto data = readFrom(std::cin);
    printTo(std::cout, data);

    return 0;
}

https://godbolt.org/z/jahnYf

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