2018级《算法分析与设计》练习4(java)

问题 A: 2的个数

题目描述

请编写一个程序,输出0到n(包括n)中数字2出现了几次。

输入

输入一个正整数n。

输出

输出0到n的数字中2出现了几次。

样例输入 Copy

2
10
20

样例输出 Copy

1
1
3

import java.util.*;
public class Main {
  
           //判断一个数含有二的个数
         public static int m(int n){
         int temp=0;
         while(n>0){
         if(n%10==2) temp++;
         n/=10;
         }
         return temp;
         }
           //循环记录1~n的数字里面含有二的个数
         public static int sum(int n) {
         int count=0;
         for (int i = 0; i <= n; i++) {
         count+=m(i);
         }
         return count;
           
         }
           
  
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
         
        while(cin.hasNext()){
         int n=cin.nextInt();
        System.out.println(Main.sum(n));
            
        }
         
          
    }
}

问题 B: 互异字符串

题目描述

请实现一个算法,确定一个字符串的所有字符是否全都不同。

给定一个字符串,请返回一个True代表所有字符全都不同,False代表存在相同的字符。

输入

输入一个字符串。保证字符串中的字符为ASCII字符。字符串的长度小于等于3000。

输出

如果所有字符全都不同输出“True”,如果存在相同的字符则输出“False”。

样例输入 Copy

aeiou
BarackObama

import java.util.*;
public class Main {
      
  
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            String a=cin.nextLine();
           char c;
            boolean flag=true;
            for(int i=0;i<a.length();i++){
                c=a.charAt(i);
                for(int j=i+1;j<a.length();j++){
                   if(a.charAt(j)==c){
                       flag= false;
                   }else{
                      continue;
                  }
                     
                }
            }  
            if(flag==true) {
                System.out.println("True");
            }else {
                System.out.println("False");
            }
              
  
    
            }
        }
         
          
    }

样例输出 Copy

True
False

问题 C: 乒乓球筐

题目描述

Kimi有两盒(A、B)乒乓球,有红双喜的、有亚力亚的……现在他需要判别A盒是否包含了B盒中所有的种类,并且每种球的数量不少于B盒中的数量,该怎么办呢?

输入

输入有多组数据。
每组数据包含两个字符串A、B,代表A盒与B盒中的乒乓球,每个乒乓球用一个大写字母表示,即相同类型的乒乓球为相同的大写字母。
字符串长度不大于1000。

输出

每一组输入对应一行输出:如果B盒中所有球的类型在A中都有,并且每种球的数量都不大于A,则输出“Yes”;否则输出“No”。

样例输入 Copy

ABCDFYE CDE
ABCDGEAS CDECDE

样例输出 Copy

Yes
No

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String A = scanner.next();
            String B = scanner.next();
 
            int arrA[] = new int[26];
            int arrB[] = new int[26];
 
            for (int i = 0; i < A.length(); i++) {
                arrA[A.charAt(i) - 'A']++;
            }
 
            for (int i = 0; i < B.length(); i++) {
                arrB[B.charAt(i) - 'A']++;
            }
 
            boolean flag = true;
            for (int i = 0; i < 26; i++) {
                if (arrA[i] < arrB[i]) {
                    flag = false;
                    break;
                }
            }
 
            if (flag) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}

问题 D: 最难的问题

题目描述

Kimi生活在充满危险和阴谋的年代。为了生存,他首次发明了密码,用于军队的消息传递。假设你是军团中的一名军官,需要把发送来的消息破译出来、并提供给你的将军。
消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A 都分别替换成字母F),其他字符不变,并且消息原文的所有字母都是大写的。密码中的字母与原文中的字母对应关系如下。
密码字母:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
原文字母:V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

输入

输入包括多组数据,每组数据一行,为收到的密文。
密文仅有空格和大写字母组成。(长度<=1000)

输出

对应每一组数据,输出解密后的明文。

样例输入 Copy

HELLO WORLD
SNHJ

样例输出 Copy

CZGGJ RJMGY
NICE

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String a = sc.nextLine();
            char[] b = a.toCharArray();
            for(int i = 0;i< b.length;i++){
                char c = b[i];
                if(c-'A'>=0){
                    c =(char)( c >'E'? (c-5 ): (c+21));
                    b[i] = c;
                }
            }
            System.out.println(new String(b));
        }
    }
}
 

问题 E: 线性搜索

题目描述

