ncstoj 1354成績排名 1598單詞排序 1599相對分子質量

ncstoj 1354成績排名


Description
讀入n名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號。
Input

每個測試輸入包含1個測試用例,第一行n代表學生的個數(0 < n < 100)
Output

對每個測試用例輸出2行,第1行是成績最高學生的姓名和學號,第2行是成績最低學生的姓名和學號,字符串間有1空格。
Sample Input

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

Sample Output

Mike CS991301
Joe Math990112

結構體入門題
C++寫法1:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 105;
typedef struct STU {
    string name;
    string num;
    int score;
    bool operator<(const STU &S) const { return score < S.score; }
} STU;
STU s[maxn];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> s[i].name >> s[i].num >> s[i].score;
    sort(s, s + n);
    cout << s[n - 1].name << " " << s[n - 1].num << endl;
    cout << s[0].name << " " << s[0].num << endl;
    return 0;
}

C++寫法2與C語言對比:
在這裏插入圖片描述

ncstoj 1598單詞排序


Description

輸入一長串字符串,將其中的單詞按字典序輸出。
Input

輸入由K個英文單詞組成的字符串,1≤K≤20。
保證每個字母均由小寫字母構成,且每個單詞至多出現一次。
Output

將全部單詞按字典序輸出,每兩個單詞之間由空格隔開
Sample Input

tom likes apples

Sample Output

apples likes tom

STL set:使用集合排序並去重
編譯出問題的同學,請看這個視頻3分50秒: 編程環境配置與OJ介紹

#include <bits/stdc++.h>
using namespace std;
set<string> s;
int main() {
    int n;
    string str;
    while (cin >> str) {
        s.insert(str);
    }
    for (auto elem : s)
        cout << elem << " ";
    return 0;
}

學妹寫的C語言做法(她這個輸入過程寫複雜了)
在這裏插入圖片描述

寫法4: sort+vector

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<string> v;
    string str;
    while (cin >> str) {
        v.push_back(str);
    }

    sort(v.begin(), v.end());

    for (auto x : v) {
        cout << x << " ";
    }

    puts("");
    return 0;
}

ncstoj 1599相對分子質量


Description

小明在大學裏學習了編程以後,決定要解決一些高中時期最困惑他的問題之一:計算化學物質相對分子質量。
Input

輸入的第一行是一個正整數n,表示有n組測試數據。
接下來n行每行輸入一個字符串,表示某個分子式,分子式中只包含大寫字母和數字。
注意:輸入數據只包含3種元素,而這3種元素的相對原子質量如下:H(1),C(12),O(16)。

Output

對於每組輸入,輸出相對分子質量。
Sample Input

2
H2O
CH4

Sample Output

18
16

這道題算法很多,怎麼做都行
通過哈希表unordered_map(或map)構建查找元素表

#include <iostream>
#include <unordered_map>
using namespace std;
 
unordered_map<char, int> m1;
bool isDigit(char c) { return c <= '9' && c >= '1'; }
int main() {
    // freopen("/Users/zhbink/Documents/C++/C++/in.txt", "r", stdin);
    m1['H'] = 1;
    m1['C'] = 12;
    m1['O'] = 16;
 
    int T;
    cin >> T;
    while (T--) {
        string str;
        cin >> str;
        int ans = 0;
        char last_alp = 'a';
 
        for (int i = 0; i < str.length(); i++) {
            char c = str[i];
            if (isDigit(c))
                ans += m1[last_alp] * (c - '0');
            else
                ans += m1[last_alp];
            last_alp = c;
        }
        if (!isDigit(str.back()))
            ans += m1[last_alp];
        cout << ans << endl;
    }
    return 0;
}

學弟寫的算法二:

#include<iostream>
using namespace std;
int main(){
	int n,num,sum;
	string a;
	cin>>n;
	while(n--){
		sum=0;
		cin>>a;
		for(int i=0;i<a.length();i++){
			if(a[i]=='H'){
				num=1;
				sum+=1;
			}else if(a[i]=='O'){
				num=16;
				sum+=16;
			}else if(a[i]=='C'){
				num=12;
				sum+=12;
			}else if(a[i]<60){
				sum+=num*(a[i]-'0'-1);
			}
		}
		cout<<sum<<endl;
	}
	return 0;
} 

算法二的C語言版:
注意用getchar()喫換行符
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章