标签:
一、基础篇
1.不借助第三者实现两个变量值的交换:
package basic;
import java.util.Scanner;
public class ExchangeValue {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入变量A的值:");
long a = scan.nextLong();
System.out.println("请输入变量B的值:");
long b = scan.nextLong();
System.out.println("a=" + a + "\tb=" + b);
System.out.println("变量交换...\t");
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a=" + a + "\tb=" + b);
}
}
2.利用异或进行加解密:
package basic;
import java.util.Scanner;
public class Encryption {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入一个英文字符串或解密字符串");
String password=scan.nextLine();
char[] array=password.toCharArray();
for(int i=0;i<array.length;++i){
array[i]=(char)(array[i]^20000);
}
System.out.println("加密或解密结果如下:");
System.out.println(new String(array));
}
}
3.利用BigDecimal求和:
package basic;
import java.math.BigDecimal;
public class Factorial {
public static void main(String[] args) {
BigDecimal sum=new BigDecimal(0.0);
BigDecimal factorial=new BigDecimal(1.0);
int i=1;
while(i<=10){
sum=sum.add(factorial);
++i;
factorial=factorial.multiply(new BigDecimal(1.0/i));
}
System.out.println("1+1/2!+1/3!+1/4!....+1/10!="+sum);
}
}
4.for循环打印空心菱形:
package basic;
public class Diamond {
public static void main(String[] args) {
printDiamond(6);
}
public static void printDiamond(int size) {
if (size % 2 == 0) {
size++;
}
for (int i = 0; i < size / 2 + 1; ++i) {
for (int j = size / 2 + 1; j > i + 1; j--) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
if (j == 0 || j == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
for (int i = size / 2 + 1; i < size; i++) {
for (int j = 0; j < i - size / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
if (j == 0 || j == 2 * (size - i - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
}
}
5.猴子分桃问题:一堆桃子,5个猴子先后到,每次都均分5份,扔掉多余的一个,拿走自己的那份。问最少有几个桃子,最后一个猴子得到几个桃子。
package basic;
public class MonkeyPeach {
public static void main(String[] args) {
int n = 1;
int m = 0;
int flag = 1;
int monkeyNum = 5;
while (true) {
flag = 1;
m = monkeyNum * n + 1;
for (int i = monkeyNum; i >= 1; i--) {
if ((m % (monkeyNum - 1) == 0)) {
m = m / (monkeyNum - 1) * monkeyNum + 1;
flag++;
} else {
break;
}
}
if (flag == monkeyNum)
break;
n++;
}
System.out.println("桃子总数为:" + m);
System.out.println("第五个猴子得到的桃子数为:" + n);
}
}
6.约瑟夫问题:
package basic;
import javax.swing.JOptionPane;
public class Josephus {
public static void main(String[] args) {
String s;
int n, k, m, n1;
s = JOptionPane.showInputDialog("请输入人数:");
n = Integer.parseInt(s);
n1 = n + 1;
s = JOptionPane.showInputDialog("请输入起始报数的人的编号:");
k = Integer.parseInt(s);
s = JOptionPane.showInputDialog("请输入死亡数字:");
m = Integer.parseInt(s);
int a[] = new int[n + 1];
a[0] = 0;
System.out.println("出局人的编号:");
for (int i = 1; i < a.length; i++) {
a[i] = 1;
}
for (int i = 1; i <= m; i++) {
if (n == 1)
break;
else if (i == m) {
n--;
i = 0;
a[k] = 0;
System.out.print(k + " ");
}
do {
k++;
k = k % n1;
} while (a[k] != 1);
}
System.out.println("");
System.out.println("幸存者编号:" + k);
}
}
7.数独问题:
package basic;
import java.util.Scanner;
public class Sudoku {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入要计算的数独维数(大于1的奇数):");
int x = s.nextInt();
int h = 0;
int l = x / 2;
int[][] a = new int[x][x];
for (int i = 1; i <= x * x; i++) {
a[h][l] = i;
h--;
l++;
if (h < 0 && l >= x) {
h += 2;
l--;
} else if (h < 0) {
h = x - 1;
} else if (l >= x) {
l = 0;
} else if (a[h][l] > 0) {
h += 2;
l--;
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
}
8.数字格式转为货币字符串:
package basic;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class CurrencyFormat {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个货币数字:");
double number = scan.nextDouble();
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CHINA);
System.out.println("Locale.CHINA:" + format.format(number));
format = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println("Locale.US:" + format.format(number));
format = NumberFormat.getCurrencyInstance(Locale.UK);
System.out.println("Locale.UK:" + format.format(number));
}
}
9.检验IP地址:
package basic;
import java.util.Scanner;
public class IPAddress {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要校验的IP地址:");
String text = sc.nextLine();
String info = matches(text);
System.out.println(info);
}
public static String matches(String text) {
if (text != null && !text.isEmpty()) {
String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
if (text.matches(regex)) {
return text + "是一个合法的IP地址!";
} else {
return text + "不是一个合法的IP地址!";
}
}
return "请输入要验证的IP地址!";
}
}
标签:
原文地址:http://www.cnblogs.com/xavierjzhang/p/4501391.html