Informatics Olympiad One Pass 1.6: Functions (1)

​The first part of C++ language

Chapter 6 Functions

The first section function

1150 Find the perfect number between a positive integer 2 and n
#include <iostream>
using namespace std;

int f(int x) {
    int sum = 0;

    for (int i = 1; i < x; i ++ ) {
        if (x % i == 0) sum += i;
    }

    return sum;
}

int main() {
    int n;
    cin >> n;

    for (int i = 2; i <= n; i ++ ) {
        if (i == f(i)) cout << i << endl;
    }

    return 0;
}
1151 Number of primes
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;

    for (int i = 2; i <= x/i; i ++ ) {
        if (x%i == 0) return false;
    }

    return true;
}

int main() {
    int n, cnt = 0;
    cin >> n;

    for (int i = 2; i <= n; i ++ ) {
        if (isPrime(i)) cnt ++ ;
    }

    cout << cnt << endl;

    return 0;
}
1152 Maximum number max(x,y,z)
#include <cstdio>
using namespace std;

double myMax(double a, double b, double c) {
    if (a>=b && a>=c) return a;
    if (b>=a && b>=c) return b;
    if (c>=a && c>=b) return c;
}

int main() {
    double a, b, c, m;

    scanf("%lf %lf %lf", &a, &b, &c);
    m = myMax(a,b,c) / (myMax(a+b,b,c) * (myMax(a,b,b+c)));

    printf("%.3lf\n", m);

    return 0;
}
1153 absolute prime
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;

    for (int i = 2; i <= x/i; i ++ ) {
        if (x%i == 0) return false;
    }

    return true;
}

int main() {
    int t;
    for (int i = 10; i < 100; i ++ ) {
        t = i%10 * 10 + i/10;
        if (isPrime(i) && isPrime(t)) cout << i << endl;
    }

    return 0;
}
1154 Affinity Number
#include <iostream>
using namespace std;

int f(int x) {
    int sum = 0;

    for (int i = 1; i < x; i ++ ) {
        if (x%i == 0) sum += i;
    }

    return sum;
}

int main() {
    int i, j;

    for (i = 2; ; i ++ ) {
        j = f(i);
        if (i == f(j) && i != j) {
            cout << i << ' ' << j << endl;
            return 0;
        }
    }

    return 0;
}
1155 Three-digit palindrome
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;

    for (int i = 2; i <= x/i; i ++ ) {
        if (x%i == 0) return false;
    }

    return true;
}

int main() {
    int t;
    for (int i = 1; i <= 9 ; i ++ ) {
        for (int j = 0; j <= 9; j ++ ) {
            t = i*101 + j*10;
            if (isPrime(t)) cout << t << endl;
        }
    }

    return 0;
}
1156 Find the value of π
#include <cstdio>
#include <cmath>
using namespace std;

double f(double x) {
    double sum = 0, a = x, b = x;
    int i = 1;

    while (a >= 1e-6) {
        if (i%4 == 3) sum -= a;
        else sum += a;

        i += 2 ;
        b = b * x * x;
        a = b / i;
    }

    return sum;
}

int main() {
    double pi;

    pi = 6 * f(1/sqrt(3));

    printf("%.10lf\n", pi);

    return 0;
}
1157 Goldbach Conjecture
#include <cstdio>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;

    for (int i = 2; i <= x/i; i ++ ) {
        if (x%i == 0) return false;
    }

    return true;
}

int main() {
    for (int i = 6; i <= 100 ; i += 2 ) {
        for (int j = 3; j <= i/2; j ++ ) {
            if (isPrime(j) && isPrime(i-j)) {
                printf("%d=%d+%d\n", i, j, i-j);
                break;
            }
        }
    }

    return 0;
}
1397 Simple arithmetic expression evaluation
#include <iostream>
using namespace std;

int f(int a, char ch, int b) {
    if (ch == '+') return a + b;
    if (ch == '-') return a - b;
    if (ch == '*') return a * b;
    if (ch == '/') return a / b;
    if (ch == '%') return a % b;
}

