我是靠谱客的博主 俏皮导师,这篇文章主要介绍超级基础的编程题50道,现在分享给大家,希望可以做个参考。

题目1:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21…

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package jichu50; import java.util.Scanner; public class number1 { public static void main(String[] args) { System.out.println("hello world"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); if(n<1) System.out.println("invalid input!"); int res=coutRabbits(n); System.out.println("第"+n+"个月"+"兔子的数量:"+res); } public static int coutRabbits(int n){ if(n==1||n==2) return 1; int a=1; int b=1; int f=0; for(int i=3;i<=n;i++){ f=a+b; a=b; b=f; } return f; } }

题目2:判断101-200之间有多少个素数,并输出所有素数。

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package jichu50; import java.util.Scanner; public class number2 { public static void main(String[] args) { System.out.println("请输入两个数,m和n,要求n>=m>0"); Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); sc.close(); System.out.println("输出"+m+"-"+n+"之间的素数个数:"); int count=0; for(int i=m;i<=n;i++){ if(isPrime(i)) count++; } System.out.println(count); } public static boolean isPrime(int n){ if(n<=1) return false; for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ return false; } } return true; } }

题目3:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package jichu50; import java.util.Scanner; public class number3 { public static void main(String[] args) { System.out.println("请输入一个三位数:"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.close(); if(n<100||n>999) System.out.println("n不是合法的三位数!"); else { boolean res = isLotus(n); if (res) System.out.println(n + "是水仙花数"); else System.out.println(n + "不是水仙花数"); } } public static boolean isLotus(int n){ boolean flag=false; int a=n/100; int b=n%100/10; int c=n%10; //if(a*a*a+b*b*b+c*c*c==n) if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==n) flag=true; return flag; } }

题目4:将一个正整数分解质因数。例如:输入90,打印出90=233*5。

程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n!=k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package jichu50; import java.util.Scanner; public class number4 { public static void main(String[] args) { System.out.println("请输入一个正整数"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.close(); if(n<=1) System.out.println("输入的数字有误"); else decompose(n); } public static void decompose(int n){ System.out.print(n+"= "); for(int i=2;i<=n;i++){ while (n%i==0&&n!=i){ System.out.print(i+"*"); n=n/i; } if(n==i){ System.out.print(i); break; } } } }

题目5:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

程序分析:(a>b)?a:b这是条件运算符的基本例子。

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package jichu50; import java.util.Scanner; public class number5 { public static void main(String[] args) { System.out.println("请输入一个0-100之间的数"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.close(); if(n<0||n>100) System.out.println("输入数字不正确!"); else { System.out.println(n+"分的等级是"+grade(n)); } } public static String grade(int n){ String res=""; res=n>=90?"A":((n>=60)?"B":"C"); return res; } }

网上找的关于华为面试代码:

题目一:是给定两个无序数组,不用set的情况下求二者的交集。

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package huaweiInterview; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; /* 求两个数组的交集,且不能重复 */ public class number1 { public static void main(String[] args) { int[] res1=intersection1(new int[]{1,2,3,4},new int[]{2,3,3,4,5}); int[] res2=intersection2(new int[]{1,2,3,4},new int[]{2,3,3,4,5}); for(int i:res1){ System.out.print(i+" "); } System.out.println(); for(int i:res2){ System.out.print(i+" "); } } //使用set的解法 public static int[] intersection1(int[] num1,int[] num2){ if(num1==null||num2==null){ return null; } HashSet<Integer> set1=new HashSet<>(); HashSet<Integer> set2=new HashSet<>(); for(int i:num1){ set1.add(i); } for(int j:num2){ if(set1.contains(j)){ set2.add(j); } } int[] res=new int[set2.size()]; int index=0; for(int i:set2){ res[index++]=i; } return res; } //不适用set的解法 public static int[] intersection2(int[] num1,int[] num2){ if(num1==null||num2==null){ return null; } Arrays.sort(num1); Arrays.sort(num2); int i=0,j=0; ArrayList<Integer> list=new ArrayList<>(); while (i<num1.length&&j<num2.length){ if(num1[i]<num2[j]){ i++; }else if(num1[i]>num2[j]){ j++; }else { if(list.isEmpty()||list.get(list.size()-1)!=num1[i]){//容器为空或者和之前的不等 list.add(num1[i]); } i++; j++; } } int[] res=new int[list.size()]; int index=0; for(int l:list){ res[index++]=l; } return res; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package huaweiInterview; import java.util.ArrayList; import java.util.Arrays; /* 求两个数组的交集,包含重复的元素 */ public class number2 { public static void main(String[] args) { int[] num1=new int[]{1,2,3,3,4}; int[] num2=new int[]{2,3,3,4,5}; int[] res2=intersection2(num1,num2); System.out.println(); for(int i:res2){ System.out.print(i+" "); } } //先排序法,不去重 public static int[] intersection2(int[] num1,int[] num2){ if(num1==null||num2==null){ return null; } Arrays.sort(num1); Arrays.sort(num2); int i=0,j=0; ArrayList<Integer> list=new ArrayList<>(); while (i<num1.length&&j<num2.length){ if(num1[i]<num2[j]){ i++; }else if(num1[i]>num2[j]){ j++; }else { list.add(num1[i]);//不去重,直接放入list中 i++; j++; } } int[] res=new int[list.size()]; int index=0; for(int l:list){ res[index++]=l; } return res; } }

题目二:是求一个字符串在另一个字符串中出现的次数。

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package huaweiInterview; public class number3 { public static void main(String[] args) { String str="kkwwkkaakkkww"; String key="kk"; int res=getSubCount2(str,key); System.out.println(res); } //此方法会重复计算,推荐方法2 // public static int getSubCount1(String str,String key){ // if(str==null||key==null||key.length()>str.length()){ // return 0; // } // // int count=0; // for(int i=0;i<str.length();i++){ // for(int j=i+1;j<=str.length();j++){ // if(str.substring(i,j).equals(key)){ // count++; // } // } // } // return count; // } public static int getSubCount2(String str,String key){ if(str==null||key==null||key.length()>str.length()){ return 0; } int index=0; int count=0; //public int indexOf(String str,int fromIndex) //返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始 while ((index=str.indexOf(key,index))!=-1){ index=index+key.length(); count++; } return count; } }

题目三:树的前、中、后序遍历的非递归写法

java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package huaweiInterview; import java.util.ArrayDeque; public class number4 { public static class Node{ int val; Node left; Node right; public Node(int val){ this.val=val; this.left=null; this.right=null; } } //先序遍历,非递归模式 public static void preOder(Node node){ if(node==null) return; ArrayDeque<Node> stack=new ArrayDeque<>(); Node p=node; while (!stack.isEmpty()||p!=null){ if(p!=null){ System.out.print(p.val+" "); stack.addFirst(p); p=p.left; }else { Node t=stack.pollFirst(); p=t.right; } } } //中序遍历,递归版 public static void inOrder(Node node ){ if(node==null) return; inOrder(node.left); System.out.print(node.val+" "); inOrder(node.right); } //后续遍历 非递归版本 public static void postOder(Node node){ if(node==null) return; ArrayDeque<Node> stack=new ArrayDeque<>(); ArrayDeque<Integer> res=new ArrayDeque<>(); Node p=node; while (!stack.isEmpty()||p!=null){ if(p!=null){ res.addFirst(p.val); stack.addFirst(p); p=p.right; }else { Node t=stack.pollFirst(); p=t.left; } } for(int num:res){ System.out.print(num+" "); } } public static void main(String[] args) { Node root=new Node(1); root.left=new Node(2); root.right=new Node(3); root.left.left=new Node(4); root.left.right=new Node(5); root.right.left=new Node(6); root.right.right=new Node(7); preOder(root); System.out.println(); postOder(root); System.out.println(); inOrder(root); } }

最后

以上就是俏皮导师最近收集整理的关于超级基础的编程题50道的全部内容,更多相关超级基础内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部