请编写一个程序,输入包含n(n<=10000)个整数的数列S以及包含q个(q<=500)不重复整数的数列T,输出既包含于T也包含于S的整数的个数C。S、T中的元素均大于0且小于109,T的元素不重复。

输入

第一行输入n,第二行输入代表S的n个整数,第三行输入q,第四行输入代表T的q个整数。

输出

用1行输出C。

样例输入 Copy

5
1 2 3 4 5
3
3 4 1

样例输出 Copy

3

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int count=0;
            int n=sc.nextInt();
            int S[]=new int[n];
            for(int i=0;i<n;i++) {
                S[i]=sc.nextInt();
            }
            int q=sc.nextInt();
            int T[]=new int[q];
            for(int i=0;i<q;i++) {
                T[i]=sc.nextInt();
            }
            for(int i=0;i<n;i++) {
                for(int j=0;j<q;j++) {
                    if(S[i]==T[j]) {
                        count++;
                    }
                }
            }
            System.out.println(count);
        }
    }
}

问题 F: 二分搜索(递归)

题目描述

使用递归算法,实现二分搜索。

输入

多组数据输入,每组第一个数字为数组的长度n,然后输入n个整数,最后输入待查询的值。

输出

输出待查询值所在的位置,如果没有找到,则返回-1。

样例输入 Copy

3 1 2 3 2
4 0 1 3 4 2

样例输出 Copy

2
-1

二分查找前提是要数组有序

import java.util.Scanner;
public class Main {
    public  static  int binarySearch(int a[], int key, int n) {
        int low = 0;
        int high = n - 1;
        return binarySearch(a, key, low, high);
    }
    public  static  int binarySearch(int a[], int key, int low, int high) {
        if (low > high)
            return -1;
        int mid = (low + high) / 2;
        if (key == a[mid])
            return mid+1;
        else if (key < a[mid])
            return binarySearch(a, key, low, mid - 1);
        else
            return binarySearch(a, key, mid + 1, high);
    }



    public static void main(String[] args) {

        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()) {
            int n = cin.nextInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++){
                arr[i]=cin.nextInt();
            }
            int k=cin.nextInt();
            int res=binarySearch(arr,k,n);
            System.out.println(res);
        }
    }



}






问题 G: 二分搜索(非递归)

题目描述

使用非递归算法,实现二分搜索。

输入

多组数据输入,每组第一个数字为数组的长度n,然后输入n个整数,最后输入待查询的值。

输出

输出待查询值所在的位置,如果没有找到,则返回-1。

样例输入 Copy

3 1 2 3 2
4 0 1 3 4 2

样例输出 Copy

2
-1

二分查找前提是要数组有序

import java.util.Scanner;
public class Main {
    public  static int binarySearch(int[] arr, int k, int n) {

        int low=0,high=arr.length;
        int mid;
        while(low<=high){
            mid=(low+high)/2;
            if(arr[mid]==k)return mid+1;
            else if(k<arr[mid]){
                high=mid-1;
            }
            else{
                low=mid+1;
            }

        }
        return -1;
    }

    public static void main(String[] args) {

        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()) {
            int n = cin.nextInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++){
                arr[i]=cin.nextInt();
            }
            int k=cin.nextInt();
            int res=binarySearch(arr,k,n);
            System.out.println(res);
        }
    }



}






问题 H: 汉诺塔-3

题目描述

用1,2,…,n表示n个盘子,称为1号盘,2号盘,…。号数大盘子就大。经典的汉诺塔问题经常作为一个递归的经典例题存在。可能有人并不知道汉诺塔问题的典故。汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一回只能移动一个圆盘。我们知道最少需要移动2^64-1次.在移动过程中发现,有的圆盘移动次数多,有的少 。 告之盘子总数和盘号,计算该盘子的移动次数.

输入

包含多组数据,首先输入T,表示有T组数据.每个数据一行,是盘子的数目N(1<=N<=60)和盘号k(1<=k<=N)。

输出

对于每组数据,输出一个数,到达目标时k号盘需要的最少移动数。

样例输入 Copy

2
60 1
3 1

样例输出 Copy

576460752303423488
4

import java.util.Scanner;
public class Main {
    public static long  f(int n,int m)
    {
        if(n==m)
            return 1;
        else
            return  2*f(n,m+1);
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T=sc.nextInt();
        while(T>0) {
            int n=sc.nextInt();
            int k=sc.nextInt();
            long count=f(n,k);
            System.out.println(count);
            T--;
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_45353823/article/details/106261333