int main() {
    int a, b;
    char ch;

    cin >> a >> ch >> b;
    cout << f(a, ch, b);

    return 0;
}
1398 SMS billing
#include <cstdio>
#include <cmath>
using namespace std;

double f(int nums) {
    return ceil(1.0*nums/70) * 0.1;
}

int main() {
    int n, nums;
    double cost = 0;

    scanf("%d", &n);
    while (n--) {
        scanf("%d", &nums);
        cost += f(nums);
    }

    printf("%.1lf\n", cost);

    return 0;
}
1399 Preliminary screening of patients with influenza A
#include <iostream>
using namespace std;

bool f(float temp, bool flag) {
    if (temp >= 37.5 && flag) return true;
    else return false;
}

int main() {
    int n, cnt = 0;
    string name;
    float temp;
    bool flag;

    cin >> n;
    while (n--) {
        cin >> name >> temp >> flag;
        if (f(temp, flag)) {
            cout << name << endl;
            cnt ++ ;
        }
    }

    cout << cnt << endl;

    return 0;
}
1400 count the number of words
#include <iostream>
using namespace std;

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

int main() {
    string a,b;

    getline(cin, a);
    toLower(a);
    getline(cin, b);
    toLower(b);

    int pos = -1, cnt = 0;
    bool flag = true;
    for (int i = 0; i < b.size(); i ++ ) {
        int j = i;
        while (j < b.size() && b[j] != ' ') j ++ ;

        if (b.substr(i, j-i) == a) {
            if (flag) {
                pos = i;
                flag = false;
            }
            cnt ++ ;
        }

        i = j;
    }

    if (pos == -1) cout << -1 << endl;
    else cout << cnt << ' ' << pos << endl;

    return 0;
}
1401 Machine Translation
#include <iostream>
#include <cstring>
using namespace std;

const int M = 1e7 ;
int m, n, a[M], h[1001];
int pos = -1, num, cnt = 0;

void search(int x) {
    if (h[x] == 0) {
        cnt ++ ;
        h[x] = 1;

        pos = (pos+1)%m;
        if (a[pos] != -1) {
            h[a[pos]] = 0;
        }
        a[pos] = x;
    }
}

int main() {
    cin >> m >> n;

    memset(a, -1, M);

    for (int i= 0; i < n; i ++ ) {
        cin >> num;
        search(num);
    }

    cout << cnt << endl;

    return 0;
}
1402 Vigenère password
#include <iostream>
#include <cstdio>
using namespace std;

string key, str;

void translate(char x, char y) {
    if (x >= 'a' && x <= 'z') {
        printf("%c", ((x-'a') - (y-'A') + 26)%26 + 'a' );
    }
    else {
        printf("%c", ((x-'A') - (y-'A') + 26)%26 + 'A');
    }
}

int main() {
    cin >> key >> str;

    for (int i = 0; i < key.size(); i ++ ) {
        key[i] = (key[i] - 'A') % 32 + 'A';
    }

    for (int i = 0; i < str.size(); i ++ ) {        
        translate(str[i], key[i%key.size()]);
    }

    return 0;
}
1403 Prime Number Pair
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;

    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

int main() {
    int n;
    cin >> n;

    bool flag = true;
    for (int i = 5; i <= n; i ++ ) {
        if (isPrime(i-2) && isPrime(i)) {
            cout << i-2 << ' ' << i << endl;
            flag = false;
        }
    }

    if (flag) cout << "empty" << endl;

    return 0;
}
1404 My house number
#include <iostream>
using namespace std;


int main() {
    int n;
    cin >> n;

    for (int i = 1; ; i ++ ) {
        for (int j = 1; j <= i; j ++ ) {
            if (i*(i+1)/2 - 3*j == n) {
                cout << j << ' ' << i << endl;
                return 0;
            }
        }
    }

    return 0; 
}
1405 Sum and Product of Prime Numbers
#include <iostream>
#include <algorithm>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

int main() {
    int s, maxx = 0;
    cin >> s;

    for (int i = 3; i <= s/2; i ++ ) {
        if (isPrime(i) && isPrime(s-i)) {
            maxx = max(maxx, i*(s-i));
        }
    }

    cout << maxx << endl;

    return 0; 
}
1406 Word Substitution
#include <iostream>
#include <sstream>
using namespace std;


