Enzo Li @ eynzof.com

算法

2019.11.14

https://blog.csdn.net/bat67/article/details/79685997

Q1016

#include <iostream>
using namespace std;

int main() {

    int h, m, s;
    int h2, m2, s2;

    // 连续两行输入时间
    scanf("%02d:%02d:%02d", &h, &m, &s);
    scanf("%02d:%02d:%02d", &h2, &m2, &s2);
    cout << (h2 - h) * 3600 + (m2 - m) * 60 + s2 - s;

    return 0;

}

Q1017

#include <cmath>
#include <iostream>

using namespace std;

int main() {

    int yr;
    double m, rs;

    while (cin >> yr >> m) {
        printf("%.6f",pow(1.0225, yr) * m);
    }
    return 0;
}

Q1017

#include <iostream>

using namespace std;

int main() {

    int num;
    while (cin >> num) {
        int i = 0;
        while (num > 0) {
            num /= 10;
            i++;
        }
        cout << i << endl;
    }

    return 0;
}

Q1018

#include <iostream>

using namespace std;

int main() {

    int num;
    while (cin >> num) {
        cout << (num % 2 == 0 ? "even" : "odd");
    }

    return 0;
}

Q1019

#include <iostream>

using namespace std;

int main() {

    int num;
    while (cin >> num) {
        cout << (num >= 30 ? num * 48 : num * 50);
    }

    return 0;
}

Q1020

#include <iostream>

using namespace std;

int main() {

    int a, b;
    while (cin >> a >> b) {
        cout << min(a, b) << " " << max(a, b);
    }

    return 0;
}

q1022

int main() {
//    排序三个整数
//    三个if是并列关系,依次执行下来。
//    第一个if用来比较a和b的大小,如果a>b则交换a,b的值,保证a比b小。
//    第二个if用来比较b和c的大小,如果b>c则交换b,c的值,保证b比c小。
//    由于原来可能a比c大,第二次的比较与交换后,需要再次比较a和b。
//    这个程序的意思就是把大的数依次往后挪。
    int a, b, c,x;
    while (cin >> a >> b >> c) {
        if(a>b)
        {x=a;a=b;b=x;}
        if(b>c)
        {x=b;b=c;c=x;}
        if(a>b)
        {x=a;a=b;b=x;}
        printf("%d %d %d",c,b,a);
    }

    return 0;
}

q1023

#include <iostream>

using namespace std;

int main() {

    char c;
    while (cin >> c) {
        cout << (char)toupper(c);
    }

    return 0;
}

q1024

#include <iostream>

using namespace std;

int main() {

    char c;
    while (cin >> c) {
        c = toupper(c);
        cout << (int)c-64<<endl;
    }

    return 0;
}

q1025 https://blog.csdn.net/zhengmmm1999/article/details/84202264 关于scanf的用法 http://c.biancheng.net/view/160.html

#include <iostream>

using namespace std;
// 对三个整数排序
int main() {
    char c[3], t;
    scanf("%c %c %c%*c", &c[0], &c[1], &c[2]);
    // 只求最大值,前面的排序可忽略
    if (c[0] > c[1]) {
        t = c[0];
        c[0] = c[1];
        c[1] = t;
    }
    if (c[1] > c[2]) {
        t = c[1];
        c[1] = c[2];
        c[2] = t;
    }
    cout << c[2] << endl;
}

q1026

#include <iostream>

using namespace std;

int main() {
    char c;
    while (cin >> c) {
        cout << (c < 48 ? "other" :
                 (c <= 57 ? "digit" :
                  (c <= 64 ? "other" :
                   (c <= 90 ? "upper" :
                    (c <= 96 ? "other" :
                     (c <= 122 ? "lower" : "other")))))) << endl;
    }
}

q1027

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int n, a, b, c;
    while (cin >> n) {
        a = n / 100;
        b = (n % 100 / 10);
        c = n % 10;
        cout << (n == (pow(a, 3) + pow(b, 3) + pow(c, 3)) ? "yes" : "no") << endl;
    }
}

q1028

#include <iostream>

using namespace std;
// 四年一闰;百年不闰,四百年再闰
int main() {
    int n;
    while (cin >> n) {
        cout << (n % 400 == 0 ? "Yes" : (n % 100 == 0 ? "No" : n % 4 == 0 ? "Yes" : "No")) << endl;
    }
}

q1031

#include <iostream>

using namespace std;

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    cout << (a > 0 ? (b > 0 ? 1 : 4) : (b > 0 ? 2 : 3));
}

q1036

#include <iostream>

using namespace std;

int main() {
    int y, m;
    int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    while (cin >> y >> m) {
        cout << (m == 2 ? (y % 400 == 0 ? 29 : ((y % 100 == 0) ? 28 : (y % 4 == 0)) ? 29 : 28) : month[m - 1]);
    }
}

q1037

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    float a, b;
    char op;
    scanf("%f %c %f", &a, &op, &b);
    switch (op) {
        case '+':
            printf("%.2f", a + b);
            break;
        case '-':
            printf("%.2f", a - b);
            break;
        case '*':
            printf("%.2f", a * b);
            break;
        case '/':
            // 判断除数是0的方法
            if (abs(b) < 10e-10) {
                printf("Wrong input!");
            } else {
                printf("%.2f", a / b);
            }
            break;
        default:
            printf("Wrong input!");
    }
    return 0;
}

