题目2
题目4
1.恺撒密码
输入:
偏移量K
待加密的字符串
输出:
加密后的字符串。注意只需用对大小写字母进行变换,其它字符原样输出
public class Main{ public static void main(String[] args) { // 偏移大小 int offset = Integer.parseInt(StdIn.readLine()); while (!StdIn.isEmpty()) { String inStr = StdIn.readLine(); char[] arr = inStr.toCharArray(); // 对单个字符解密 for (int i = 0; i < arr.length; i++) { int chNum = arr[i]; int passChar = 0; // 是否为英文字母 if (chNum >= 97 && chNum <= 122) { passChar = chNum + offset; if (passChar > 122) { passChar = passChar - 123 + 97; } } else if (chNum >= 65 && chNum <= 90) { passChar = chNum + offset; if (passChar > 90) { passChar = passChar - 91 + 65; } } else { passChar = chNum; } arr[i] = (char) passChar; } // 输出 System.out.println(arr); } } }
2.翻硬币
输入:
初始硬币的数量n
输出:
至少需要多少次翻转,又能恢复最初的正面朝上。
public class Main { public static void main(String[] args) { // 输入硬币数量 int N = StdIn.readInt(); // 定义硬币数字,false:正面 true:背面 boolean[] bool = new boolean[N]; // 统计反转次数 int num = 0; // 循环控制条件 boolean breakFlag = true; while (breakFlag) { for (int i = 0; i < N && breakFlag; i++) { breakFlag = false; int j = 0; for (; j <= i / 2; j++) { // 确定交换位置并交换 int x = i - j; if (x == j) { bool[j] = !bool[j]; } else { boolean tmp = !bool[j]; bool[j] = !bool[x]; bool[x] = tmp; } } num++; // 检测退出条件成立 for (int k = 0; k < N; k++) { if (bool[k]) { breakFlag = true; break; } } } } System.out.println(num); } }
黄一鹤提供版本:
import java.util.Scanner; public class Coins { public static void main(String[] args) { Scanner cin=new Scanner(System.in); System.out.println("请输入硬币个数"); int coins=cin.nextInt(); int[] ints =new int[coins] ; for (int i = 0; i < ints.length; i++) { ints[i]=0; } boolean isTrue = false; int i = 1; for (; ; i++) { isTrue = true; int n = i%coins; if(n==0) { n=coins; } changePosition(ints,n); trunCoins(ints,n); printResult(ints,i); for (int j = 0; j< ints.length; j++) { if(ints[j]!=0) { isTrue = false; break; } } if(isTrue) { break; } } System.out.println("```````````````````````````"); System.out.println("总共"+coins+"个硬币"); System.out.println("总共需要翻转"+i+"次"); System.out.println("```````````````````````````"); } public static void changePosition(int[] ints,int position) { position = position -1;; int i = 0; int temp; while(i<position) { temp = ints[i]; ints[i]=ints[position]; ints[position]=temp; i++; position--; } } public static void trunCoins(int[] ints, int n) { for (int j = 0; j < n; j++) { ints[j] = (ints[j]+1)%2; } } public static void printResult(int[] ints,int n) { System.out.print("第"+n+"次翻转结果:"); for (int j = 0; j< ints.length; j++) { System.out.print(" "+ints[j]); } System.out.println(); } }
3.随机播放和生日“悖论”
输入:
歌曲总数量N,蒙特卡洛模拟的次数T(都是整数)
输出:
取整后的平均次数(为了方便系统进行自动判定,故请输出整数。取整方法:(int)(x+0.5))
public class Main { public static void main(String[] args) { int N = StdIn.readInt(); int M = StdIn.readInt(); boolean arr[] = null; double sum = 0; for (int i = 0; i < M; i++) { int num = 1; arr = new boolean[N]; while (true) { int rad = (int) (Math.random() * N); if(arr[rad]){ break; } arr[rad] = true; num ++; } sum += num; } System.out.println((int) (sum / M + 0.5)); } }
黄一鹤提供版本:
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class MathRandom { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int N=cin.nextInt(); int T=cin.nextInt(); double sumTimes = 0; int result; for (int i = 0; i < T; i++) { result = returnMandomTimes(N); sumTimes = sumTimes + result; } System.out.println((int)(sumTimes/T+0.5)); } public static int returnMandomTimes(int n) { Map<Integer, String> map = new HashMap<Integer, String>(); boolean isExist = false; int i = 1; while (!isExist) { int mathRandom = (int) (Math.random()*n); if(null!=map.get(mathRandom)) { isExist = true; } else { map.put(mathRandom, "exist"); i++; } } return i; } }