当前位置:网站首页>Email: analysis of wrong arrangement
Email: analysis of wrong arrangement
2022-06-10 04:31:00 【little-peter】
Raise questions : one day , Five people each received a letter , Everyone has a mailbox in front of their home , But the messenger just delivered everyone's letter to someone else's mailbox when delivering the letter , ask : How many delivery schemes are there to meet this requirement ?
- To analyze problems
When n A number element is placed in n A number position , Remember the number of staggered methods D(n)~
⒈ The first n Put two elements in one place , For example, location k, Altogether (n-1) Methods ;
⒉ Put number as k The elements of , There are two scenarios :
1° Put it in place n, that , For the rest (n-1) Elements , Due to the first k An element is placed in position n, be left over (n-2) One element has D(n-2) Methods ;
2° The first k An element doesn't put it in position n, At this time , For this (n-1) Elements , Yes D(n-1) Methods ;.
So there is :
D(n) = (n-1) [D(n-2) + D(n-1)]
- solve the problem

Let's take the same question of sending emails as an example , Based on the above analysis , We can get the following code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
long sum = count(n);
System.out.println(sum);
}
}
// Calculate the situation that everyone can't receive their own email : Staggered algorithm
private static long count(int n) {
if (n == 1) {
return 0;
} else if (n == 2) {
return 1;
} else {
return (n - 1) * (count(n - 1) + count(n - 2));
}
}
}
边栏推荐
- 在MindSpore官网容器内安装MindInsight不能在本地正常工作
- Metersphere | a super easy-to-use open source testing platform
- .NET C#基礎(7):接口 - 人如何和猫互動
- Why do SQL statements use indexes but still slow queries?
- . Net C Foundation (7): interface - how people interact with cats
- [in depth study of 4g/5g/6g topic -31]: 5g NR startup process 5.2 - ue attach process (registration request/accept/complete)
- [Error] anonymous type with no linkage used to declare function ‘bool InitSLinkList
- 深度学习与CV教程(13) | 目标检测 (SSD,YOLO系列)
- What are the advantages of multi merchant mall applet source code?
- Jenkinsclient | easy to use Jenkins client
猜你喜欢
![[science and technology specialty-1]: overview and advantages of science and Technology Specialty Students](/img/f6/77fda73053ed4afefc8277e196c1a9.png)
[science and technology specialty-1]: overview and advantages of science and Technology Specialty Students

超好用的 Chrome 插件!

Detailed explanation of tcp/ip protocol mechanism

多商户商城小程序源码有何优势?

Why do SQL statements use indexes but still slow queries?

idea中怎样关联查看Scala源代码

使用MindSpore在GPU-PYNATIVE/ CPU-GRAPH_MODE 与 GPU-GRAPH_MODE 执行不一致

Celery | 任务队列神器
![[laser principle and application-1]: what is a laser and its common applications](/img/90/d463b762e546154a6427404fa0de8d.jpg)
[laser principle and application-1]: what is a laser and its common applications

Puzzling color deviation of unity illumination black
随机推荐
[in depth study of 4g/5g/6g topic -28]: 5g NR startup process 4.6 - msg5 (rrcsetupcomplete) message scheduling
如何用天气预警API接口进行快速开发
[in depth study of 4g/5g/6g topic -30]: 5g NR startup process 5.1 - connection between the base station and the core network (ng setup request, ng setup response) and message details.
在MindSpore官网容器内安装MindInsight不能在本地正常工作
tensorflow 中的 cross_entropy
The relationship between libc, glibc and glib
[learn FPGA programming from scratch -13]: quick start chapter - operation step 3 (functional simulation) -2-mentor HDL simulation tool introduction to Modelsim tool and basic principle of functional
Zero basic network: command line (CLI) debugging firewall practice
[机缘参悟-16]:金字塔层次结构符合天道自然
Who ate IO?
.NET C#基础(7):接口 - 人如何和猫互动
[in depth study of 4g/5g/6g topic -29]: 5g NR startup process 5.1 - NR network architecture, core network configuration of base stations
What are the advantages of multi merchant mall applet source code?
JMeter testing TCP million connections
[android l]seandrod security enhancement background summary and impact
Jenkinsclient | easy to use Jenkins client
5分钟学会本地Pypi源搭建
mindspore1.6conda安装gpu版本验证失败
[science and technology specialty-1]: overview and advantages of science and Technology Specialty Students
Unity光照黑莫名其妙的偏色问题