Informatics Olympiad One Pass 1.5: Array(3)

​The first part of C++ language

Chapter 5 Arrays

Section III Character Types and Character Arrays

1129 Count the number of numeric characters
#include <iostream>
using namespace std;

int main() {
    char ch;
    int cnt = 0;

    while (cin >> ch) {
        if (ch >= '0' && ch <= '9') cnt ++;
    }

    cout << cnt << endl;

    return 0;
}
1130 Find the first character that appears only once
#include <iostream>
using namespace std;

int main() {
    string s;
    int a[26] = {};

    cin >> s;

    for (int i = 0; i < s.size(); i ++ ) {
        a[s[i]-'a'] ++;
    }

    for (int i = 0; i < s.size(); i ++ ) {
        if (a[s[i]-'a'] == 1) {
            cout << s[i] << endl;
            return 0;
        }
    }

    cout << "no" << endl;

    return 0;
}
1131 Gene Correlation
#include <iostream>
using namespace std;

int main() {
    double x;
    string a, b;
    int cnt = 0;

    cin >> x >> a >> b;

    for (int i = 0; i < a.size(); i ++ ) {
        if (a[i] == b[i]) cnt ++;
    }

    if (1.0*cnt/a.size() >= x) cout << "yes" << endl;
    else cout << "no" << endl;

    return 0;
}
1132 Rock Paper Scissors
#include <iostream>
using namespace std;

int main() {
    int n;
    string p1,p2;

    cin >> n;

    while (n--) {
        cin >> p1 >> p2;
        if (p1 == p2) cout << "Tie" << endl;

        if (p1 == "Rock" && p2 == "Scissors") cout << "Player1" << endl;
        if (p1 == "Rock" && p2 == "Paper") cout << "Player2" << endl;

        if (p1 == "Scissors" && p2 == "Rock") cout << "Player2" << endl;
        if (p1 == "Scissors" && p2 == "Paper") cout << "Player1" << endl;

        if (p1 == "Paper" && p2 == "Scissors") cout << "Player2" << endl;
        if (p1 == "Paper" && p2 == "Rock") cout << "Player1" << endl;

    }

    return 0;
}
1133 Output string of relatives and friends
#include <iostream>
using namespace std;

int main() {
    string s;

    getline(cin, s);

    for (int i = 0; i < s.size(); i ++ ) {
        cout << char(s[i] + s[(i+1)%s.size()]);
    }

    return 0;
}
1134 Legal C identifier
#include <iostream>
using namespace std;

int main() {
    string s;

    cin >> s;

    if (s[0] >= '0' && s[0] <= '9') {
        cout << "no" << endl;
        return 0;
    }

    for (int i = 0; i < s.size(); i ++ ) {
        bool judge = (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '_';

        if (!judge) {
            cout << "no" << endl;
            return 0;
        }
    }

    cout << "yes" << endl;

    return 0;
}
1135 paired base chain
#include <iostream>
using namespace std;

int main() {
    string s;

    cin >> s;

    for (int i = 0; i <= s.size(); i ++ ) {
        if (s[i] == 'A') cout << 'T';
        if (s[i] == 'T') cout << 'A';
        if (s[i] == 'G') cout << 'C';
        if (s[i] == 'C') cout << 'G';
    }

    return 0;
}
1136 Password Translation
#include <iostream>
using namespace std;

int main() {
    string s;

    getline(cin, s);

    for (int i = 0; i < s.size(); i ++ ) {
        if (s[i] >= 'a' && s[i] <= 'z') {
            s[i] = (s[i]-'a'+1) % 26 + 'a';
        }
        else if (s[i] >= 'A' && s[i] <= 'Z') {
            s[i] = (s[i]-'A'+1) % 26 + 'A';
        }        
    }

    cout << s; 

    return 0;
}
1137 Encrypted Medical Record
#include <iostream>
using namespace std;

int main(){
    string s;
    char c;
    cin >>s;

    for (int i = s.size()-1; i >= 0; i -- ) {
        c = s[i];

        if (c < 'a'){
            cout << char((c-'A'+3) % 26 + 'A' + 32);
        }
        else {
            cout << char((c-'a'+3) % 26 + 'a' - 32);
        }
    } 

    return 0;
}
1138 Convert lowercase letters in a string to uppercase letters
#include <iostream>
using namespace std;

int main(){
    string s;
    getline(cin, s);

    for (int i =0; i < s.size(); i ++ ){
        if (s[i] >= 'a' && s[i] <= 'z') {
            cout << char(s[i] - 32);
        }
        else {
            cout << s[i];
        }
    }

    return 0;
}
1139 Sorting out the name of the medicine
#include <iostream>
using namespace std;

int main(){
    int n;
    string s;

    cin >> n;

    while (n--) {
        cin >> s;

        if (s[0] >= 'a' && s[0] <= 'z') s[0] = s[0] - 32;

        for (int i = 1; i < s.size(); i ++ ) {
            if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] + 32;
        }

        cout << s << endl;
    }

    return 0;
}
1140 Verification substring
#include <iostream>
using namespace std;

