腾讯2020校园招聘笔试

腾讯2020校园招聘笔试_第1张图片

输入1:

2 2
1 1
1 1

输出1:

0

输入2:

2 2
1 2
2 1

输出2:

2

import java.util.Scanner; 
public class Main { 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[]a=new int[n];
        int[]b=new int[m];
        int aeven=0,aodd=0;
        int beven=0,bodd=0;
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
            if(a[i]%2==1)
                aeven++;
            else 
                aodd++;
        }
        for(int j=0;j<m;j++){
            b[j]=sc.nextInt();
            if(b[j]%2==1)
                beven++;
            else 
                bodd++;
        }
        int temp=Math.min(aeven, bodd)+Math.min(aodd, beven);
        System.out.println(temp);
        sc.close();
    }
}

腾讯2020校园招聘笔试_第2张图片
腾讯2020校园招聘笔试_第3张图片
腾讯2020校园招聘笔试_第4张图片

import java.util.Arrays;
import java.util.Scanner;
 
public class Main { 
	
    public static void main(String[] args) {       
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        long[] b = new long[n];
        long[] c = new long[n];
        long sum = 0, ans = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextLong();
            b[i] = sc.nextLong();
            sum += b[i] * n - a[i];
            c[i] = a[i] - b[i];
        }
        Arrays.sort(c);
        for (int i = 1; i <= n; i++) {
            ans += c[n - i] * i;
        }
        System.out.println(ans + sum);
        sc.close();
    } 
}
//有大佬用贪心实现的
#include
#include
#include
using namespace std;
int n;
struct person
{
    int a,b;
}num[111111];
bool cmp(person a,person b)
{
    return a.b+b.a<a.a+b.b;
} 
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&num[i].a,&num[i].b);
    }
    sort(num,num+n,cmp);
    long long ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=(long long)i*num[i].a;
        ans+=(long long)num[i].b*(n-1-i);
    }
    cout<<ans<<endl;
}

你可能感兴趣的:(笔试,笔试,腾讯,2020)