zoukankan      html  css  js  c++  java
  • 每日两题编程

    此博客链接:https://www.cnblogs.com/ping2yingshi/p/12419542.html

    1.分糖果(力扣57min)

    题目链接:https://leetcode-cn.com/problems/distribute-candies-to-people/submissions/

    排排坐,分糖果。
    我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友。
    给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果。
    然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果。
    重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果。注意,就算我们手中的剩下糖果数不够(不比前一次发出的糖果多),这些糖果也会全部发给当前的小朋友。
    返回一个长度为 num_people、元素之和为 candies 的数组,以表示糖果的最终分发情况(即 ans[i] 表示第 i 个小朋友分到的糖果数)。
     
    示例 1:
    输入:candies = 7, num_people = 4
    输出:[1,2,3,1]
    解释:
    第一次,ans[0] += 1,数组变为 [1,0,0,0]。
    第二次,ans[1] += 2,数组变为 [1,2,0,0]。
    第三次,ans[2] += 3,数组变为 [1,2,3,0]。
    第四次,ans[3] += 1(因为此时只剩下 1 颗糖果),最终数组变为 [1,2,3,1]。

    示例 2:
    输入:candies = 10, num_people = 3
    输出:[5,2,3]
    解释:
    第一次,ans[0] += 1,数组变为 [1,0,0]。
    第二次,ans[1] += 2,数组变为 [1,2,0]。
    第三次,ans[2] += 3,数组变为 [1,2,3]。
    第四次,ans[0] += 4,最终数组变为 [5,2,3]。
     
    提示:

     1 <= candies <= 10^9
     1 <= num_people <= 1000
    题解:
             题意:循环给围成一个圈的小朋友发糖,每发发糖比上次多一个糖,直到手里糖不够,不够的糖也给最后一个小朋友。
             方法:
                     1.定义一个数组,数组内的坐标对小朋友人数取余,这样能循环发糖。
                     2.用Math库函数取手中剩的糖和每次发糖增加的数目的交小值(这里其实是对于最后一个小朋友得到的糖做判断,只有最后一个小朋友得到的糖是小于等于手中剩的糖的个数)。
                     3.更新糖果数目,发完一次糖,糖果数组还剩糖果数减去上次已发的糖果数目。
       代码如下:
    class Solution {
        public int[] distributeCandies(int candies, int num_people) {
            int i=0;
            int[]arr=new int[num_people];
            while(candies>0)
            {
                        arr[i%num_people]+=Math.min(candies,i+1);
                        candies-=i+1;
                        i++;
                
        
            }
         return arr;
        }
    }

    一开始写的代码,对最后一个小朋友得到的糖果数做判断来写的代码,但是超时。

    代码如下:

    class Solution {
        public int[] distributeCandies(int candies, int num_people) {
            int i;
            int[]arr=new int[num_people];
            int count=0;
            while(candies>0)
            {
                 for(i=0;i<num_people;i++)
                  {
                        count++;
                        if(candies>=count)
                        {
                            arr[i]+=count;
                            candies=candies-count;
                        }
                        else 
                            arr[i]+=candies;
                  }
        
            }
         return arr;
        }
    }

     2.Switch Game(杭电 59min)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2053

    Problem Description
    There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
     
    Input
    Each test case contains only a number n ( 0< n<= 10^5) in a line.
     
    Output
    Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
     
    Sample Input
    1
    5
     
    Sample Output
    1
    0
    Hint
    hint
    Consider the second test case: The initial condition : 0 0 0 0 0 … After the first operation : 1 1 1 1 1 … After the second operation : 1 0 1 0 1 … After the third operation : 1 0 0 0 1 … After the fourth operation : 1 0 0 1 1 … After the fifth operation : 1 0 0 1 0 … The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.

     题解:(我英语太难了,一开始题目愣是没有看懂,然后搜索别人写的,基本好多就一句话一带而过,我还是不懂,最后还是看题目最后那个样例,一点一点推才看懂了题目)

           题意:此题意思是,给定一个数,给的n是几,就有几盏灯,比如给的n是1就只有一盏灯,给的n是5就有5盏灯,判断最后一盏灯是打开还是关上。判断规则:灯一开始全是关上,对灯操作n边,第一边对是1的倍数的灯取反情况(即打开的就关上,关上的就打开),第二遍对灯好事2的灯取反情况,依次类推:

                           on 1
                           off 0
                                                                            灯取反编号                      灯状态
    n=0                                                                                                      0
    n=1          0-1>1                                                       1                               1
    n=2         00-1>11    11-2>10                                           1  2                            0
    n=3        000-1>111   111-2>101   101-3>100                             1   3                           0    
    n=4       0000-1>1111  1111-2>1010  1010-3>1000   1000-4>1001            1 2 4                           1
    n=5     00000-1>11111   11111-2>10101  10101-3>10001  10001-4>10011  
           10011-5>10010                                                     1  5                                                   0
    n=6                                                                      1 2 3 6                          0
    n=7                                                                      1  7                             0
    n=8                                                                      1 2 4 8                          0
    n=9                                                                      1 3  9                           1
    ……..
    依次类推       n的约数为偶数时,结果为0,为奇数数结果为1

          方法:判断约数是奇数还是偶数。

         思路:由上面推导可以看出,取反的灯个数为偶数时,最后一个灯的状态为0,灯的个数为奇数时,灯的个数为1。

    (我还发现灯取反的个数为奇数时,n为平方数,不知道后面数是不是这样。)

    代码如下:

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
        int n;
        while (~scanf_s("%d",&n))
        {
            int i;
            int count=0;//记录约数个数
            for ( i = 1; i <= n; i++)
            {
              if(n%i==0)
                   count++;
            }
            if(count%2==0)//约数为偶数输出0,否则输出1
               printf("0
    ");
            else 
               printf("1
    ");
    
        }
        return 0;
            
    }

           

  • 相关阅读:
    android操作数据库
    Android读写SD卡上的文件
    第四章 函数与程序结构
    getchar()与EOF
    NULL, '',0 '0'的区别
    TCPL 行计数
    行计数
    getchar()用法
    在C语言中,double、long、unsigned、int、char类型数据所占字节数
    队列——解密QQ号
  • 原文地址:https://www.cnblogs.com/ping2yingshi/p/12419542.html
Copyright © 2011-2022 走看看