zoukankan      html  css  js  c++  java
  • lightoj 1148 Mad Counting(数学水题)

    lightoj 1148 Mad Counting

    链接http://lightoj.com/volume_showproblem.php?problem=1148

    题意:民意调查,每一名公民都有盟友,问最少人数。

    思路:考察的知识点有两个:第一是整数相乘取上整;第二是容器大小(ps:不能算一个知识点,只能算一个坑点)。

    做题思路:排序,如果被调查的人有相同盟友人数(n)的个数(cnt)大于这个数+1,即:(cnt > n+1), 容器已满,只能新开辟一个容器来装盟友。

    代码

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #define N 51
     5 using namespace std;
     6 
     7 int a[N];
     8 
     9 int main()
    10 {
    11     int t, ic = 1, n, i;
    12     scanf("%d", &t);
    13     while(t--)
    14     {
    15         scanf("%d", &n);
    16         for(i = 0; i < n; i++)
    17             scanf("%d", a+i);
    18         sort(a, a+n);
    19         int k , ans = 0, cnt = 0;
    20         for(i = 0; i < n; i++)
    21         {
    22             if(i == 0 || a[i] == a[i-1]) cnt++;
    23             else
    24             {
    25                 k = (cnt+a[i-1]) / (a[i-1]+1); //+1 是因为加上自己人数,分子加上a[i-1]是因为取上整
    26                 ans += k*(a[i-1]+1);
    27                 cnt = 0;
    28             }
    29         }
    30         ans += (cnt+a[n-1]) / (a[n-1]+1) * (a[n-1]+1);  //最后一组没有参加for循环的计算,这里单独算 
    31         printf("Case %d: %d
    ", ic++, ans);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    最大上升子序列
    vue的keep-alive组件
    对小程序的研究3
    对getBoundingClientRect属性的研究
    消除浮动的方式
    对微信小程序的研究2
    对小程序的研究1
    对props的研究
    对provide/inject的研究
    对calc()的研究
  • 原文地址:https://www.cnblogs.com/Duahanlang/p/3409179.html
Copyright © 2011-2022 走看看