Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

31232 855242 郭凱明 剛畢業典禮完沒想到還要繼續寫程式 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions 855242/bubble-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include<vector>
using namespace std;

int main() {
vector<int> vector = {};
int input;
bool isSwapped = true;

cout << "請輸入所有數字(若不是數字或空格即結束):";
while (cin >> input) {
vector.push_back(input);
}
int vLength = vector.size();

if (vLength == 0) {
cout << "沒有任何數字排個X!";
return 0;
}

do {
isSwapped = false;
if (vLength < 2) break;
for (int i = 0; i < vLength - 1; i++) {
if (vector[i] > vector[i + 1]) {
int temp;
temp = vector[i];
vector[i] = vector[i + 1];
vector[i + 1] = temp;
isSwapped = true;
}
}
vLength --;
} while (isSwapped);

cout << "由小到大為:";
for (int i : vector) {
cout << i << " ";
}
}
20 changes: 20 additions & 0 deletions 855242/d518.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <map>
using namespace std;

int main() {
int times = 0;
string word = "";
while (cin >> times) {
map<string,int> maps;
for (int i = 0; i < times; i++) {
cin >> word;
if (maps[word] == 0) {
maps[word] = maps.size();
cout << "New! " << maps[word] << endl;
} else {
cout << "Old! " << maps[word] << endl;
}
}
}
}
37 changes: 37 additions & 0 deletions 855242/selection-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include<vector>
using namespace std;

int main() {
vector<int> vector = {};
int input;

cout << "請輸入所有數字(若不是數字或空格即結束):";
while (cin >> input) {
vector.push_back(input);
}
int vLength = vector.size();
if (vLength == 0) {
cout << "沒有任何數字排個X!";
return 0;
}

for (int i = 0; i < vLength; i++) {
int min = vector[i];
int minOrdinal = i;
for (int j = i; j < vLength; j++) {
if (vector[j] < min) {
min = vector[j];
minOrdinal = j;
}
}
int temp = vector[minOrdinal];
vector[minOrdinal] = vector[i];
vector[i] = temp;
}

cout << "由小到大為:";
for (int i : vector) {
cout << i << " ";
}
}