Olimpiada de Informática One Pass 1.6: Funciones (1)

La primera parte del lenguaje C ++

Capítulo 6 Funciones

La función de la primera sección

1150 Encuentra el número perfecto entre un entero positivo 2 y 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 Número de primos
#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 Número máximo 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 primo absoluto
#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 Número de afinidad
#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 Palíndromo de tres dígitos
#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 Halla el valor de π
#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 Conjetura de Goldbach
#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 Evaluación de expresión aritmética simple
#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 facturación por SMS
#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 Examen preliminar de pacientes con 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 cuenta el número de palabras
#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 Traducción automática
#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 Contraseña de Vigenère
#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 Par de números primos
#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 Mi número de casa
#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 Suma y producto de números primos
#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 Sustitución de palabras
#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 Pequeño mono estúpido
#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 Número de palíndromos primos
#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 Determina el número de primos
#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 Secuencia máxima de factores primos
#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; 
}
Verdaderos primos en el intervalo de 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 Clasificación binaria
#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 Base determinada
#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; 
}

Si su hijo está en cuarto grado en adelante, está interesado en la programación de computadoras y tiene capacidad adicional para lecciones culturales, comuníquese con el servicio al cliente (ID de WeChat: xiaolan7321) para participar en el aprendizaje de la informática. Somos entrenadores profesionales de competencias de informática, utilizando métodos de enseñanza de clases pequeñas en línea, el objetivo es ayudar a los estudiantes de primaria y secundaria que aman la programación a lograr excelentes resultados en competencias de informática nacionales y extranjeras.

Funciones de enseñanza:

  • Enseñanza en línea en clases pequeñas, establezca una buena base de código. Evite el problema de "no puedo mantener el ritmo" o "no comer lo suficiente" en clases grandes.

  • Rica experiencia docente, familiarizado con la estructura de conocimiento y la capacidad de aprendizaje de los estudiantes, y organice el horario de manera razonable.

  • Practique con competencias y mejore continuamente las habilidades de los estudiantes a través de exámenes y competencias.

Supongo que te gusta

Origin blog.csdn.net/davidliule/article/details/106139648
Recomendado
Clasificación