q1038

#include <iostream>
#include <cmath>

using namespace std;
//输出三个数中绝对值最大的数,单独占一行。若绝对值最大的数不唯一,则输出最先出现的那个。例如,若输入为1 -3 3,则输出为-3;若输入为1 3 -3则输出为3。
int main() {
    int n[3];
    while (cin >> n[0] >> n[1] >> n[2]) {
        if (abs(n[0]) > abs(n[1])) {
            swap(n[0], n[1]);
        }
        if (abs(n[1]) > abs(n[2])) {
            swap(n[1], n[2]);
        }
        if (abs(n[0]) > abs(n[1])) {
            swap(n[0], n[1]);
        }
        if (abs(n[1]) == abs(n[2])) {
            if (abs(n[0]) == abs(n[1])) {
                cout << n[0] << endl;
            } else {
                cout << n[1] << endl;
            }
        } else {
            cout << n[2] << endl;
        }
    }
    return 0;
}

q1039

#include <iostream>

using namespace std;
//输入一个整数n和n个整数,输出这n个整数的和。
int main() {
    int n, sum = 0;
    cin >> n;
    int *p = new int[n];
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        sum += p[i];
    }
    cout << sum << endl;
    return 0;
}

q1040

#include <iostream>

using namespace std;

int main() {
    int n;
    while (cin >> n) {
        if (n == 1) {
            printf("%.2f\n", 1.0f);
        } else {
            float sum = 1, odd = 3.0;
            for (int i = 0; i < n - 1; i++) {
                sum += 1.0f / odd;
                odd += 2;
            }
            printf("%.2f\n", sum);
        }
    }
    return 0;
}

q1041

#include <iostream>

using namespace std;

int main() {
    // 输入一个整数n,输出数列1-1/3+1/5-……前n项的和。
    int n;
    while (cin >> n) {
        double i = 1, s = 0, d = 1;
        for (i = 1; i <= 2 * n - 1; i += 2) {
            s += d / i;
            // 反转正负
            d = -d;
        }
        printf("%.2f\n", s);
    }
    return 0;
}

q1043

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int *p = new int[n];
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++)
            if(p[j]<p[j+1]) {
                swap(p[j], p[j + 1]);
            }

    }
    cout << p[0] << endl;
    return 0;
}

q1047

#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

// 输入两个正整数m和n,输出m到n之间每个整数的自然对数。
// 每行输出一个整数及其对数,整数占4列,对数占8列,右对齐,对数保留4位小数。

string getl(int a) {
    int i = 0;
    string r, r2;

    // 求出字符串长度
    i = to_string(a).length();
    for (int j = 0; j < 4 - i; j++) {
        r += " ";
    }
    // 第一个数字处理完成
    r += to_string(a);

    double l = log(a) * 10000.0;
    l = floor(l + 0.5) / 10000.0;
    string k = to_string(l).substr(0, 6);
    // 保留四位小数,.substr(0, 7) x10000小数点右移四位
    // 使用floor(x+0.5)进行四舍五入

    int kl = k.length();
    for (int j = 0; j < 8 - kl; j++) {
        r2 += " ";
    }
    r2 += k;
    r += r2;
    return r;
}

int main() {
    int s, e;
    cin >> s >> e;
    for (int i = s; i < e + 1; i++) {
        cout << getl(i) << endl;
    }
    return 0;
}

Q1049

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int s, e;
    cin >> s >> e;
    int r1 = 0, r2 = 0;
    for (int i = s; i <= e; i++) {
        if (i % 2 == 0) {
            r1 += pow(i, 2);
        } else {
//            r2 += i*i*i; 诡异的结果 输入2 5 输出152
            r2 += pow(i,3); // 输入2 5 输出151
            // pow的返回类型是double
        }
    }
    cout << r1 << " " << r2;
    return 0;
}

Q1061 求最大公约数,可用辗转相除法(欧几里得算法) 假如需要求 1997 和 615 两个正整数的最大公约数,用欧几里德算法,是这样进行的: 1997 / 615 = 3 (余 152) 615 / 152 = 4(余7) 152 / 7 = 21(余5) 7 / 5 = 1 (余2) 5 / 2 = 2 (余1) 2 / 1 = 2 (余0) 至此,最大公约数为1 可参考 https://blog.csdn.net/chen_zan_yu_/article/details/82943306

#include <iostream>
#include <cmath>

using namespace std;

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

    //不需要辗转相除的情况
    if (x % y == 0 || y%x==0) {
        cout << min(x,y);
        return 0;
    }
    while (x % y != 0) {
        z = x % y;
        x = y;
        y = z;
    }
    cout << z;
    return 0;
}

q1071 分解质因数

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    string rs = "";
    while (n > 1) {
        for (int i = 2; i <= n; i++) {
            // 2自动去除形如n*2的因子
            // 3自动去除形如n*3的因子
            // 以此类推剩下的都是质因子
            if (n % i == 0) {
                n /= i;
                rs += to_string(i);
                rs += " ";
                break;
            }
        }
    }
    cout << rs;
}

·