Browse Source

ac section3-3 and section3-4

tangs 7 năm trước cách đây
mục cha
commit
117f0747bc
3 tập tin đã thay đổi với 70 bổ sung4 xóa
  1. 16 4
      section3-3/section3-3.cpp
  2. 7 0
      section3-4/CMakeLists.txt
  3. 47 0
      section3-4/main.cpp

+ 16 - 4
section3-3/section3-3.cpp

@@ -1,5 +1,7 @@
 #include <iostream>
 #include <vector>
+#include <string>
+#include <algorithm>
 
 using namespace std;
 
@@ -8,6 +10,15 @@ struct Grade {
     int Score;
 };
 
+bool compare(Grade a, Grade b) {
+    if (a.Score > b.Score) {
+        return true;
+    } else if (a.Score < b.Score) {
+        return false;
+    }
+    return a.Id < b.Id;
+}
+
 int main() {
     int N = 0, M = 0, G = 0;
     int *questions;
@@ -31,10 +42,9 @@ int main() {
             total = 0, scoreIndex = 0;
             cin >> id;
             cin >> answer;
-            answer = answer > M ? M : answer;
             for (int j = 0; j < answer; j++) {
                 cin >> scoreIndex;
-                total += questions[scoreIndex];
+                total += questions[scoreIndex - 1];
             }
             if (total < G) {
                 continue;
@@ -43,11 +53,13 @@ int main() {
             grade.push_back(tempGrade);
         }
 
+        sort(grade.begin(), grade.end(), compare);
+
+        cout << grade.size() << endl;
         for (int i = 0; i < (grade.size()); i++) {
-            cout << "id: " << grade.at(i).Id << "; scoreIndex: " << grade.at(i).Score << endl;
+            cout << grade.at(i).Id << " " << grade.at(i).Score << endl;
         }
 
-        // free
         free(questions);
         grade.clear();
     }

+ 7 - 0
section3-4/CMakeLists.txt

@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 3.6)
+project(section3_4)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+
+set(SOURCE_FILES main.cpp)
+add_executable(section3_4 ${SOURCE_FILES})

+ 47 - 0
section3-4/main.cpp

@@ -0,0 +1,47 @@
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+
+using namespace std;
+
+const string space = "5";
+
+bool compare(int a, int b) {
+    return a < b;
+}
+
+int main() {
+    string str;
+    vector<int> result;
+    int val = 0;
+    while (cin >> str) {
+        int index = 0;
+        for (; str.size() > 0;) {
+            index = str.find(space);
+            if (index < 0) {
+                val = atoi(str.substr(0, index).c_str());
+                result.push_back(val);
+                break;
+            }
+            if (index != 0) {
+                val = atoi(str.substr(0, index).c_str());
+                result.push_back(val);
+            }
+            str = str.substr(index + 1, str.size());
+        }
+        sort(result.begin(), result.end(), compare);
+
+        for (int i = 0; i < result.size(); i++) {
+            cout << result.at(i);
+            if (i != (result.size() - 1)) {
+                cout << " ";
+            }
+        }
+        cout << endl;
+
+        result.clear();
+    }
+
+    return 0;
+}