ผลต่างระหว่างรุ่นของ "Algo lab/sorting tasks notes"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 1: | แถว 1: | ||
− | == | + | == Templates == |
+ | |||
+ | === flipsort === | ||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
แถว 18: | แถว 20: | ||
// do bubble sort | // do bubble sort | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === dnasorting === | ||
+ | |||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | #include <algorithm> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | const int MAX_M = 110; | ||
+ | |||
+ | string st[MAX_M]; | ||
+ | pair<int,int> x[MAX_M]; | ||
+ | int n,m; | ||
+ | |||
+ | int unsortedness(string s) | ||
+ | { | ||
+ | // TODO | ||
+ | // read how to work on strings here: https://cplusplus.com/reference/string/string/ | ||
+ | // ex. try s.length() and s.at(xxx) | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void solve() | ||
+ | { | ||
+ | cin >> n >> m; | ||
+ | for(int i=0; i<m; i++) { | ||
+ | cin >> st[i]; | ||
+ | |||
+ | x[i].first = unsortedness(st[i]); | ||
+ | x[i].second = i; | ||
+ | } | ||
+ | sort(x, x+m); | ||
+ | |||
+ | for(int i=0; i<m; i++) { | ||
+ | cout << st[x[i].second] << endl; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int k; | ||
+ | |||
+ | cin >> k; | ||
+ | for(int j=0; j<k; j++) { | ||
+ | solve(); | ||
} | } | ||
} | } |
รุ่นแก้ไขปัจจุบันเมื่อ 06:44, 28 สิงหาคม 2566
Templates
flipsort
#include <iostream>
using namespace std;
int x[1010];
main()
{
int n;
while(cin >> n) {
for(int i=0; i<n; i++) {
cin >> x[i];
}
// do bubble sort
}
}
dnasorting
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX_M = 110;
string st[MAX_M];
pair<int,int> x[MAX_M];
int n,m;
int unsortedness(string s)
{
// TODO
// read how to work on strings here: https://cplusplus.com/reference/string/string/
// ex. try s.length() and s.at(xxx)
return 0;
}
void solve()
{
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> st[i];
x[i].first = unsortedness(st[i]);
x[i].second = i;
}
sort(x, x+m);
for(int i=0; i<m; i++) {
cout << st[x[i].second] << endl;
}
}
int main()
{
int k;
cin >> k;
for(int j=0; j<k; j++) {
solve();
}
}
Sample codes
Sorting / pairs
#include <iostream>
#include <algorithm>
using namespace std;
pair<int,int> x[1000];
int main()
{
int n;
cin >> n;
for(int i=0; i<n; i++) {
cin >> x[i].first >> x[i].second;
}
sort(x, x+n);
for(int i=0; i<n; i++) {
cout << x[i].first << " " << x[i].second << endl;
}
}