zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 182B

    B - Almost GCD

    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 200 points

    Problem Statement

    Given is an integer sequence AA1,A2,A3,,AN.
    Let the GCD-ness of a positive integer k be the number of elements among A1,A2,A3,…,AN that are divisible by k.
    Among the integers greater than or equal to 2, find the integer with the greatest GCD-ness. If there are multiple such integers, you may print any of them.

    Constraints

    • 1N100≤N≤100
    • 2Ai1000
    • All values in input are integers.

    Input

    Input is given from Standard Input in the following format:

    N

    A1A2A3AN

    Output

    Print an integer with the greatest GCD-ness among the integers greater than or equal to 22. If there are multiple such integers, any of them will be accepted.


    Sample Input 1 

    3

    3 12 7

    Sample Output 1 

    3

    Among 312, and 7, two of them - 3 and 12 - are divisible by 3, so the GCD-ness of 3 is 2.

    No integer greater than or equal to 2 has greater GCD-ness, so 3 is a correct answer.


    Sample Input 2 

    5

    8 9 18 90 72

    Sample Output 2 

    9

    In this case, the GCD-ness of 9 is 4

    2 and 3 also have the GCD-ness of 4, so you may also print 2 or 3.


    Sample Input 3 

    5

    1000 1000 1000 1000 1000

    Sample Output 3

    1000

    解题思路:拿到手,第一眼(没看题)看了输入的形式加上有个GCD我还以为要靠贝祖定理了呢,然后仔细分析是求一个K,K满足两个条件

    ①:K是输入的数组的元素的公因数,且从2开始。

    ②:k的倍数的数最多(当然这样的话就会有很多情况,随便输出一种情况就行)

     再一看数据,这么小直接二重循环暴力出来啦,外层循环从2开始,一直到1000,内次循环遍历数组,每次内存循环记录满足倍数条件的个数

    Code:

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    using namespace std;
     
    int a[1005];
    
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i = 1; i <= n; ++i) {
            scanf("%d",&a[i]);
        }
        int max_ = 0,max_key = a[1],loc;
        for(int i = 2; i <= 1000; ++i) {
            loc = 0;//表示的是每次循环i的倍数的个数(当然是对a来说的)
            for(int j = 1; j <= n; ++ j) {
                if(a[j] % i == 0)
                loc++;
            }
            if(loc > max_) {//如果i的倍数比记录的大,就更新
                max_ = loc;
                max_key = i;
            }
        }
        printf("%d
    ",max_key);
        return 0;
    }

     开始以为k只能是a数组的元素,wa了很多发T^T

  • 相关阅读:
    测试爬虫
    流式大数据处理的三种框架:Storm,Spark和Samza
    redo日志
    HTTP协议之chunk编码(分块传输编码
    QWidget 实现 打破布局 或者 当前窗体内的 弹窗 (借助伪造实现)
    How to use kingshard building a MySQL cluster
    转载: Qt 学习之路 2归档
    Python与机器人技术
    Nginx配置正向代理
    使用Chrony配置 NTP
  • 原文地址:https://www.cnblogs.com/Mangata/p/13950311.html
Copyright © 2011-2022 走看看