Selaa lähdekoodia

字符串练习

jal 5 vuotta sitten
vanhempi
commit
9d2708c6c3
5 muutettua tiedostoa jossa 147 lisäystä ja 0 poistoa
  1. 21 0
      A.cpp
  2. 28 0
      C.cpp
  3. 31 0
      D.cpp
  4. 28 0
      F.cpp
  5. 39 0
      G.cpp

+ 21 - 0
A.cpp

@@ -0,0 +1,21 @@
+//
+// Created by jal on 2019-05-02.
+//
+
+#include<bits/stdc++.h>
+using namespace std;
+int main(){
+    int n;
+    while(cin >> n){
+        vector<int>v(n);
+        for(auto&i:v){
+            cin >> i;
+        }
+        sort(v.begin(), v.end());
+        if(v.size()%2==0){
+            cout << fixed << setprecision(2) << (v[n/2-1]+v[n/2])/2.0 << endl;
+        }else{
+            cout << fixed << setprecision(2) << v[n/2]/1.0 << endl;
+        }
+    }
+}

+ 28 - 0
C.cpp

@@ -0,0 +1,28 @@
+//
+// Created by jal on 2019-05-02.
+//
+
+#include <bits/stdc++.h>
+using namespace std;
+int main(){
+    int n;
+    while (cin >> n){
+        priority_queue<int>Q;
+        for (int i = 0; i < n; ++i) {
+            int x;
+            cin >> x;
+            Q.push(x);
+        }
+        int cnt = 0;
+        for(int i = 1; i < n; i++){
+            int a = Q.top();
+            Q.pop();
+            int b = Q.top();
+            Q.pop();
+            int c = min(a, b);
+            cnt += c;
+            Q.push(c);
+        }
+        cout << cnt << endl;
+    }
+}

+ 31 - 0
D.cpp

@@ -0,0 +1,31 @@
+//
+// Created by jal on 2019-05-02.
+//
+
+#include<bits/stdc++.h>
+using namespace std;
+int main(){
+    string s;
+    while(getline(cin, s)){
+        int b = 0, e = 0;
+        for(int i = 0; i < s.size(); i++){
+            if(i == 0){
+                if(isalpha(s[i])){
+                    b = i;
+                }
+            }
+            else if (not isalpha(s[i-1]) && isalpha(s[i])){
+                b = i;
+            }
+            if(isalpha(s[i])){
+                e = i+1;
+            }else{
+                if(b != e){
+                    reverse(s.begin()+b, s.begin()+e);
+                }
+                b = e = i;
+            }
+        }
+        cout << s << endl;
+    }
+}

+ 28 - 0
F.cpp

@@ -0,0 +1,28 @@
+//
+// Created by jal on 2019-05-02.
+//
+
+#include<bits/stdc++.h>
+using namespace std;
+int main(){
+    int n;
+    while(cin >> n){
+        priority_queue<int, vector<int>, greater<int>>Q;
+        for(int i = 0; i < n; i++){
+            int x;
+            cin >> x;
+            Q.push(x);
+        }
+        int cnt = 0;
+        for(int i = 1; i < n; i++){
+            int a = Q.top();
+            Q.pop();
+            int b = Q.top();
+            Q.pop();
+            int c = a + b;
+            cnt += c;
+            Q.push(c);
+        }
+        cout << cnt << endl;
+    }
+}

+ 39 - 0
G.cpp

@@ -0,0 +1,39 @@
+//
+// Created by jal on 2019-05-02.
+//
+
+#include<bits/stdc++.h>
+using namespace std;
+int main(){
+    string s;
+    getline(cin, s);
+    map<string, int>mp;
+    int b = 0, e = 0;
+    for(int i = 0; i < s.size(); i++){
+        s[i] = tolower(s[i]);
+        if(i == 0){
+            if(isalpha(s[i])){
+                b = i;
+            }
+        }else if(not isalpha(s[i-1]) && isalpha(s[i])){
+            b = i;
+        }
+        if(isalpha(s[i])){
+            e = i+1;
+        }else{
+            if(b != e){
+                string str = s.substr(b, e - b);
+                mp[str]++;
+            }
+            b = e = i;
+        }
+    }
+    int n;
+    cin >> n;
+    while(n--){
+        string s1;
+        cin >> s1;
+        cout << mp[s1] << endl;
+    }
+
+}