[Every day a small algorithm (82)] [Exhaustive] (vivo written test questions) Calculate the total number of mobile phones that can be mass produced on the nth day.

Foreword:
Time flows down the river and lives against the current.

Title description

  • On the vivo production line, as each employee's familiarity and experience with mobile phone processing increases, the daily output will continue to soar.
  • Assume that one unit is mass-produced on the first day, and two units are mass-produced every day for the next two days (that is, the second and third days), and the next three days (that is, the third, fourth, and fifth days)
  • Mass production of 3 pieces per day. By analogy, calculate the total number of mobile phones that can be mass produced on the nth day.

Topic analysis

This question, I used exhaustive. Use an array to save the number of mobile phones in mass production every day, and so on.

Code sample

package com.asong.leetcode.TwoJianZhiOffer;

/**
 * 在vivo生产线上,每位职工随着对手机加工流程认识的熟悉和经验的增加,日产量也会不断飙升。
 * 假设第一天量产1台,接下来2天(即第二、三天)每天量产2件,接下来三天(即第三、四、五天)
 * 每天量产3件。以此类推,计算出第n天总共可以量产的手机数量。
 */
public class Vivo2 {
    public static void main(String[] args) {
        Vivo2 vivo2 = new Vivo2();
        int res = vivo2.Num(11);
        System.out.println(res);
    }
    public int Num(int n)
    {
        if(n<=0)
        {
            return 0;
        }
        int[] dp =new  int[n+1]; //保存量产的手机数量
        int one = 1;
        int count = 0;
        for (int i = 1; i <= n; i++) {
            dp[i] = dp[i-1] + one;
            count++;
            if(count==one)
            {
                count=0;
                one++;
            }
        }
        return dp[n];
    }
}

Published 197 original articles · praised 73 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_39397165/article/details/104748723