在C++中查找唯一对,使得每个元素都小于或等于N
在本教程中,我们将学习如何查找小于给定数字n的唯一对。
让我们看看解决问题的步骤。
初始化数字。
从i = 1迭代到i < n。
从j = i + 1迭代到j < n。
打印(i, j)。
示例
让我们看看代码。
#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; }
输出
如果执行上述程序,则会得到以下结果。
(1,2) (1,3) (1,4) (2,3) (2,4) (3,4)
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
结论
如果您在本教程中有任何疑问,请在评论区提出。
广告