当前位置:网站首页>[number theory] leetcode1006 Clumsy Factorial
[number theory] leetcode1006 Clumsy Factorial
2022-06-21 15:44:00 【Twilight_ years】
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( Round down ) such that 10 * 9 / 8 = 90 / 8 = 11.
Given an integer n, return the clumsy factorial of n.
The question : seek clumsy factorial ,*/+- The four symbols alternate .
because / It's rounding down , So you can separate constants .


The rest cannot be done 4 One component pair 「 elimination 」 The situation needs to be classified and discussed . because 「 Stupid factorials 」 according to 「 ride 」「 except 」「 Add 」「 reduce 」 The order of the loop defines the operation , take n According to the right 4 Discussion on remainder classification of modulo .
(1)n%4==0

(2)n%4==1
(3)n%4==2
(4)n%4==3
When n≤4 when , Can be calculated separately 「 Stupid factorials 」;
When n>4 when , According to n Yes 4 Take the remainder of the module to discuss the classification .
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; } }
边栏推荐
- Finding minimum spanning tree by using union search set
- 33岁程序员的年中总结
- GO语言-接口
- 5700s layer 2 interface and layer 3 interface switching
- Selection (038) - what is the output of the following code?
- 真香!十五分钟搞定智能标注、模型训练、服务部署……
- 我不太想在网上开户,网上股票开户安全吗
- 【数论】leetcode1006. Clumsy Factorial
- 理财产品预约赎回确认日是什么?
- After the uproar, is the yuan universe "cool"?
猜你喜欢
![[Yugong series] February 2022 wechat applet -app Networktimeout of JSON configuration attribute](/img/51/dcd0062dbf5fbbd04c6fc3737a0be2.jpg)
[Yugong series] February 2022 wechat applet -app Networktimeout of JSON configuration attribute

C multithreading

对Integer进行等值比较时踩到的一个坑

Basic concepts of database

Perfect partner of ebpf: cilium connected to cloud native network

原生JS路由,iframe框架

使用APICloud实现文档下载和预览功能

Stm32l431 immediate sleep mode (code + explanation)
![[go] goroutine pool](/img/0c/4e78c59f9b4f963035c911cee3d62d.jpg)
[go] goroutine pool
![[Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute](/img/33/103a207dc085c431557981b3c9a1d5.jpg)
[Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute
随机推荐
Fluent encapsulates an immersive navigation bar with customizable styles NavigationBar
Gee Registration Guide
The select drop-down box prohibits drop-down and does not affect presentation submission
Je ne veux pas ouvrir un compte en ligne.
I don't really want to open an account online. Is it safe to open an account online
Unity grid programming 09
PLSQL learning log
Research Report on the overall scale, major producers, major regions, products and application segments of active aluminum chloride in the global market in 2022
我不太想在網上開戶,網上股票開戶安全嗎
泰山OFFICE技术讲座:域在MODEL的存储结构
Go language - Method
进程之间使用共享内存通信
Someone is storing credit card data - how do they do it- Somebody is storing credit card data - how are they doing it?
SAP QM qs41 attempts to maintain a code group with a catalog of 3 and reports an error -you need to maintain catalog 3
Integration of sparkstreaming and sparksql
Algorithm question: interview question 32 - I. print binary tree from top to bottom (title + idea + code + comments) sequence traversal time and space 1ms to beat 97.84% of users once AC
Defcampctf2122 Forensics
shell使用数组
What is PDT
H2O brings AI master NLP technology to enterprises