Explorar o código

section3-6 and section3-8 has completed

tangs %!s(int64=7) %!d(string=hai) anos
pai
achega
5468bb1933
Modificáronse 3 ficheiros con 135 adicións e 17 borrados
  1. 48 17
      section3-5/section3-5.cpp
  2. 55 0
      section3-6/section3-6.cpp
  3. 32 0
      section3-8/section3-8.cpp

+ 48 - 17
section3-5/section3-5.cpp

@@ -5,6 +5,7 @@
 
 #include<iostream>
 #include<vector>
+#include<string>
 #include<algorithm>
 using namespace std;
 
@@ -13,31 +14,61 @@ struct Wooden
 {
 	int length;
 	int weight;
+	string status;
 };
 
+const string CURRENT = "current";
+const string DELETED = "delete";
+
 bool compare(Wooden a, Wooden b) {
 	return (a.length < b.length) && (a.weight < b.weight);
 }
 
 int calc(vector<Wooden> woodens) {
-	int cost = 0;
 	int size = woodens.size();
-	int index = 0;
-	for (; size != 0; ) {
-		if (size == 1) {
-			cost++;
-			break;
-		}
-		sort(woodens.begin(), woodens.end(), compare);
-		Wooden node = woodens[0];
+	if (size == 0) {
+		return 0;
+	}
+	if (size == 1) {
+		return 1;
+	}
 
-		for (int i = 1; i < size; i++) {
-			if (woodens.at(i).length < node.length || woodens.at(i).weight < node.weight) {
-				woodens.erase(woodens.begin());
-				break;
+	int cost = 0;
+	int index = -1;
+	Wooden node;
+	for (int i = 0; i < size; i++) {
+		if (node.status != CURRENT)
+		{
+			if (DELETED == woodens.at(i).status) {
+				continue;
 			}
-			woodens.erase(woodens.begin() + i);
-			size = woodens.size();
+			else {
+				woodens.at(i).status = CURRENT;
+				cost++;
+				node = woodens[i];
+				index = i;
+			}
+		}
+
+		if (CURRENT == woodens.at(i).status) {
+			continue;
+		}
+		if (woodens.at(i).length >= node.length && woodens.at(i).weight >= node.weight) {
+			woodens.at(i).status = DELETED;
+			continue;
+		}
+		if (woodens.at(i).length <= node.length &&woodens.at(i).weight <= node.weight) {
+			woodens.at(index).status = DELETED;
+			woodens[i].status = CURRENT;
+			node = woodens[i];
+			index = i;
+			i = -1;
+			continue;
+		}
+		if (i == (size - 1)) {
+			woodens.at(index).status = DELETED;
+			index = -1;
+			node = Wooden{};
 		}
 	}
 	return cost;
@@ -52,12 +83,12 @@ int main()
 			cin >> n;
 			for (int j = 0; j < n; j++) {
 				cin >> l >> w;
-				woodens.push_back(Wooden{ l,w });
+				woodens.push_back(Wooden{ l, w, "" });
 			}
 			cout << calc(woodens) << endl;
 			woodens.clear();
 		}
 	}
-    return 0;
+	return 0;
 }
 

+ 55 - 0
section3-6/section3-6.cpp

@@ -0,0 +1,55 @@
+// Question expression is ambiguous
+// reference: http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=20392&messageid=1&deep=0
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+using namespace std;
+
+struct Student {
+	int id;
+	int score;
+};
+
+bool compare(Student a, Student b) {
+	return a.score > b.score;
+}
+
+int getRank(int targetScore, vector<Student> students) {
+	sort(students.begin(), students.end(), compare);
+	int size = students.size();
+	int rankNum = 1;
+	for (int i = 0; i < size; i++) {
+		if (students.at(i).score > targetScore){
+			rankNum++;
+		}
+		else {
+			return rankNum;
+		}
+	}
+	return 0;
+}
+
+int main()
+{
+	int targetId = 0, targetScore = 0;
+	vector<Student> students;
+	int id = 0, score = 0;
+	while (cin >> targetId) {
+		while (cin >> id >> score)
+		{
+			if (targetId == id)
+			{
+				targetScore = score;
+			}
+			if (id == 0 && score == 0) {
+				cout << getRank(targetScore, students) << endl;
+				students.clear();
+				break;
+			}
+			students.push_back(Student{ id, score });
+		}
+	}
+    return 0;
+}
+

+ 32 - 0
section3-8/section3-8.cpp

@@ -0,0 +1,32 @@
+#include<algorithm>
+#include<iostream>
+
+
+using namespace std;
+int main()
+{
+	int N;
+	int *digs;
+	int n;
+	while (cin >> N && N) {
+		for (int times = 0; times < N; times++) {
+			cin >> n;
+			digs = new int[n];
+			for (int i = 0; i < n; i++) {
+				cin >> digs[i];
+			}
+			sort(digs, digs + n);
+			for (int i = 0; i < n; i++) {
+				cout << digs[i];
+				if (i != (n-1))
+				{
+					cout << " ";
+				}
+			}
+			cout << endl;
+			free(digs);
+		}
+	}
+    return 0;
+}
+