import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
while(sc.hasNextInt()){
int n = sc.nextInt();
if(n==1||n==2)System.out.println(1);
else {
int a = 1 , b = 1 , c = 0;
for(int i =3;i<=n;i++){
c = a + b ;
a = b ;
b = c;
}
System.out.println(c);
}
}
return ;
}
}
m个苹果放在n个盘子中,有f(m,n)种分法,则可以分解为两种可能
1.假设至少有一个盘子是空的,那么就有f(m,n-1)种分法
2.假设没有盘子是空的,则n个盘子都有苹果,本质上跟m-n个苹果分给n个盘子的分法是相同的,即f(m-n,n)
即f(m,n)= f(m,n-1)+f(m-n,n)
在边界情况下,只有一个苹果、没有苹果、只有一个盘子,都只有一种分法;盘子数小于等于0,或者苹果树小于0则返回0;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int m = sc.nextInt();
int n = sc.nextInt();
int res = get(m,n);
System.out.println(res);
}
}
public static int get(int m,int n){
if(m<0||n<=0)return 0;
if(m==1||m==0||n==1)return 1;
return get(m,n-1)+get(m-n,n);
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String s = sc.nextLine();
int res = 0;
for(int i = 2;i< s.length();i++){
char c = s.charAt(i);
res *=16;
if(c>='0'&&c<='9'){
res += c-'0';
}else{
res += c-'A'+10;
}
}
System.out.println(res);
}
}
}
质因子:任何大于1的整数都可以由多个质数相乘而得,如果这个数本身是质数,那么质因子是其本身。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
//任何数只可能有一个质因子大于其开方
long x = (long)Math.sqrt(n);
for(int i = 2;i<=x;i++){
while( n % i == 0){
System.out.print(i + " ");
n /=i;
}
}
//如果n=1则说明n的所有质因子已被找到,否则n则为其最后一个质因子
if(n!=1)System.out.println(n+" ");
}
}
动态规划
从N个人拿出N-K个,求N-K最小值,则是求K最大值,即是求在以A为结尾的升序子串+以A为开头的降序子串的个数和最大。
正序数组求升序
186 186 150 200 160 130 197 200 1 1 1 2 2 1 3 4
逆序数组求升序
200 197 130 160 200 150 186 186 1 1 1 2 3 2 3 3
两者相加
186(4) 186(4) 150(3) 200(5) 160(4) 130(2) 197(4) 200(5)
显然最大和为5,但对于200来说,已经同时作为递增子串的末尾的递减子串的开头,所以应该减去1,为4。最终结果为N-K=4
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
while(sc.hasNextInt()){
int n = sc.nextInt();
//存储身高
int[] a = new int[n];
//反向存储身高
int[] b = new int[n];
//存储最大递增子串
int[] up = new int[n];
//存储反向数组的最大递增子串
int[] reup = new int[n];
for(int i = 0 ;i<n;i++){
int x = sc.nextInt();
a[i] = x;
//反向存储
b[n-i-1] = x;
}
for(int i = 0;i < n ;i++){
up[i]=1;
reup[i]=1;
for(int j = 0;j<i;j++){
/**动态规划,如果当前遍历的学生身高比被比较的学生高,则有两种可能
如数组 1 2 3 8 1 2 4,当前遍历i = 6,j = 5,显然up[6] = 4,up[5]=2,
则最大子串仍是4;
**/
if(a[i]>a[j]){
//感觉用注释这行的写法更好理解一点,但运行比较慢
//if(up[j]==up[i])up[i]++;
up[i] = Math.max(up[j]+1,up[i]);
}
if(b[i]>b[j]){
//if(reup[j]==reup[i])reup[i]++;
reup[i] = Math.max(reup[j]+1,reup[i]);
}
}
}
int max = 1;
for(int i = 0;i < n;i++){
max = Math.max(up[i]+reup[n-i-1],max);
}
System.out.println(n-max+1);
}
}
}