int main() {
    string s1, s2;

    cin >> s1 >> s2;

    if (s2.find(s1) != -1) {
        cout << s1 << " is substring of " << s2;
    }
    else if (s1.find(s2) != -1)    {
        cout << s2 << " is substring of " << s1;
    } 
    else {
        cout << "No substring";
    }

    return 0;
}
1141 Delete word suffix
#include <iostream>
using namespace std;

int main() {
    string s;

    cin >> s;
    int len = s.size();

    if (len <= 2) {
        cout << s << endl;
    }
    else if (s.substr(len-2) == "er" || s.substr(len-2) == "ly") {
        cout << s.substr(0, len-2) << endl;
    }
    else if (s.substr(len-3) == "ing") {
        cout << s.substr(0, len-3) << endl;
    }
    else {
        cout << s << endl;
    }

    return 0;
}
1142 word length
#include <iostream>
using namespace std;

int main() {
    string s;
    bool flag = true;

    while (cin >> s) {
        if (flag) {
            cout << s.size();
            flag = false;
        }
        else {
            cout << ',' << s.size();
        }
    }

    return 0;
}
1143 Longest and Shortest Words
#include <iostream>
using namespace std;

int main() {
    string s, a, b;
    int cnt = 0, max = 0, min = 101;

    getline(cin, s);

    for (int i = 0; i < s.size(); i ++ ) {
        int j = i;
        while (j < s.size() && s[j] != ' ' && s[j] != ',') {
            j ++;
        }

        if (j-i > max) {
            max = j - i;
            a = s.substr(i, j-i); 
        }
        if (j-i < min && j-i != 0) {
            min = j - i;
            b = s.substr(i, j-i);
        }

        i = j;
    }

    cout << a << endl;
    cout << b << endl;

    return 0;
}
1144 Word Reversal
#include <iostream>
using namespace std;

int main() {
    string s;

    getline(cin, s);

    for (int i = 0; i < s.size(); i ++ ) {
        int j = i;
        while (j < s.size() && s[j] != ' ') j ++ ;

        for (int k = j - 1; k >= i; k -- ) cout << s[k];
        cout << ' ';

        i = j;
    }

    return 0;
}
1145 string p-type encoding
#include <iostream>
using namespace std;

int main() {
    string s;

    cin >> s;

    for (int i = 0; i < s.size(); i ++ ) {
        int j = i + 1;

        while (j < s.size() && s[j] == s[j-1]) {
            j ++ ;
        }
        cout << j-i << s[i];

        i = j - 1;
    }

    return 0;
}
1146 Determine whether the string is a palindrome
#include <iostream>
using namespace std;

int main() {
    string s;

    cin >> s;

    for (int i = 0, j = s.size()-1; i < j; i ++, j -- ) {
        if (s[i] != s[j]) {
            cout << "no" << endl;
            return 0;
        }
    }

    cout << "yes" << endl;

    return 0;
}
1147 Name of student with highest score
#include <iostream>
using namespace std;

int main() {
    int n, score, max = 0;
    string name, ans;

    cin >> n;

    for (int i = 0; i < n; i ++ ) {
        cin >> score >> name;

        if (score > max) {
            max = score;
            ans = name;
        }
    }

    cout << ans << endl;

    return 0;
}
1148 consecutive characters
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int k;
    string s;

    scanf("%d\n", &k); 
    getline(cin, s);

    for (int i = 0; i < s.size(); i ++ ) {
        int j = i + 1;
        while (j < s.size() && s[j] == s[j-1]) j ++ ;

        if (j-i >= k) {
            cout << s[i] << endl;
            return 0;
        }

        i = j - 1;
    }

    cout << "No" << endl;

    return 0;
}
1149 Longest word 2
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    string s, ans;

    while (cin >> s) {
        if (s[s.size()-1]=='.') s = s.substr(0, s.size()-1);
        if (s.size() > ans.size()) ans = s;
    }

    cout << ans << endl;

    return 0;
}

If your child is in the fourth grade or above, is interested in computer programming and has spare capacity in cultural lessons, please contact customer service (WeChat ID: xiaolan7321) to participate in informatics learning. We are professional informatics competition coaches. We use online small class teaching methods. The goal is to help primary and middle school students who love programming to achieve excellent results in informatics competitions at home and abroad.

Teaching features:

  • Online small class teaching, lay a good code foundation. Avoid the problem that students in large classes are either "can't keep up" or "not enough to eat".

  • Rich teaching experience, familiar with students' knowledge structure and learning ability, and arrange the schedule reasonably.

  • Practice with competitions, and continuously improve students' abilities through examinations and competitions.

Guess you like

Origin blog.csdn.net/davidliule/article/details/106139630