以下为java的一些常见的题
1.
代码:
package cn.wwh.www.exercise;
import java.util.Scanner;
/**
* 类的作用:1.输出N * N的乘法口诀表
*
*
* @author 一叶扁舟
* @version 1.0
* @创建时间: 2014年9月4日 下午6:09:08
*/
public class PrintMultTable {
public static void main(String[] args) {
System.out.print("请输入行数:");
Scanner scn = new Scanner(System.in);
int num = scn.nextInt();
printNum(num);
}
/**
* @param inNum
* 控制台输入的整数
*
*/
private static void printNum(int inNum) {
if (inNum < 1) {
System.out.println("输入错误!");
return;
}
System.out.println("输出");
for (int i = 1; i <= inNum; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
}
package cn.wwh.www.exercise;
import java.util.Scanner;
/**
* 类的作用: 2.根据输入的起始数和结束数,输出这个范围的所有质数
*
*
* @author 一叶扁舟
* @version 1.0
* @创建时间: 2014年9月4日 下午6:26:41
*/
public class PrintPrime {
public static void main(String[] args) {
System.out.print("请输入起始数据:");
Scanner scn = new Scanner(System.in);
int beginNum = scn.nextInt();
scn = new Scanner(System.in);
System.out.print("请输入结束数据:");
int endNum = scn.nextInt();
// 调用输出函数
outPrime(beginNum, endNum);
}
/**
* @param beginNum
* 起始数
* @param endNum
* 结束数
*/
private static void outPrime(int beginNum, int endNum) {
if (beginNum >= endNum) {
System.out.println("输入范围错误!");
}
int count = 0; // 用于换行
int sum = 0;// 所有的质数的和
for (int i = beginNum; i < endNum; i++) {
if (isPrime(i)) {
System.out.print(i + "\t");
sum = i + sum;
if (++count % 8 == 0)
System.out.println();
}
}
System.out.println("\ncount:" + count + "\tsum:" + sum);
}
/**
* @param num
* 要判断的整数
* @return 如果i是质数则返回true,否则返回false
*/
private static boolean isPrime(int num) {
boolean flag = false;
if (num <= 1) {
return flag;
}
if (num == 2) {
return true;
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return flag;
}
if (i * i >= num) {
flag = true;
}
}
return flag;
}
}
3.
代码:
package cn.wwh.www.exercise;
import java.util.Scanner;
/**
*类的作用:3.输出水仙花数
*思路:例如输入一个3位数,则三位的范围是[100,1000)
*假设判断123?
*a>temp = 123 last = 123/1%10=3 sum = 3*3*3
*b>temp = 12 last = 12%10 = 2 sum = sum + 2*2*2
*c>temp = 1 last = 1%10 = 1 sum = sum + 1* 1*1
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014年9月4日 下午8:48:25
*/
public class PrintNarcissusNum {
public static void main(String[] args) {
System.out.print("请输入数字的位数:");
Scanner scn = new Scanner(System.in);
int numLength = scn.nextInt();
System.out.println(numLength + "位数的水仙花数为:");
// 输入的最小位数
int begin = (int) Math.pow(10, numLength - 1);
for (int i = begin; i < begin * 10; i++) {
if (isNarcissus(i, numLength)) {
System.out.print(i + "\t");
}
}
}
/**
* @param i
* @return 如果是水仙花数则返回true,否则返回false
*/
private static boolean isNarcissus(int num, int length) {
int temp = 0;
// 记录所有数字之和
int sum = 0;
// 记录各个位上的数字,从右向左记录
int last = 0;
for (int i = 0; i < length; i++) {
int t = (int) Math.pow(10, i);
temp = num / t;
last = temp % 10;
sum = (int) (sum + Math.pow(last, length));
}
return sum == num;
}
}
说明:第一种方法的思路是用数组来模拟一个环形,但是最大的难度是数字的复制,即淘汰数字后将剩下的数字任然组成一个环形。
第二种方式是直接用循环链表,但是最大的难度是按照题目的格式化输出结果
代码:
第一种方式:
package cn.wwh.www.exercise.selectmonkey;
import java.util.Arrays;
import java.util.Scanner;
/**
* 类的作用:5.猴子选大王
* 这种做法有漏洞,并未完善(是个错误的,请勿参考)
* 本人认为这种思路是对的,只不过有些处理分支有些问题,在此写下,以后有时间再转头仔细研究下
*
* @author 一叶扁舟
* @version 1.0
* @创建时间: 2014年9月5日 上午12:32:15
*/
public class SelectKing1 {
public static void main(String[] args) {
while (true) {
System.out.print("请输入猴子的个数m:");
Scanner scn = new Scanner(System.in);
int total = scn.nextInt();
scn = new Scanner(System.in);
System.out.print("请输入淘汰的数字n(n<m):");
int outNum = scn.nextInt();
int king = getKing(total, outNum);
System.out.println("第" + king + "只猴子为猴王!");
}
}
/**
* @param total
* 猴子的总数
* @param outNum
* 要淘汰的数字
* @return 猴王的编号
*/
private static int getKing(int total, int outNum) {
// 记录返回值的,king的编号
int king = 0;
int begin = total;
int circle = 1;
boolean flag = true;
// 初始化数组,数组的长度为total+1,和一个临时数组
int[] monkey = new int[total + 1];
int[] temp = new int[total + 1];
// 让数组中的数据和下标相等
for (int i = 0; i < total + 1; i++) {
monkey[i] = i;
}
while (flag) {
// 还剩下一只猴子了
if (total == 1) {
king = monkey[1];
break;
}
// 用于一个标志位,用于记录一个数组中最大N的倍数的位置
int sign = 0;
if (total != 1)
System.out.print("第" + circle + "轮报数,");
// 将这个n的倍数的数组位置置为0,并用sign记录最大的n的倍数下标
for (int i = 1; i < total + 1; i++) {
if (i % outNum == 0) {
System.out.print("第" + monkey[i] + "只猴子因报" + i + "号 退出!");
monkey[i] = 0;
sign = i;
}
}
int num = 1;
if (total > outNum) {
// 将sing+1-total+1位置
for (int i = sign + 1; i < total + 1; i++) {
// if(i >= total +1) break;
if (monkey[i] != 0) {
temp[num++] = monkey[i];
}
}
// 1-sign非0位置的数据复制到temp数组中
for (int i = 1; i < sign; i++) {
if (monkey[i] != 0) {
temp[num++] = monkey[i];
// 剩下了num - 1只猴子了
}
}
// 将temp数组中的数据复制到monkey数组中
// 首先将monkey全部置为0
for (int i = 1; i < total + 1; i++) {
monkey[i] = 0;
}
monkey = Arrays.copyOfRange(temp, 0, num);
if (num - 1 == 1 || num == 1) {
king = monkey[1];
flag = false;
}
System.out.println("当前退出" + (total + 1 - num) + "只猴子。");
// 还剩下的猴子数量
total = num - 1;
// 报的圈数(轮数)
circle++;
if (total == outNum) {
System.out.print("第" + circle + "轮报数,");
System.out.print("第" + monkey[outNum] + "只猴子因报" + outNum
+ "号 退出!");
System.out.println("当前退出1只猴子。");
monkey[outNum] = 0;
total = total - 1;
circle++;
continue;
}
} else {// 最大标志位小于淘汰数outNum
while (true) {
// int leftNum = total;
int loop = 0;
int tempNum = 0;
for (int i = 1; i < begin * 2; i++) {
tempNum = total * i - outNum;
loop = i;
if (tempNum > 0) {
break;
} else if (tempNum == 0) {
loop++;
break;
}
}
num = 1;
for (int i = 0; i < loop; i++) {
for (int j = 1; j < total + 1; j++) {
temp[num++] = monkey[j];
}
}
if (tempNum == 0) {
System.out.print("第" + ++circle + "轮报数,");
}
System.out.print("第" + temp[outNum] + "只猴子因报" + outNum
+ "号 退出!");
System.out.println("当前退出1只猴子。");
temp[outNum] = 0;
// System.out.println(Arrays.toString(temp));
// 首先将monkey全部置为0
for (int i = 1; i < total + 1; i++) {
monkey[i] = 0;
}
if (tempNum == 0) {
// System.out.println("num:"+num);
num = num - 1;
monkey = Arrays.copyOfRange(temp, outNum, num);
} else {
monkey = Arrays.copyOfRange(temp, outNum, num);
}
System.out.println("num -1 -outNum:" + (num - 1 - outNum));
System.out.println("total" + total);
int m = monkey.length;
System.err.println(m);
if (total - 1 != 1 && total - 1 != num - 1 - outNum) {
for (int i = 1; i < temp.length; i++) {
monkey[m++] = temp[i];
System.out.println(Arrays.toString(monkey));
if (temp[i] == temp[outNum]) {
break;
}
}
}
total = total - 1;
// 还剩下一只猴子了
if (total == 1) {
king = monkey[1];
break;
}
}
}
}
return king;
}
}
第二中方式:
package cn.wwh.www.exercise.selectmonkey;
/**
* 类的作用:一个猴子实体类
*
*
* @author 一叶扁舟
* @version 1.0
* @创建时间: 2014年9月5日 下午10:43:15
*/
public class Monkey {
// 猴子的编号
public int no;
// 当前的猴子的下一个猴子
public Monkey nextMonkey = null;
public Monkey(int no) {
this.no = no;
}
}
package cn.wwh.www.exercise.selectmonkey;
/**
* 类的作用:构建一个猴子环形圈
*
*
* @author 一叶扁舟
* @version 1.0
* @创建时间: 2014年9月5日 下午10:46:46
*/
public class CycleLinked {
// 环形圈中猴子的个数
private int num;
// 第一个猴子,即编号为1
private Monkey firstMonkey = null;
private Monkey tempMonkey = null;
// 创建一个数量为num的猴子圈
public CycleLinked(int num) {
this.num = num;
for (int i = 0; i < num; i++) {
// 如果是第一个猴子
if (i == 0) {
// 创建一个猴子
Monkey monkey = new Monkey(i + 1);
// 指向第一猴子
this.firstMonkey = monkey;
this.tempMonkey = monkey;
} else if (i == num - 1) {// 最后一个猴子
// 创建一个猴子
Monkey monkey = new Monkey(i + 1);
this.tempMonkey.nextMonkey = monkey;
this.tempMonkey = monkey;
// 最后一个猴子指向第一个猴子
this.tempMonkey.nextMonkey = this.firstMonkey;
} else {
// 创建一个猴子
Monkey monkey = new Monkey(i + 1);
this.tempMonkey.nextMonkey = monkey;
this.tempMonkey = monkey;
}
}
}
/**
* @param outNum 要淘汰的数字编号
*
*/
public void specak(int outNum) {
// 剩下猴子的个数
int last = this.num;
// 记录一圈数过的数量
int count = 0;
// 记录轮数
int circle = 1;
Monkey first = this.firstMonkey;
Monkey pre = first;
Monkey delete;
Monkey prePre = first;
System.out.print("第" + circle + "轮报数,");
while (last != 1) {
// 数n-1下,找到要淘汰的猴子的前一个
for (int i = 1; i < outNum - 1; i++) {
while (prePre.nextMonkey.no != pre.no) {
prePre = prePre.nextMonkey;
if (prePre.nextMonkey.no == pre.no)
break;
}
// System.out.println("pre.nextMonkey.no "+pre.nextMonkey.no );
// System.out.println("pre.no"+pre.no);
// System.out.println("prePre.no"+prePre.no);
if ((pre.nextMonkey.no > pre.no && count != 0 && pre.no < prePre.no)
|| (pre.nextMonkey.no < pre.no && count != 0 && pre.no > prePre.no)) {
System.out.println("当前退出" + count + "只");
// 以一圈为循环单位,下一个猴子报数大于当前猴子的报数
circle++;
System.out.print("第" + circle + "轮报数,");
count = 0;
}
// current = pre.nextMonkey;
pre = pre.nextMonkey;
// current = current.nextMonkey;
while (prePre.nextMonkey.no != pre.no) {
prePre = prePre.nextMonkey;
if (prePre.nextMonkey.no == pre.no)
break;
}
// System.out.println("pre.nextMonkey.no "+pre.nextMonkey.no );
// System.out.println("pre.no"+pre.no);
// System.out.println("prePre.no"+prePre.no);
if ((pre.nextMonkey.no > pre.no && count != 0 && pre.no < prePre.no)
|| (pre.nextMonkey.no < pre.no && count != 0 && pre.no > prePre.no)) {
System.out.println("当前退出" + count + "只");
// 以一圈为循环单位,下一个猴子报数大于当前猴子的报数
circle++;
System.out.print("第" + circle + "轮报数,");
count = 0;
}
}
// 删除下一个结点
delete = pre.nextMonkey;
pre.nextMonkey = delete.nextMonkey;
pre = pre.nextMonkey;
System.out.print("第" + delete.no + "只猴子因报" + outNum + "号 退出!");
count++;
while (prePre.nextMonkey.no != pre.no) {
prePre = prePre.nextMonkey;
if (prePre.nextMonkey.no == pre.no)
break;
}
// System.out.println("pre.nextMonkey.no "+pre.nextMonkey.no );
// System.out.println("pre.no"+pre.no);
// System.out.println("prePre.no"+prePre.no);
if ((pre.nextMonkey.no > pre.no && count != 0 && pre.no < prePre.no)
|| (pre.nextMonkey.no < pre.no && count != 0 && pre.no > prePre.no)) {
System.out.println("当前退出" + count + "只");
// 以一圈为循环单位,下一个猴子报数大于当前猴子的报数
circle++;
System.out.print("第" + circle + "轮报数,");
count = 0;
}
last--;
if (last == 1) {
System.out.println("当前退出" + count + "只。");
}
}
System.out.println("第" + pre.no + "只猴子为猴王!");
}
public static void main(String[] args) {
// CycleLinked test = new CycleLinked(7);
// test.specak(3);
// Monkey first = test.getFirstMonkey();
// Monkey temp = first;
//测试猴子圈是否创建正确
// do{
// System.out.println("猴子报数:" + temp.no);
// temp = temp.nextMonkey;
// }while (temp != first);
}
}
package cn.wwh.www.exercise.selectmonkey;
import java.util.Scanner;
/**
*类的作用:2.采用循环链表的方式处理猴子选王的题目
*最大的难度就是格式化输出结果(这个是分析+不断的测试的结果)
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014年9月5日 下午10:42:38
*/
public class SelectKing2 {
public static void main(String[] args) {
while (true) {
System.out.print("请输入猴子的个数m:");
Scanner scn = new Scanner(System.in);
int total = scn.nextInt();
scn = new Scanner(System.in);
System.out.print("请输入淘汰的数字n(n<m):");
int outNum = scn.nextInt();
// 创建一个total长度的猴子圈
CycleLinked test = new CycleLinked(total);
// 淘汰猴子,
test.specak(outNum);
}
}
}
原文地址:http://blog.csdn.net/u011662320/article/details/39096929