迅雷 2019校园招聘笔试编程题-2018.09.12

版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82706974

1-1
1-2.png
1-3.png
类似题目 UVA106- Fermat vs. Pythagoras
分析
参考链接,点击
分析
代码:

#include <bits/stdc++.h>

using namespace std;
bool gcd(int a, int b){//最大公约数
    int temp;
    while(b > 0){
        temp = a % b;
        a = b;
        b = temp;
    }
    if(a == 1)
        return true;
    return false;
}

int main() {
    int n;
    scanf("%d", &n);
    int res = 0;
    int maxLen = sqrt(n);
    for (int i=1; i<=maxLen; i++){
        for (int j=i+1; j<=maxLen; j++){
            int c = j*j + i*i;
            if (c > n)
                break;
            int a = j*j - i*i;
            int b = 2*i*j;
            if (gcd(a, b) && gcd(a, c) && gcd(b, c)){
                res++;
            }
        }
    }
    printf("%d\n", res);
    return 0;
}

2-1.png
2-2.png
暴力AC了 其实规律也很简单 注释中给出规律了

#include <iostream>

using namespace std;

int main()
{
    int res = 0;
    int temp_A, temp_B;
    cin >> temp_A >> temp_B;
    if (temp_A*6+temp_B<0){
        res = (temp_A*6+temp_B)*2 + temp_A*3;
    }else if(temp_A*5+temp_B*2<0){
        res = (temp_A*5+temp_B*2)*2 + temp_A*3;
    }else if(temp_A*4+temp_B*3<0){
        res = (temp_A*4+temp_B*3)*2 + temp_A*3;
    }else if(temp_A*3+temp_B*4<0){
        res = (temp_A*3+temp_B*4)*2 + temp_A*3;
    }else if(temp_A*2+temp_B*5<0){
        res = (temp_A*2+temp_B*5)*2 + temp_A*2 + temp_B;
    }else if(temp_A*1+temp_B*6<0){
        res = (temp_A*1+temp_B*6)*2 + temp_A + temp_B*2;
    }else{
        res = temp_B*17;
    }
    cout << res << endl;
    return 0;
}
/*
A:1 B:0
(1 1 1 1 1 1 0)  (1 1 1 1 1 1 0)  (1 1 1)
(1 1 1 1 1 0 0)  (1 1 1 1 1 0 0)  (1 1 1)
(1 1 1 1 0 0 0)  (1 1 1 1 0 0 0)  (1 1 1)
(1 1 1 0 0 0 0)  (1 1 1 0 0 0 0)  (1 1 1)
(1 1 0 0 0 0 0)  (1 1 0 0 0 0 0)  (1 1 0)
(1 0 0 0 0 0 0)  (1 0 0 0 0 0 0)  (1 0 0)
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0)
*/

猜你喜欢

转载自blog.csdn.net/whl_program/article/details/82706974