当前位置:网站首页>【数论】leetcode1006. Clumsy Factorial
【数论】leetcode1006. Clumsy Factorial
2022-06-21 15:33:00 【暮色_年华】
The factorial of a positive integer n is the product of all positive integers less than or equal to n.
For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.
We make a clumsy factorial using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply '*', divide '/', add '+', and subtract '-' in this order.
For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.
However, these operations are still applied using the usual order of operations of arithmetic. We do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.
Additionally, the division that we use is floor division(下取整) such that 10 * 9 / 8 = 90 / 8 = 11.
Given an integer n, return the clumsy factorial of n.
题意:求clumsy factorial ,*/+-四个符号交替使用。
因为/是下取整,所以可以分离常数。


剩下不能够成 4个一组成对「消去」的情况需要分类讨论。由于「笨阶乘」按照「乘」「除」「加」「减」循环的顺序定义运算,将 n 按照对 4 取模的余数分类讨论。
(1)n%4==0

(2)n%4==1
(3)n%4==2
(4)n%4==3
当 n≤4 时,可以分别单独计算「笨阶乘」;
当n>4 时,可以根据 n 对 4 取模的余数进行分类讨论。
class Solution { public int clumsy(int n) { if(n==1)return 1; if(n==2)return 2; if(n==3)return 6; if(n==4)return 7; if(n%4==0)return n+1; if(n%4<=2)return n+2; return n-1; } }
边栏推荐
- 2022awe opened in March, and Hisense conference tablet was shortlisted for the review of EPLAN Award
- ConnectionOptions. Username attribute definition
- C language pointer
- Get the mobile number of QQ friends through exhaustive search
- R language uses file of FS package_ Access function, file_ Exists function, dir_ Exists function, link_ The exists function checks whether the file can be accessed, whether the file exists, whether th
- Retrieve the compressed package password
- Which service provider is cheaper to do website penetration testing
- [Yugong series] February 2022 wechat applet -app Networktimeout of JSON configuration attribute
- November 28, 2020 merge sort and merge in place
- Merge two ordered linked lists
猜你喜欢

Go language - structure

原生JS路由,iframe框架

Build an efficient and scalable result cache

AAAI 2022 | sasa: rethinking point cloud sampling in 3D object detection

Online keyboard key detection tool

Go language - Interface

Tomb. Weekly update of Finance (February 14-20)

Research Report on the overall scale, major producers, major regions, products and application segments of active aluminum chloride in the global market in 2022

GO语言-type关键字

Select everything between matching brackets in vs Code - select everything between matching brackets in vs Code
随机推荐
Word thesis typesetting tutorial
GO语言-结构体
Based on Transformer's artificial neural network, the image of organic structure is transformed into molecular structure
Je ne veux pas ouvrir un compte en ligne.
Native JS routing, iframe framework
AAAI 2022 | sasa: rethinking point cloud sampling in 3D object detection
Tomb. Weekly update of Finance (February 14-20)
Rk3399 platform development series explanation (network debugging) 7.32. What is network routing?
C language selection type supplement
New project template of punctual atom F103 based on firmware library
Dentron: ultra fast open source notes in vscode
Live long to see the true chapter
马拦过河卒
What is a good product for children's serious illness insurance? Please recommend it to a 3-year-old child
Go admin framework analysis (2-1)
Elastic stack Best Practices Series: a filebeat memory leak analysis and tuning
Gold, silver and four interviews are necessary. The "brand new" assault on the real topic collection has stabilized Alibaba Tencent bytes
What is the server room
Turn off WordPress auto thumbnail
Shared memory communication between processes