其他题目1

爬楼梯 异味词 百钱买白鸡问题 凯撒密码

60.爬一个或者两个台阶,输入 1 <= n < 90 的数字为台阶数,以输入 0 作为
结束标志,输出 n 个台阶共有多少种上楼方式60.爬一个或者两个台阶,输入 1 <= n < 90 的数字为台阶数,以输入 0 作为结束标志,输出 n 个台阶共有多少种上楼方式
import java.util.Arrays;
import java.util.Scanner;
public class hi {
public static int solution(int n){
int dp[]=new int[n+1];
dp[0]=1;
dp[1]=1;
for(int i=2;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
System.out.print(dp[n]);
return dp[n];
}
public static void main(String arg[]){
int n=3;
solution(n);
}
}

57.分段函数实现问题
模拟一下即可
58.凯撒密码(caesar)是最早的代换密码,对称密码的一种,求加密解密算法
算法:将每个字母用字母表中它之后的第 k 个字母(称作位移值)替代
判断是大写还是小写这里面定了两个字符数组,一个方大写字母一个放小写字母
可以利用哈希数组进行实现
result=result+lowerDict[((lowerDict.index(ch)+offset)%26)]
offset是一个偏转量 %26是为了可以还是保持一个字母不至于变成特殊字符
import java.util.*;
public class solution{
public static void solution(String s,int k)
{
Map<Character,Integer> map1=new HashMap<Character,Integer>();
Map<Character,Integer> map2=new HashMap<Character,Integer>();
char[] c=s.toCharArray();
char[] ans=new char[s.length()];
char[] A=new char[26];
char[] a=new char[26];
for(int i=0;i<26;i++)
{
a[i]=(char)('a'+i);
map1.put((char)('a'+i), i);
}
for(int i=0;i<26;i++)
{
A[i]=(char)('A'+i);
map2.put((char)('A'+i), i);
}
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)<='Z'&&s.charAt(i)>='A')
ans[i]=A[(map2.get(s.charAt(i))+k)%26];
}
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)<='z'&&s.charAt(i)>='a')
ans[i]=a[(map1.get(s.charAt(i))+k)%26];
}
for(int i=0;i<ans.length;i++)
System.out.print(ans[i]);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
solution(s,3);
}
}
59.小球从 100 米下落,每次回弹一半距离,求第 n 次落地后的总距离。
import java.util.Arrays;
import java.util.Scanner;
public class hi {
public static double solution(int n){
double h=100;
double h1=100;
for(int i=0;i<n;i++){
h=h+h1/2;
h1=h1/2;
}
System.out.print(h);
return h;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("請輸入數字n");
int n = sc.nextInt();
solution(n);
}
}

56.排序算法:冒泡排序、快速排序、选择排序等
冒泡排序
public class hi {
/**
* 设置一个标志,如果这一趟发生了交换,则为true,否则为false。明显如果有一趟没有发生交换,说明排序已经完成。
* @param a
* @param n
*/
public static void bubbleSort3(int [] a, int n){
int j, k = n;
boolean flag = true;//发生了交换就为true, 没发生就为false,第一次判断时必须标志位true。
while (flag){
flag=false;//每次开始排序前,都设置flag为未排序过
for(j=1; j<k; j++){
if(a[j-1] > a[j]){//前面的数字大于后面的数字就交换
//交换a[j-1]和a[j]
int temp;
temp = a[j-1];
a[j-1] = a[j];
a[j]=temp;

                //表示交换过数据;
                flag = true;
            }
        }
        k--;//减小一次排序的尾边界
    }//end while
}//end
public static void main(String arg[]) 
{
    int[] arr = {1,1,2,0,9,3,12,7,8,3,4,4,65,22,21,22,33,44,55,66};
    bubbleSort3(arr, arr.length);
    for(int i:arr){
        System.out.print(i+",");
    };
}

}
快速排序
public class hi {
public static void main(String[] args) {
int[] arr = new int[] {9,4,6,8,3,10,4,6};
quickSort(arr,0,arr.length - 1);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+",");
};
}
public static void quickSort(int[] arr,int low,int high) {
int p,i,j,temp;

if(low >= high) {
    return;
}
//p就是基准数,这里就是每个数组的第一个
p = arr[low];
i = low;
j = high;
while(i < j) {
    //右边当发现小于p的值时停止循环
    while(arr[j] >= p && i < j) {
        j--;
    }

    //这里一定是右边开始,上下这两个循环不能调换(下面有解析,可以先想想)    

    //左边当发现大于p的值时停止循环
    while(arr[i] <= p && i < j) {
        i++;
    }

        temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
}
arr[low] = arr[i];//这里的arr[i]一定是停小于p的,经过i、j交换后i处的值一定是小于p的(j先走)
arr[i] = p; 
quickSort(arr,low,j-1);  //对左边快排
quickSort(arr,j+1,high); //对右边快排
}

}
选择排序
import java.util.Arrays;

