浦发银行笔试真题

这篇博客汇总了浦发银行笔试中的编程题目,包括判断素数、闰年、字符串操作、回文子串、IP地址验证等,还涉及约瑟夫环问题和多种排序算法,是编程面试的宝贵参考资料。
摘要由CSDN通过智能技术生成

浦发银行笔试真题

1、判断素数

package com.elijih.banktests10;

import java.util.Scanner;

/**
 * 第一题
 * 判断素数:对于一个大于1的正整数,除了1和该数本身,没有其他因数的数称为素数
 * */
public class test01 {
   
    public static void main(String[] args) {
   
        System.out.print("请输入一个正整数:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        if (isPrime(num)){
   
            System.out.printf("%d是一个素数",num);
        }else{
   
            System.out.printf("%d不是素数",num);
        }
        sc.close();

    }

    public static boolean isPrime(int num){
   
        if(num<2){
   
            //不符合素数的定义
            return false;
        }
        int t = (int)Math.sqrt(num);
        for (int i=2;i<=t;i++){
   
            //当开平方的值小于2,必为素数,因为只有2和3满足条件
            if (num%i == 0){
   
                //当该数能够对i取模余0,说明还有其他因数
                return false;
            }
        }
        return true;
    }
}

2、判断闰年

package com.elijih.banktests10;
/**
 * 第二题:
 * 判断闰年
 * 输出从1990年到2010年之间的闰年
 *
 * 闰年:①、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1901年不是闰年)
 * ②、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
 * */
public class test02 {
   
    public static void main(String[] args) {
   
        for (int year=1990;year<=2010;year++){
   
            if(year%4 == 0 && year%100 != 0){
   
                System.out.printf("%d是闰年\n",year);
                continue;
            }else if (year%100 == 0 && year%400 == 0 ){
   
                System.out.printf("%d是闰年\n",year);
            }
        }
    }
}

3、判断是否为非完全平方数

package com.elijih.banktests10;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * 第三题
 * 判断是否为非完全平方数
 *
 * 非完全平方数:如果一个正整数 a 不是某一个整数 b 的平方,那么这个正整数 a 叫做非完全平方数
 * */
public class test03 {
   
    public static void main(String[] args) {
   
        Scanner sc =new Scanner(System.in);
        System.out.print("请输入一个数:");
        int num = 0;
        try {
   
            num = sc.nextInt();
        } catch (Exception e) {
   
            throw new RuntimeException("输入的不是整数");
        }
        if(!isNumric(num)){
   
            //先判断是不是正整数,再判断是不是完全平方数
            System.out.printf("%d不是一个正整数,更不是非完全平方数",num);
        }else if ((Math.sqrt(num) - (int)Math.sqrt(num)) == 0){
   
            System.out.printf("%d不是一个非完全平方数",num);
        }else{
   
            System.out.printf("%d是一个非完全平方数",num);
        }
        sc.close();
    }

    public static boolean isNumric(int num){
   
        Pattern p = Pattern.compile("[0-9]*");
        return p.matcher(Integer.toString(num)).matches();
    }

}

4、异位词

package com.elijih.banktests10;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 第四题:
 * 判断两个字符串是否是异位,比如abcn和banc是一对,anc和nac是一对,两个字符串完全奇偶互换,则称为异位,
 * 判断两个字符串是否为异位词,意思是判断两个字符串有相同数量的字母,再对两个字符串进行排序,排序后相等则为异位词
 * */
public class test04 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        String str1 = sc.nextLine();
        System.out.print("请输入第二个字符串:");
        String str2 = sc.nextLine();

        if (isAnagram(str1,str2)){
   
            System.out.printf("%s和%s是一对异序字符串",str1,str2);
        }else {
   
            System.out.printf("%s和%s不是一对异序字符串",str1,str2);
        }

    }
    public static boolean isAnagram(String str1,String str2){
   
        if(str1.length() != str2.length()){
   
            return false;
        }
        char[] c1 = str1.toCharArray();
        char[] c2 = str2.toCharArray();
        Arrays.sort(c1);
        Arrays.sort(c2);
        return Arrays.equals(c1,c2);
    }
}

5、字符替换

package com.elijih.banktests10;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 第五题
 * 字符串中字符替换,把字符串中的a和A换成c输出
 * */
public class test05 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个字符串:");
        String s = sc.nextLine();
        char[] t = s.toCharArray();
        for (int i=0;i<t.length;i++){
   
            if (t[i] == 'a' || t[i] == 'A'){
   
                t[i]='c';
            }
        }
        String str = new String(t);
        System.out.printf("转换后的字符串是%s",str);
        sc.close();
    }
}

6、求字符串所有子串

package com.elijih.banktests10;
/**
 * 第六题
 * 求字符串的所有子串
 * */
public class test06 {
   
    public static void main(String[] args) {
   
        String str = "abbcc";
        System.out.printf("原字符串为:%s\n",str);
        int count = 0;

        for (int i=0;i<str.length();i++){
   
            for(int j=i+1;j<=str.length();j++){
   
                count++;
                System.out.printf("第%d个子串为:%s\n",count,str.substring(i,j));
            }
        }
    }
}

7、字符串大小写转换

package com.elijih.banktests10;
/**
 * 第七题
 * 字符串大小写转换
 * */
public class test07 {
   
    public static void main(String[] args) {
   
        String str = "AFSGsbf";

        //大写转小写
        System.out.println(str.toLowerCase());

        //小写转大写
        System.out.println(str.toUpperCase());
    }
}

8、输出字符串中仅出现一次的第一个字符

package com.elijih.banktests10;

import java.util.HashMap;

/**
 * 第八题
 * 输出字符串中仅出现一次的第一个字符
 *
 * 思路:哈希表
 * */
public class test08 {
   
    public static void main(String[] args) {
   
        String str = "affasf";

        HashMap<Character,Integer> hashMap = new HashMap<>();

        //遍历字符串,把每个字符存储在哈希表里
        for (int i=0;i<str.length();i++){
   
            if (hashMap.containsKey(str.charAt(i))){
   
                //如果包含有当前字符,value+1
                int value = hashMap.get(str.charAt(i));
                hashMap.put(str.charAt(i),value+1);
            }else{
   
                //不包含,value置为1
                hashMap.put(str.charAt(i),1);
            }
        }

        //再次遍历字符串,看每个字符出现的次数
        for(int i=0;i<str.length();i++){
   
            if(hashMap.get(str.charAt(i)) == 1){
   
                System.out.println(str.charAt(i));
                break;
            }
        }

    }
}

9、两个字符串,在第一个字符串中出现,第二个中未出现的字符串

package com.elijih.banktests10;

impor
  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值