1. 判断101-200之间有多少个素数,并输出所有素数
public static void main(String[] args) {
System.out.println("101-200之间的素数有:");
for (int i = 101; i <= 200; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(i);
}
}
}
2. 一个球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
public static void main(String[] args) {
double height = 100;
double distance = 0;
int count = 10;
for (int i = 1; i <= count; i++) {
distance += height;// 加落下的距离
height = height / 2;// 弹起的高度 第i次弹起的高度
if (i != count) {
distance += height; // 加弹起的距离
}
}
System.out.println("第" + count + "次落地时,经过了:" + distance + "米");
System.out.println("第" + count + "次反弹的高度是:" + height + "米");
}
3. 用100元钱买100支笔,其中钢笔3元/支,圆珠笔2元/支,铅笔0.5元/支,问钢笔、圆珠笔和铅笔可以各买多少支?请写main方法打印需要买的数目。
public static void main(String[] args) {
double money = 100;
double pPrice = 3;
double yPrice = 2;
double qPrice = 0.5;
int count = 100;
for (int pen = 1; pen <= money / pPrice; pen++) {
for (int yuan = 1; yuan <= money / yPrice; yuan++) {
for (int qian = 1; qian <= money / qPrice; qian++) {
if (pen + yuan + qian == count && pen * pPrice + yuan * yPrice + qian * qPrice == money) {
System.out.println("购买" + pen + "支钢笔," + yuan + "支圆珠笔," + qian + "支铅笔");
}
}
}
}
}
4. 通项公式如下:f(n)=n + (n-1) + (n-2) + .... + 1,其中n是大于等于5并且小于10000的整数,例如:f(5) = 5 + 4 + 3 + 2 + 1,f(10) = 10 + 9 + 8 + 7+ 6 + 5 + 4 + 3 + 2 + 1,请用递归的方式完成方法long f( int n)的方法体。
public static long f(int n) {
long sum = 0;
if(n==1){
sum += 1;
}else if(n>1){
sum += n + f(n-1);
}
return sum;
}
5. 求1+2!+3!+...+20!的和
public static void main(String[] args) {
long sum = 0;
for (int i = 1; i <= 20; i++) {
sum += jieCheng(i);
}
System.out.println("sum = " + sum);
}
public static long jieCheng(int n){
long temp = 1;
for (int j = 1; j <=n; j++) {
temp *= j;
}
return temp;
}
6. 第一个人10岁,第2个比第1个人大2岁,以此类推,请用递归方式计算出第8个人多大?
public static void main(String[] args) {
int count = 8;
int age = getAge(count);
System.out.println("第" + count +"个人的年龄:" + age);
}
public static int getAge(int count){
if(count == 1){
return 10;
}else{
return getAge(count-1) + 2;
}
}
7. 有n步台阶,一次只能上1步或2步,共有多少种走法?
答案一:递归
public static int f(int n) {
if (n <= 2)
return n;
int x = f(n - 1) + f(n - 2);
return x;
}
答案二:不用递归
public static int f(int n) {
if (n <= 2)
return n;
int first = 1, second = 2;
int third = 0;
for (int i = 3; i <= n; i++) {
third = first + second;
first = second;
second = third;
}
return third;
}
8. 输入整型数98765,输出是56789
public static void main(String[] args) {
long num = 98765L;
StringBuffer sf = new StringBuffer(num + "");
sf.reverse();
num = Long.parseLong(sf.toString());
System.out.println(num);
}
9. 有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的字数。
举例说明: String content = “中中国55kkfff”;
统计出:中:2 国:1 5:2 k:2 f:3
public static void main(String[] args) {
String content = "中中国55kkfff";
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
while (content.length() > 0) {
Character c = content.charAt(0);
content = content.substring(1);
Integer count = map.get(c);
if (count == null) {
map.put(c, 1);
} else {
map.put(c, count + 1);
}
}
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
System.out.println(entry);
}
}
10. 斐波纳契数列(Fibonacci Sequence),又称黄金分割数列。
一列数的规则如下:1、1、2、3、5、8、13、21、34....求第n位数是多少?
在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用
答案一:递归
public static long fibonacci(int n) {
long result = 1;
if (n > 2) {
result = fibonacci(n - 1) + fibonacci(n - 2);
}
return result;
}
答案二:非递归
public static long fibonacci(int n) {
long result = 1;
if (n > 2) {
long first = 1;
long second = 1;
int i = 0;
n = n - 2;
while (i < n) {
first = second;
second = result;
result = first + second;
i++;
}
}
return result;
}
11. 请使用二分查找算法查找字符数组{“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”}中”g”元素的位置?
public static void main(String[] args) {
char[] arr = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
char findValue = 'g';
int findIndex = -1;
int leftIndex = 0;// 最开始,左边的边界是0
int midIndex = arr.length / 2;// 最开始的中间:arr.length/2
int rightIndex = arr.length - 1;// 最开始,右边的边界是arr.length-1
while (true) {
if (arr[midIndex] == findValue) {
findIndex = midIndex;
break;
} else {
// 判断是否已经到达边界,如果是就结束查找过程
// 如果不是,继续往左边或右边查找
if (midIndex == 0 || midIndex == arr.length - 1) {
break;
}
// 判断是往左还是往右
if (findValue < arr[midIndex]) {
// 往左边查找
rightIndex = midIndex;
midIndex = leftIndex + (rightIndex - leftIndex) / 2;
} else {
// 往右边查找
leftIndex = midIndex;
midIndex = leftIndex + (rightIndex + 1 - leftIndex) / 2;
}
}
}
if (findIndex == -1) {
System.out.println(findValue + "在数组中不存在");
} else {
System.out.println(findValue + "在数组中的位置就是" + findIndex);
}
}
12. 消除下面集合中重复元素?
List list = Arrays.asList(1,2,3,3,4,4,5,5,6,1,9,3,25,4);
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 3, 4, 4, 5, 5, 6, 1, 9, 3, 25, 4);
HashSet<Integer> set = new HashSet<Integer>();
set.addAll(list);
for (Integer integer : set) {
System.out.println(integer);
}
}
}
13. 请用wait()和notify()方法编写一个生产者消费者设计模式程序?
import java.util.Random;
public class Test {
public static void main(String[] args) {
Houseware h = new Houseware();
Worker w = new Worker(h);
Saler s = new Saler(h);
w.start();
s.start();
}
}
class Houseware {
private Object[] buffer = new Object[10];
private int total;
synchronized public void put(Object data) {
if (total >= buffer.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
buffer[total] = data;
total++;
System.out.println(data + "被存入,现在数量是:" + total);
this.notify();
}
synchronized public Object take() {
if (total <= 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object data = buffer[0];
System.arraycopy(buffer, 1, buffer, 0, total - 1);
total--;
this.notify();
System.out.println(data + "被取出,现在数量是:" + total);
return data;
}
}
class Worker extends Thread {
private Houseware h;
public Worker(Houseware h) {
super();
this.h = h;
}
public void run() {
Random r = new Random();
while (true) {
h.put(r.nextInt());
}
}
}
class Saler extends Thread {
private Houseware h;
public Saler(Houseware h) {
super();
this.h = h;
}
public void run() {
while (true) {
Object take = h.take();
}
}
}