一.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
🔺思路:将分子分母分开来看,分子依次是2,3,5,8,13,21,除开第一项和第二项之外,其他的每一项都与前两项有直接的关系(前两项求和),所以明显是递归算法的特征,分母也是一样,所以该题通过递归方法求解。
public class QiuHe {
public static void main(String[] args){
float total=0;
for(int i=0;i<20;i++){
float top= getTop(i+1);
float bottom=getBottom(i+1);
total+=top/bottom;//每一项的值
}
System.out.println(total);
}
//获取分子核心递归方法
public static float getTop(int position){
if(position==1){
return (float)2;
}
if(position==2){
return (float) 3;
}
return getTop(position-1)+getTop(position-2);
}
//获取分母核心递归方法
public static float getBottom(int position){
if(position==1){
return (float) 1;
}
if(position==2){
return (float)2;
}
return getBottom(position-1)+getBottom(position-2);
}
二.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
🔺思路:首先应先找到最大值与最小值,并记录它们的位置,然后与第一个和最后一个进行交换,最后输出结果。
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int t;
int max=0 , min = 0;//初始化最大值最小值为0;
int m, n;//m,n记录最大值最小值的位置
Scanner input = new Scanner(System.in);//创建input对象
int[] a = new int[5];//声明并创建了一个一维数组a,并分配了5个元素
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
max = a[0];//将第一个值定义为最大值
m = 0;
min = a[0];//将第一个值定义为最小值
n = 0;
if (max <= a[i])
max = a[i]; //取出最大值
if (min >= a[i])
min = a[i];//取出最小值
}
for (int i = 0; i < a.length; i++) {
if (max == a[i]) {
m = i; //获取最大值位置
}
if (min == a[i]) {
n = i;//获取最小值位置
}//将最大值与第一个元素互换,最小值与最后一个元素互换
t = a[0];
a[0] = max;
max = t;
t = a[a.length - 1];
a[a.length - 1] = min;
min = t;
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);//输出数组
}
}
三.Java编程之有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子
🔺思路:假设一共有n个人,则定义一个长度为n的boolean数组。全部元素的值为true。报数为3的元素其值为false。一直循环下去,直到只剩下一个值为true的元素。则该原色的下标加一就是最后剩下的人的号码。
public class Test19CallNumber {
public static void main(String[] args) {
boolean[] persons = doCall(10);
for (int i = 0; i < persons.length; i++) {
if (persons[i]) {
System.out.println("最后留下的是:"+(i+1)+"号。");
}
}
}
public static boolean[] doCall(int person) {
boolean[] persons = new boolean[person];
int number = person, key = 0;
for (int i = 0; i < person; i++)
persons[i] = true;
while (number != 1) {
for (int i = 0; i < person; i++) {
if (!persons[i]) {
continue;
} else {
key++;
if (key % 3 == 0) {
System.out.println("编号为:"+(i+1)+"的人退出。");
persons[i] = false;
key = 0;
}
}
}
number = 0;
for (int i = 0; i < person; i++) {
if (persons[i]) {
number++;
}
}
}
return persons;
}
四.一个偶数总能表示为两个素数之和。
🔺思路:用一个函数判断是否是素数,确定两个数相加之后等于输入的那个数,再分别判断两个数是不是素数,如果都是素数,则可以输出
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int i,j;
for(i=2;i<num;i++){
if(isPrime(i)){
j=num-i;
if(isPrime(j)){
System.out.println(num + " = " + i + " + " + j);
}
}
}
}
public static boolean isPrime(int n){
for(int i=2;i<Math.sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
五.计算字符串中子串出现的次数
public static void main(String[] args) {
System.out.println("请输入父串");
Scanner sc =new Scanner(System.in);
String father = sc.next();
System.out.println("请输入子串");
String son = sc.next();
int count=0;
while(son.length()<=father.length()) {
if(father.length()!=-1) {
// int index = father.indexof(son)可以简写为father.indexof(son)
//找到子串出现的地方,使count++,然后截取子串后面的部分再进行查找
father=father.substring(father.indexOf(son)+son.length(), father.length());
count++;
}else {
break;
}
}
System.out.println("子串在父串出现的次数是"+count+"次");
}