public class hi {

public static void selectSort(int[] a) {
    if (a == null || a.length <= 0) {
        return;
    }
    for (int i = 0; i < a.length; i++) {
        int temp = a[i];
        int flag = i; // 将当前下标定义为最小值下标
        for (int j = i + 1; j < a.length; j++) {
            if (a[j] < temp) {// a[j] < temp 从小到大排序;a[j] > temp 从大到小排序
                temp = a[j];
                flag = j; // 如果有小于当前最小值的关键字将此关键字的下标赋值给flag
            }
        }
        if (flag != i) {
            a[flag] = a[i];
            a[i] = temp;
        }
    }
}

public static void main(String[] args) {
    int a[] = { 49,38,65,97,76,13,27,49 };
    selectSort(a);
    System.out.println(Arrays.toString(a));
}

}

  1. 判断素数:除了 1 和该数本身,没有其他可以整除的数
    public class Holldworld{
    public static void reverse(int x) {

     for(int i=1; i<x;i++){
         if(x%i==0){
             System.out.print("no");
             break;
         }
         else{System.out.print("yes"); }
     }

    }
    public static void main(String args[])
    {

     int x=2021;
     reverse(x);

    }

}
2. 判断闰年:输出从 1990 年到 2010 年之间的闰年
public class Holldworld{
public static void isyear(int year)
{
boolean b1=(year%4==0);
boolean b2=(year%100!=0);
boolean b3=(year%400==0);
if((b1&&b2)||b3){
System.out .print(year+"是闰年");
System.out .print('\n');
}
else{
System.out.print(year+"不是闰年");
System.out .print('\n');
}
}
public static void main(String arg[])
{
for(int i=1999;i<=2010;i++)
{
isyear(i);
}
}
}

  1. 判断是否为非完全平方数
    public class Holldworld{
    public static void issqrtnum(int x){
     if((int) Math.sqrt(x) == Math.sqrt(x))
     {
         System.out .print(x+"是一个完全平方数"+'\n');
     }
     else{System.out .print(x+"不是一个完全平方数"+'\n');}
    }
    public static void main(String arg[]){
     for(int i=1;i<100;i++)
     {
         issqrtnum(i);
     }
    }
    }

65.百钱买百鸡:公鸡 5 文钱一只,母鸡 3 文钱一只,小鸡 3 只一文钱,用 100
文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要
买多少只刚好凑足 100 文钱
public class Holldworld{
public static void issqrtnum(int x){
for(int i=0;i<=20;i++)
{
for(int j=0;j<=33;j++)
{
int z = 100-i-j;
if((5i+3j+z/3)==100 && (i+j+z)==100)
{System.out .print("i,j,z"+i+" "+j+" "+z+" "+"\n");}
}
}
System.out .print("无解");
}
public static void main(String arg[]){
issqrtnum(1);
}
}
(dp问题)69.有一个超级细菌,它每秒增殖出一个小细菌。每个小细菌经过生长,从第四
秒开始,每秒也生一个小细菌。请编程实现在第 n 秒的时候,共有多少个细
菌?假设超级细菌不会死亡或者消失
import java.util.Arrays;
import java.util.Scanner;
public class hi {
public static int solution(int n){
int dp[]=new int[n+1];
dp[1]=1;
dp[2]=2;
dp[3]=3;
dp[4]=4;
for(int i=5;i<=n;i++){
dp[i]=dp[i-1]+dp[i-3];
}
System.out.print(dp[n]);
return dp[n];
}
public static void main(String arg[]){
int n=10;
solution(n);
}
}
前三秒刚刚产生的细菌也开始繁殖了

  1. 判断两个字符串是否是异位:比如 abcn 和 banc 是一对,anc 和 nac 是一对,
    两个字符串完全奇偶互换,则称为异位,判断两个字符串是否为异位词,意
    思是判断两个字符串有相同数量的字母
    import java.util.Arrays;
    import java.util.Scanner;

public class Holldworld{
public static void solution(String s,String t)
{
if(s.length()!=t.length()){System.out .print("no");}
char[] s1=s.toCharArray();
char[] t1=s.toCharArray();
Arrays.sort(s1);
Arrays.sort(t1);
System.out.print(t1);
System.out.print(s1);
if(Arrays.equals(t1, s1)){System.out.print("yes");}
else{System.out .print("no");}

}
public static void main(String args[]){
    solution("abc","bac");
}

}
5. 字符串中字符替换:把字符串中的字符 a 和 A 换成 c 输出
import java.util.Arrays;
import java.util.Scanner;

public class Holldworld{
public static void solution(String s)
{
char [] s1=s.toCharArray();
for(int i=0;i<s1.length;i++){
if(s1[i]=='a'||s1[i]=='A'){System.out.print('c');}
else {System.out.print(s1[i]);}
}
}
public static void main(String args[]){
solution("abcdsAads");
}
}
63.一元钱买一瓶汽水,两个空汽水瓶换一瓶汽水,给定钱的金额,问能喝多少
钱数,问能喝多少瓶汽水(不能借)
import java.util.*;
public class solution{
public static int solution(int i,int j,int k)
{
j=i+j;
k=i+k;
if(j/2+k/3==0)
{
return i;
}
else return i+solution(j/2+k/3,j%2,k%3);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int n= Integer.parseInt(s);
solution(n,0,0);
System.out.print(solution(n,0,0));
}
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务