买苹果、删除公共字符串

1.属于黑盒测试的方法是:边界值分析
2.java语言的下面几种数组复制方法中,哪个效率最高?B

A.for 循环逐一复制
B.System.arraycopy
C.Array.copyOf
D.使用clone方法

3.买苹果
小易去附近的商店买苹果,奸诈的商贩使用了捆绑交易,只提供6个每袋和8个每袋的包装(包装不可拆分)。 可是小易现在只想购买恰好n个苹果,小易想购买尽量少的袋数方便携带。如果不能购买恰好n个苹果,小易将不会购买。

public class Test4 {
    public static int Solution(int num){
        int a = num/8;
        int b = num%8;
        if(num%2!=0){
            return -1;
        }else {
           if(b==0){
               return a;
           }else {
               return a+1;
           }
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int result = Solution(num);
        System.out.println(result);
    }
}
//方法二:
import java.util.*;
public class Main{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();
            System.out.println(count(n));
        }
    }
    public static int count(int n){
//一定是偶数(6,8都是),最小是6,10以上偶数都可以;
        if(n%2!=0||n==10||n<6)
            return -1;
//如果拿八个拿完最好
        if(n%8==0)
            return n/8;
//对于10以上的偶数,只要对8取余数不为0,就要从前面的1或者2个8中拿出2个,
//把余数补为6(本来余数就是6,就不用拿)。所以+1;
        return 1+n/8;
    }
}

4.删除公共字符串
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

public class Test5 {
    public static String Solution(String s1,String s2){
      StringBuilder s = new StringBuilder(s1);
      String result = s1;
      for(int i = 0;i<s2.length();i++){
          for(int j = 0;j<result.length();j++){
              if(s2.charAt(i)==result.charAt(j)){
                  s = s.deleteCharAt(j);
                  result = new String(s);
              }
          }
      }
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();
        String result = Solution(s1,s2);
        System.out.println(result);
    }
}
//方法二
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[] ch = sc.nextLine().toCharArray();
            String str = sc.nextLine();
            for(int i=0;i<ch.length;i++){
                if(!str.contains(String.valueOf(ch[i]))){
                    System.out.print(ch[i]);
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值