【leetcode】20190825字节跳动笔试

第一道

# -*- coding:utf-8 -*-

def Solution(N):

    M = []
    for i in range(N):
        input_x = [int(x) for x in input().split()]
        M.append(input_x)
    # M = [[0,4,0], [4,0,0],[0,0,0]]

    flag = [False] * N
    q = []
    res = 0
    for start in range(N):
        if not flag[start]:
            res += 1
            q.append(start)
            while q:
                now = q.pop(0)
                flag[now] = True
                for i in range(N):
                    if M[now][i] >= 3 and not flag[i]:
                        q.append(i)
                        flag[i] = True

    return res

# tmp = input()
N = int(input())

res = Solution(N)
print(res)

第二道

卡特兰数列:ACM数论-卡特兰数Catalan

#include <iostream>
#include <string>
using namespace std;

const long long MOD = 1000000007;
long long ans[2000];

void Egcd(long long a,long long b, long long &x,long long &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return ;
    }
    Egcd(b, a%b, x, y);
    long long tmp=x;
    x = y;
    y = tmp-a/b*y;
}

int main()
{
    int n;
    ans[0] = 0,
    ans[1] = 1;
    for(int i = 2; i <= 1000; ++i)
    {
        long long  x,y;
        Egcd(i+1, MOD, x, y);
        ans[i] = ans[i-1]*(4*i-2)%MOD*(x%MOD+MOD)%MOD;
    }
    cin >> n;
    cout << ans[n/2] << endl;
    return 0;
}

第三道

#include <iostream>
#include <string>

using namespace std;
int a[5][5];

void r(int tr, int tc, int dr, int dc) {
    int times = dc - tc;
	int temp = 0;
	for (int i = 0; i < times; i++)
	{
		temp = a[tr][tc + i];
		a[tr][tc + i] = a[dr - i][tc];
		a[dr - i][tc] = a[dr][dc - i];
		a[dr][dc - i] = a[tr + i][dc];
		a[tr + i][dc] = temp;
	}
}

void R(int d) {
    int k = 0;
    if (d == 2) {
        k = 2;
    } else if (d == 4) {
        k = 3;
    } else if (d == 3) {
        k = 1;
    }
    while (k--) {
        int tr = 0, tc = 0, dr = 3, dc = 3;
        while (tr < dr)
        {
            r(tr++, tc++, dr--, dc--);
        }
    }
}

void L(int d) {
    int k = 0;
    if (d == 2) {
        k = 2;
    } else if (d == 4) {
        k = 1;
    } else if (d == 3) {
        k = 3;
    }
    while (k--) {
        int tr = 0, tc = 0, dr = 3, dc = 3;
        while (tr < dr)
        {
            r(tr++, tc++, dr--, dc--);
        }
    }
}

int main()
{
    int d;
    cin >> d;
    for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j)
            cin >> a[i][j];
    R(d);

    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 4; ++j)
            if (a[i][j] == a[i+1][j] && a[i][j]) {
                a[i][j] *= 2;
                a[i+1][j] = 0;
            }
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 4; ++j)
            if (a[i][j] == 0) {
                int k = i+1;
                while (k < 4 && a[k][j] == 0)
                    ++k;
                if (k < 4) {
                    a[i][j] = a[k][j];
                    a[k][j] = 0;
                }
            }

    L(d);
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (j) cout << " ";
            cout << a[i][j];
        }
        cout << endl;
    }

    return 0;
}

第四道

思路:自己建立表,然后广搜

# -*- coding:utf-8 -*-
import math
def Solution(N):
    nums = [int(x) for x in input().split()]
    # M = [[0,4,0], [4,0,0],[0,0,0]]
    M = []
    for i in range(N):
        tmp = [0] * N
        M.append(tmp)

    for i in range(N):
        for j in range(i, N):
            if math.gcd(nums[i], nums[j]) > 1:
                M[i][j], M[j][i] = 1, 1
    
    flag = [False] * N
    q = []
    res = 0
    maxres = 0

    for start in range(N):
        if not flag[start]:
            if res > maxres:
                maxres = res
            res = 0
            q.append([start])
            while q:
                nowq = q.pop(0)
                tmp = []
                while nowq:
                    res += 1
                    now = nowq.pop(0)
                    flag[now] = True
                    for i in range(N):
                        if M[now][i] > 0 and not flag[i]:
                            tmp.append(i)
                            flag[i] = True
                if tmp:
                    q.append(tmp)
    return maxres

# tmp = input()
N = int(input())


res = Solution(N)
print(res)

# 使用gcd会30%的数据超时,使用素数打表100

#include <bits/stdc++.h>
#define IO                       \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define mem(a, x) memset(a, x, sizeof(a))
#define per(x, a, b) for (int x = a; x <= b; x++)
#define rep(x, a, b) for (int x = a; x >= b; x--)

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-8;
int fa[maxn], rk[maxn];

int find_set(int x) {
    if (x != fa[x])
        fa[x] = find_set(fa[x]);
    return fa[x];
}

bool union_set(int x, int y) {
    x = find_set(x);
    y = find_set(y);
    if (x == y)
        return false;
    if (rk[x] > rk[y])
        fa[y] = x;
    else {
        fa[x] = y;
        if (rk[x] == rk[y])
            ++rk[y];
    }
    return true;
}

int a[maxn], n;
int cnt[maxn];
vector<int> G[maxn];

void getfac(int x) {
    int tmp = x;
    for (int i = 2; i * i <= tmp; i++) {
        if (tmp % i == 0) {
            G[i].push_back(x);
            while (tmp % i == 0)
                tmp /= i;
        }
    }
    if (tmp > 1)
        G[tmp].push_back(x);
}

void solve() {
    mem(cnt, 0);
    for (int i = 0; i < maxn; i++) {
        fa[i] = i;
        G[i].clear();
    }
    for (int i = 0; i < n; i++) {
        getfac(a[i]);
    }
    for (int i = 2; i < maxn; i++) {
        if (G[i].size() > 0) {
            int len = G[i].size();
            for (int j = 1; j < len; j++) {
                union_set(G[i][j - 1], G[i][j]);
            }
            // cout << i << ": ";
            // for (auto s : G[i]) {
            //     cout << s << " ";
            // }
            // cout << endl;
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        cnt[find_set(a[i])]++;
    }
    for (int i = 0; i < n; i++) {
        ans = max(ans, cnt[find_set(a[i])]);
    }
    cout << max(1, ans) << endl;
}
int main() {

#ifdef LOCAL_IM0QIANQIAN
    freopen("test.in", "r", stdin);
//    freopen("test.out", "w", stdout);
#else
    IO;
#endif // LOCAL_IM0QIANQIAN

    cin >> n;
    if (n == 0) {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] <= 1) {
            --n;
            --i;
        }
    }
    solve();
    return 0;
}


/**
6
20 50 22 74 9 63
1
1
2
1 2
2
2 4

**/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值