1. 程式人生 > >9.12 迅雷筆試程式設計題

9.12 迅雷筆試程式設計題

A
題意: 求素勾股個數
思路: 參考.

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <utility>
#include <map>
#include <set>
#include <cstdio>
#include <sstream>
#include <cctype> #define LL long long #define mst(a, b) memset(a, b, sizeof(a)) #define pill pait<int, int> #define ft first #define sd second #define pb push_back #define mk makr_pair using namespace std; const int qq = 1e5 + 10; const int INF = 1e9 + 10; const int MOD = 1e9 + 7; int
n; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif scanf("%d", &n); int ans = 0; for (int i = 1; 2 * i <= n; ++i) { for (int j = i + 1; 2 * i * j <= n; ++j) { if
(i * i + j * j > n) break; if (gcd(i, j) == 1) { if (i % 2 == 0 || j % 2 == 0) { ans++; } } } } printf("%d\n", ans); return 0; }

B
題意: 擺箱子
思路: 二進位制列舉所有情況

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <utility>
#include <map>
#include <set>
#include <cstdio>
#include <sstream>
#include <cctype>

#define LL long long
#define mst(a, b)   memset(a, b, sizeof(a))
#define pill pait<int, int> 
#define ft first
#define sd second
#define pb push_back
#define mk makr_pair

using namespace std;

const int qq = 1e5 + 10;
const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;

int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif
    int a, b;   scanf("%d%d", &a, &b);
    int n = 17;
    int num[20];
    int ans = -INF;
    for (int i = 0; i < (1 << n); ++i) {
        int maxn = 0;
        for (int j = 0; j < n; ++j) {
            num[j] = (i & (1 << j)) ? b : a;
            maxn += num[j];
        }
        bool f = true;
        int tmp = 0;
        for (int j = 0; j < 6; ++j) {
            tmp += num[j];
        }
        for (int j = 6; j < n; ++j) {
            tmp += num[j];
            if (tmp >= 0)   f = false;
            tmp -= num[j - 6];
        }
        if (f)  ans = max(ans, maxn);
    }
    printf("%d\n", ans);
    return 0;
}