个人Java刷题
题目一:最小公倍数
- 给定你五个正整数,它们最小众倍数是指能够被其中至少三个数整除的最小正整数。
- 给定你各不相同的整型a, b, c, d以及e。请返回它们的最小众倍数。
- 输入:
- 输入五个正整数a,b,c,d,e。 a, b, c, d以及e中每个的取值范围都在1和100之间(其中包括1和100)。
- a, b, c, d以及e各不相同
public class FiveNumber {
public static int least(int a,int b ,int c,int d,int e){
int i=1;
int count=0;
while(i>0){
if(i%a==0) count++;
if(i%b==0) count++;
if(i%c==0) count++;
if(i%d==0) count++;
if(i%e==0) count++;
if (count>=3) break;
count=0;
i++;
}
return i;
}
public static void main(String[] args){
int res=least(1,2,4,5,6);
System.out.println(res);
}
}
题目二:最长公共子串
- 要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的)
- 方法一: 对于较短的那个字符串,假设其长度为n,依次找到它的长度为n, n-1, n-2…1的若干的子串
- 若另外那个较长的字符串包含了较短字符串的某个子串,则找到了二者的最长公共子串。
public class LongestString {
// 求解两个字符号的最长公共子串
public static String maxSubstring(String strOne, String strTwo){
// 参数检查
if(strOne==null || strTwo == null){
return null;
}
if(strOne.equals("") || strTwo.equals("")){
return null;
}
// 二者中较长的字符串
String max = "";
// 二者中较短的字符串
String min = "";
if(strOne.length() < strTwo.length()){
max = strTwo;
min = strOne;
} else{
max = strTwo;
min = strOne;
}
String current = "";
// 遍历较短的字符串,并依次减少短字符串的字符数量,判断长字符是否包含该子串
for(int i=0; i<min.length(); i++){
for(int begin=0, end=min.length()-i; end<=min.length(); begin++, end++){
current = min.substring(begin, end);//包含索引begin,不包含索引end的截取子字符串
if(max.contains(current)){ //检查是否包含子字符串
return current;
}
}
}
return null;
}
public static void main(String[] args) {
String strOne = "abcdefg";
String strTwo = "adefgwgeweg";
String result = LeastString.maxSubstring(strOne, strTwo);
System.out.println(result);
System.out.println(strOne.substring(0,7));
}
}
题目三:输出素数
-
求100以内的素数的个数,并以每行显示5个数的方式打印出来
-
质数(又称素数),是指在大于1的自然数中,除了1和它本身外,不能被其他自然数整除(除0以外)的数。
public class PrimeNumber {
public static boolean isPrime(int i) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) count++;
}
if (count == 2) return true;
else {
return false;
}
}
public static void main(String[] args) {
int row = 0;
for (int i = 1; i <= 100; i++) {
if (isPrime(i)) {
row++;
System.out.printf("%d\t",i);
}
if (row == 5) {
System.out.println("\n");
row = 0;
}
}
}
}
题目四:最长的严格单调数列
-
一个严格递增数列是指每个数都严格比前一个数大的一列数。
-
一个严格递减数列是指每个数都严格比前一个数小的一列数。
-
一个严格单调数列是指严格递增数列或是严格递减数列。例如1, 5, 6, 10 和 9, 8, 7, 1两个数列都是严格单调数列,而 1, 5, 2, 6和 1, 2, 2, 3就不是严格单调数列。
-
给定你一个数列seq,请找到满足严格单调定义的最长连续子数列,并返回其长度
-
输入: 输入一个整形数组seq。seq含有1到50个元素(其中包括1和50),seq中的每个元素的取值范围都在1和100之间(其中包括1和100
-
输出: 返回严格单调的最长连续子序列长度
-
举例:
*seq: {1, 7, 7, 8, 3, 6, 7, 2},返回3.
*最长的严格单调数列是3,6,7.而子数列1,3,6,7是不符合要求的,因为1和3并不相连,此外1,7,7,8也是不满足条件的,因为它不是严格递增的。
public class SList {
public static int LongList(int[] seq){
int res=1;//结果,实时更新
int increasenum=1;//递增数列长度
int decreasenum=1;//递减数列长度
for(int i=0;i<seq.length-1;i++){
if (seq[i+1]>seq[i]){
increasenum++;
decreasenum=1;
}
else if (seq[i+1]<seq[i]){
decreasenum++;
increasenum=1;}
else if (seq[i+1]==seq[i]){
decreasenum=increasenum=1;
}
if (increasenum>res) res=increasenum;
if (decreasenum>res) res=decreasenum;
}
return res;
}
public static void main(String[] args){
int[] seq = {1, 6, 7,8,9,0, 8,4, 3,2,1,0, 6, 7, 9,10};
int result = LongList(seq);
System.out.println(result);
}
}
题目五:圆上格点数
- 给定一个rSquare,表示一个圆的半径的平方。
- 某圆的圆心在坐标系原点,需要计算返回处在该圆圆周上的格点(横纵坐标均为整数的点)数量。
- 输入:输入一个整数rSquare。rSquare的取值范围在1到2,000,000,000之间(其中包括1和2,000,000,000)
- 输出: 返回圆上的格点数量
- 举例: rSquare = 1,返回4. 圆心在原点、半径为1的圆通过4个格点:(1,0)、(0,1)、(-1,0)和(0,-1)
public class rSquare {
/**
* 圆的格点数
*/
public static void main(String[] args) {
int m = countPoints(200);
System.out.println(m);
}
public static int countPoints(int rSquare) {
int res=0;
int i=(int) (Math.sqrt(rSquare));
for(int x=1;x<=i;x++){
double y=Math.sqrt(rSquare-x*x);
int temp = (int) y;
if (y-temp==0){
res++;
System.out.println("x="+x+","+"y="+y);
}
}
return res*4;
}
}
题目六:阿姆斯壮数
/***
- 问题描述:阿姆斯壮数(armstrongnumber) 是等于其数字的立方数之和的数字,例如:0,1,153,370,371,407等
- 程序要求输入一个正整数或者0,检查输入的数是否为阿姆斯壮数。
- 输入格式: 一个正整数n。
- 输出格式:是阿姆斯壮数则输出”Armstrong number”,如不是则输出”Not Armstrong number”。
- 样例输入:153
- 样例输出: Armstrong number
*/
public class ArmStrongNumber {
public static void main(String[] args) {
Fun(407);
Fun(153);
Fun(1);
Fun(0);
Fun(371);
}
public static void Fun (int num) {
if (num == 0 || num == 1) {
System.out.println("Armstrong number");
}
int test = num;
int res = 0;
while (test > 0) {
int temp = test % 10;
test = test / 10;
//System.out.println(test);
res = res + temp * temp * temp;
// System.out.println(res);
}
if (res==num) System.out.println("Armstrong number");
else System.out.println("Not Armstrong number");
}
}
题目七:百元买鸡
/***
- 问题描述:编写程序解决“百钱买百鸡”问题。
- 公鸡五钱一只,母鸡三钱一只,小鸡一钱三只
- 现有百钱欲买百鸡,共有多少种买法?
- (注意,是刚好100块钱买100只鸡,不能多也不能少)
- 结果格式:将结果“公鸡 ? 只母鸡 ? 只小鸡 ? 只”添加到列表List中并返回列表。
*/
public class BuyChicken {
public static void main(String[] args) {
List<String> res = new ArrayList<>();
int x;//公鸡数量
int y ;//母鸡数量
int z;//小鸡数量
for(x=0;x<20;x++){
for(y=0;y<33;y++){
z=100-x-y;
if((z%3==0)&&(5*x+3*y+z/3==100)){
res.add("公鸡"+x+"只,"+"母鸡"+y+"只,"+"小鸡"+z+"只");
}
}
}
System.out.println(res);
System.out.println("共有"+res.size()+"种方法");
}
}
题目八:财务发钱
/***
- 问题描述:对于财务处的工作人员来说,发工资那天是最忙碌的。财务处的NowCoder最近在考虑一个问题:如果每个员工的工资额都知道,最少需要准备多少张人民币,才能在给每位同事发工资的时候都不用找零呢?
- 这里假设员工的工资都是正整数,单位元,人民币一共有100元、50元、20元、10元、5元、2元和1元七种。
- 输入格式:
- 输入数据包含多个测试实例,每个测试实例的第一行是一个整数n (n≤100),表示人数,然后是n个员工的工资
- 输出格式:
- 对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数,每个输出占一行
- 样例输入:
3
1 2 3 - 样例输出:
- 4
*/
public class Payoff {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int total=input.nextInt();//总人数
int res=0;
int[] pmoney=new int[total];
for(int i=0;i<total;i++){
pmoney[i]=input.nextInt();
}
Money(pmoney);
}
/****
* 永远用最大面额的钞票去给钱
* @param money
*/
public static void Money(int[] money) {
int count=0;
for(int i=0;i<money.length;i++) {
while (money[i] != 0) {
if (money[i] >= 100) {
money[i] = money[i] - 100;
count++;
} else if (money[i] >= 50) {
money[i] = money[i] - 50;
count++;
} else if (money[i] >= 20) {
money[i] = money[i] - 20;
count++;
} else if (money[i] >= 10) {
money[i] = money[i] - 10;
count++;
} else if (money[i] >= 5) {
money[i] = money[i] - 5;
count++;
} else if (money[i] >= 2) {
money[i] = money[i] - 2;
count++;
} else if (money[i] == 1) {
money[i] = money[i] - 1;
count++;
}
}
}
System.out.println(count);
}
}
题目九:有效括号
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: “()”
输出: true
示例 2:
输入: “()[]{}”
输出: true
示例 3:
输入: “(]”
输出: false
示例 4:
输入: “([)]”
输出: false
示例 5:
输入: “{[]}”
输出: true
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-parentheses
import org.junit.Test;
import java.util.Stack;
public class Brackets {
public static boolean isValid(String s) {
Stack<Character> left=new Stack<Character> ();
if(s.length()%2!=0) return false;
for(char c:s.toCharArray()){
if(c=='(') left.push(')');
else if(c=='[') left.push(']');
else if(c=='{') left.push('}');
else if(left.isEmpty()|| c!=left.pop() )
return false;//注意此处or两个条件顺序调换后,如果先判断c!=left.pop(),之后left里没东西了,就满足了left.isEmpty()会直接返回错误
}
return left.isEmpty();
}
public static void main(String[] args) {
boolean res=isValid("[()]");
System.out.println(res);
}
@Test
public void fun(){
System.out.println(isValid(""));
}
}
题目十:统计不重复的字符串长度
/***
- 求字符串字母的个数如"ABCCDDEFFG",CDF字母均重复
- 则"ABCDEFG"为非重复的字母,输出7即可
- (思路:遍历一遍HashMap即可,哈希表插入元素时,重复的键会被覆盖)
*/
public class MapTest {
public static int Solution(String s){
Map<Character,Integer> map=new HashMap<Character, Integer>() ;
int count;
for (int i=0;i<s.length(); i++){
char c=s.charAt(i);
//把该字符出现次数也统计进去
if (map.get(c)!=null){
count=map.get(c)+1;
map.put(c,count);
}
else {
map.put(c,1);
}
System.out.println(map);
}
return map.size();
}
public static void main(String[] args) {
System.out.println(Solution("ABCCDDEFFG"));
}
}