Mám nasledujúci kód v c++ chcem definovať a volať funkciu, ktorá sa začína a končí číslo od užívateľa a na zobrazovanie objednať páry

0

Otázka

Chcem, aby sa funkcia tlače objednať páry medzi počiatočný a koncový počet páči. Vstupné: Zadajte prvé číslo 1 ukončenie číslo 5 Výstup: (11)(12)(13)(14)(15) (22)(23)(24)(25) (33)(34)(35) (44)(45) (55) ale môj kód dáva rôzne výstup i fix it

#include <bits/stdc++.h>
using namespace std;
void uniquePairs(int n) {
   for (int i = 1; i < n; ++i) {
      for (int j = i + 1; j < n; j++) {
         cout << "(" << i << "," << j << ")" << endl;
      }
   }
}
int main() {
   int n = 5;
   uniquePairs(n);
   return 0;
}
c++ integer unordered-set
2021-11-23 06:00:40
1

Najlepšiu odpoveď

1

Môžete zmeniť svoju funkciu, aby sa take 2 argument namiesto 1, ako je uvedené nižšie. Prvý argument zodpovedá štartovné číslo a druhá odpovedá na koniec čísla.

#include <iostream>
using namespace std;
//function uniquePairs takes 2 arguments now instead of just 1
void uniquePairs(int startingNumber, int endingNumber) {
    int k = 0;
   for (int i = startingNumber; i <= endingNumber; ++i) {
      for (int j = startingNumber + k; j<= endingNumber; j++) {
         cout << "(" << i << "," << j << ")" << endl;
      }
      ++k;
   }
}
int main() {
   int startingNumber, endingNumber;
   std::cout<<"Enter startingNumber: "<<std::endl;
   std::cin >> startingNumber;
   std::cout<<"Enter endingNumber: "<<std::endl;
   std::cin >> endingNumber;
   //call the function while passing the 2 input numbers
   uniquePairs(startingNumber,endingNumber);
   return 0;
}

Výstup z vyššie uvedených program pre vstupy startingNumber =1 a endingNumber = 5 je:

Enter startingNumber: 
1
Enter endingNumber: 
5
(1,1)
(1,2)
(1,3)
(1,4)
(1,5)
(2,2)
(2,3)
(2,4)
(2,5)
(3,3)
(3,4)
(3,5)
(4,4)
(4,5)
(5,5)

Výstup programu uvedených vyššie môžete vidieť tu. Tiež, pozrite sa na dôvod, Prečo by som nemal #include <bitov/stdc++.h>?.

2021-11-23 08:53:50

@user4581301 Ach, už som ho zmenili na #include<iostream> v mojej pôvodnej odpoveď. Ale potom som urobil niektoré upraviť a znova vložiť kód z vonkajšej stránky, ktoré mal tento #include<bits/stdc++.h>. Ak ste si upraviť históriu moja odpoveď uvidíte, že som mal #include<iostream>. Pridal som odkaz na "prečo nie, ak chcete použiť tento bits/stdc++.h"pre OP teraz.
Anoop Rana

V iných jazykoch

Táto stránka je v iných jazykoch

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................