ผลต่างระหว่างรุ่นของ "Psl67/recursion templates"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(สร้างหน้าด้วย "== findmax == <syntaxhighlight lang="cpp"> #include <iostream> #include <vector> using namespace std; int n; vector<int> x; void read_input() { cin...")
 
แถว 30: แถว 30:
 
}
 
}
  
 +
</syntaxhighlight>
 +
 +
== selection sort ==
 +
<syntaxhighlight lang="cpp">
 +
#include <iostream>
 +
#include <vector>
 +
 +
using namespace std;
 +
 +
int n;
 +
vector<int> x;
 +
 +
void read_input()
 +
{
 +
  cin >> n;
 +
  for(int i = 0; i < n; i++) {
 +
    int xx;
 +
    cin >> xx;
 +
    x.push_back(xx);
 +
  }
 +
}
 +
 +
int find_max_index(vector<int>& x, int n)
 +
{
 +
  // TODO: return the index of the maximum element in x[0], x[1],... x[n-1]
 +
  //      don't use any loop control structures
 +
}
 +
 +
void swap(int& x, int& y)
 +
{
 +
  int t = x;
 +
  x = y;
 +
  y = t;
 +
}
 +
 +
void selection_sort(vector<int>& x, int n)
 +
{
 +
  // TODO: don't use any loop control structures
 +
}
 +
 +
int main()
 +
{
 +
  read_input();
 +
  selection_sort(x, n);
 +
  for(int i=0; i<n; i++) {
 +
    cout << x[i] << endl;
 +
  }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขเมื่อ 02:20, 10 กุมภาพันธ์ 2568

findmax

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> x;

void read_input()
{
  cin >> n;
  for(int i = 0; i < n; i++) {
    int xx;
    cin >> xx;
    x.push_back(xx);
  }
}

int find_max(vector<int>& x, int n)
{
  // TODO: write this function, do not use any loop control
}

int main()
{
  read_input();
  cout << find_max(x,n) << endl;
}

selection sort

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> x;

void read_input()
{
  cin >> n;
  for(int i = 0; i < n; i++) {
    int xx;
    cin >> xx;
    x.push_back(xx);
  }
}

int find_max_index(vector<int>& x, int n)
{
  // TODO: return the index of the maximum element in x[0], x[1],... x[n-1]
  //       don't use any loop control structures
}

void swap(int& x, int& y)
{
  int t = x;
  x = y;
  y = t;
}

void selection_sort(vector<int>& x, int n)
{
  // TODO: don't use any loop control structures
}

int main()
{
  read_input();
  selection_sort(x, n);
  for(int i=0; i<n; i++) {
    cout << x[i] << endl;
  }
}