zoukankan      html  css  js  c++  java
  • Handshakes(思维) 2016(暴力)

    Handshakes
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Description

    Last week, n students participated in the annual programming contest of Marjar University. Students are labeled from 1 to n. They came to the competition area one by one, one after another in the increasing order of their label. Each of them went in, and before sitting down at his desk, was greeted by his/her friends who were present in the room by shaking hands.

    For each student, you are given the number of students who he/she shook hands with when he/she came in the area. For each student, you need to find the maximum number of friends he/she could possibly have. For the sake of simplicity, you just need to print the maximum value of the n numbers described in the previous line.

    Input

    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first line contains an integer n (1 ≤ n ≤ 100000) -- the number of students. The next line contains n integers a1, a2, ..., an (0 ≤ ai < i), where ai is the number of students who the i-th student shook hands with when he/she came in the area.

    Output

    For each test case, output an integer denoting the answer.

    Sample Input

    2
    3
    0 1 1
    5
    0 0 1 1 1
    

    Sample Output

    2
    3
    题解:水题,就是参加运动会,依次进来n个人,每来一个都有朋友给他握手,问最多有多少个朋友,有两种可能;
    一:只要有人给其握手,那么把大于等于1的数字个数加起来,相当于第一个人进来给他们都握手。
    二:当前进来的人给其握手的最大值;
    比较第一和第二种情况的最大值就好了;
    代码:
    #include<cstdio>
    #include<cstring>
    
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            long long a,maxx=0,n;
            scanf("%lld",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%lld",&a);
                if(a>0)
                    maxx++;
                maxx=max(maxx,a);
            }
            printf("%lld
    ",maxx);    
        }
        return 0;
    }
    2016
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Description

    In mathematics, a polygonal number is a number represented as dots or pebbles arranged in the shape of a regular polygon. The dots are thought of as alphas (units). These are one type of 2-dimensional figurate numbers. The following picture shows how triangular numbers, square numbers, pentagonal numbers and hexagonal numbers represented as dots arranged in the shape of corresponding regular polygon.

    Polygonal Numbers: Triangular, Square, Pentagonal and Hexagonal numbers

    2016 is not only a leap year but also a triangular and hexagonal year. If you are patient enough, you can count the number of the dots in the left triangle or in the right hexagon in the following picture. The number of dots in each shape is 2016.

    2016 is a triangular-hexagonal-leap year

    Therefore, 2016 is a triangular-hexagonal-leap year. The previous triangular-hexagonal-leap year is 1540 and the next is 2556. So living to see 2016 is very rare experience.

    You task is to list the triangular-hexagonal-leap years from 2016 to 990528. 990528 is also a triangular-hexagonal-leap year.

    Input

    This problem has no input.

    Output

    Please print each triangular-hexagonal-leap year in increasing order.

    For example, if you are asked to list the triangular-hexagonal-leap years from 780 to 2556, the output should be:

     780
    1128
    1540
    2016
    2556
    

    Sample Output

    2016
    2556
    ...  <-- some lines are skipped
    990528
    题解:就是找2016--990528之间的闰年,满足三角形以及六边形的那两个公式的年份;
    n(n + 1)/2, n(2n - 1);
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<string>
    #include<cstdlib>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    bool triang(int x){
        int t = (int)sqrt(1.0 + 8 * x);
        if(t * t != 1 + 8 * x)
            return false;
        if((t - 1) % 2 != 0)
            return false;
    //    if(x == 2016)printf("%d %d %d
    ",t, t * t, 1 + 8 * x);
        return true;
    }
    bool hex(int x){
        int t = (int)sqrt(1.0 + 8 * x);
    //    if(x == 2016)printf("%d %d %d
    ",t, t * t, 1 + 8 * x);
        if(t * t != 1 + 8 * x)
            return false;
        if((t + 1) % 4 != 0)
            return false;
        return true;
    }
    bool leap(int x){
        if(x % 400 == 0 || (x % 4 == 0 && x % 100 != 0))
            return true;
        else
            return false;
    }
    int main(){
        for(int i = 2016; i <= 990528; i++){
            if(leap(i) && triang(i) && hex(i))
                printf("%d
    ", i);
        }
        return 0;
    }
  • 相关阅读:
    sortColors
    搜索二维矩阵
    矩阵置零
    求解最大子数组的和
    length of the longest substring without repeating character
    求解最大回文子字符串
    求两数的和
    验证回文串的一点解题思路
    fiddler-10-结合 Mock.js 伪接口数据进行测试(下)
    fiddler-9-结合 Mock.js 伪接口数据进行测试(上)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5408169.html
Copyright © 2011-2022 走看看