int main() {
    string s, a, b, word;

    getline(cin, s);
    cin >> a >> b;

    stringstream ssin(s);

    while (ssin >> word) {
        if (word == a) cout << b << ' ';
        else cout << word << ' ';
    }

    return 0; 
}
1407 Stupid Little Monkey
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

int main() {
    string word;
    int a[127] = {};

    cin >> word;

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

    int minn = 100, maxx = 0;
    for (int i = 'A'; i < 'z'; i ++ ) {
        maxx = max(maxx, a[i]);
        if (a[i]) minn = min(minn, a[i]);
    }

    int t = maxx - minn;
    if (isPrime(t)) cout << "Lucky Word" << endl << t << endl;
    else cout << "No Answer" << endl << 0 << endl;

    return 0; 
}
1408 Number of prime palindrome
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

bool isPalindrome(int x) {
    int x1 = x;

    int t = 0;
    while (x1) {
        t = t*10 + x1%10;
        x1/=10;
    }

    if (x == t) return true;
    else return false;
}

int main() {
    int n, cnt = 0;
    cin >> n;

    for (int i = 11; i <= n; i ++ ) {
        if (isPrime(i) && isPalindrome(i)) cnt ++ ;
    }
    cout << cnt << endl;

    return 0; 
}
1409 Determine the number of primes
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

int main() {
    int x, y, cnt = 0;
    cin >> x >> y;

    for (int i = min(x,y); i <= max(x,y); i ++ ) {
        if (isPrime(i)) cnt ++ ;
    }

    cout << cnt << endl;

    return 0; 
}
1410 Maximum prime factor sequence
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

int main() {
    int m, n; 
    cin >> m >> n;

    bool flag = true;
    for (int i = m; i <= n; i ++ ) {
        int t = i;
        for (int j = t; j >= 2; j -- ) {
            if (t%j == 0 && isPrime(j)) {
                if (flag) {
                    cout << j;
                    flag = false;
                }
                else {
                    cout << ',' << j;
                }                
                break;
            }
        }
    }    

    return 0; 
}
True primes in the interval of 1411
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x/i; i ++ ) {
        if (x % i == 0) return false;
    }
    return true;
}

int f(int x) {
    int t = 0;
    while (x) {
        t = t*10 + x%10;
        x /= 10;
    }

    return t;
}

int main() {
    int m, n;

    cin >> m >> n;

    bool flag = true;
    for (int i = m; i <= n; i ++ ) {
        int t = f(i);
        if (isPrime(i) && isPrime(t)) {
            if (flag) {
                cout << i;
                flag = false;
            }
            else {
                cout << ',' << i;
            }
        }
    }

    if (flag) cout << "No" << endl;

    return 0; 
}
1412 Binary classification
#include <iostream>
using namespace std;

int f(int x) {
    int cnt_1 = 0, cnt_0 = 0;
    while (x) {
        if (x%2) cnt_1 ++;
        else cnt_0 ++;
        x /= 2;
    }

    if (cnt_1 > cnt_0) return 1;
    else return 0;
}

int main() {
    int cnt = 0;
    for (int i = 1; i <= 1000; i ++ ) {
        if (f(i)) cnt ++;
    }

    cout << cnt << ' ' << 1000 - cnt << endl;

    return 0; 
}
1413 Determined base
#include <iostream>
using namespace std;

int f(int x, int i) {
    int ans = 0, t = 1;
    while (x) {
        if (x % 10 >= i) return -1;
        ans += x % 10 * t;
        t *= i;
        x /= 10;
    }
    return ans;
}

int main() {
    int p, q, r;
    cin >> p >> q >> r;

    for (int i = 2; i <= 40; i ++ ) {
        int p1, q1, r1;
        p1 = f(p, i);
        q1 = f(q, i);
        r1 = f(r, i);

        if (p1 * q1 == r1) {
            cout << i << endl;
            return 0;
        }
    }

    cout << 0 << 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/106139648