/* * 找出两个正整数的最大公约数 **/ @Test publicvoidgreatestCommonDivisor(){ Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); long n1 = input.nextLong(); System.out.print("Enter second integer: " ); long n2 = input.nextLong(); int gcd = 1; for (int k = 1; k <= n1 && k <= n2; ++k) { if (n1 % k == 0 && n2 % k == 0) gcd = k; }
System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd); }
找出两个正整数的最大公约数(欧几里得算法)
publicstaticintgcd(int m, int n){ if (m % n == 0) return n; else return gcd(n, m % n); }
十六进制转十进制
/* * 十进制转十六进制 **/ @Test publicvoiddec2Hex(){ Scanner input = new Scanner(System.in); System.out.print("Enter a decimal number: "); int decimal = input.nextInt(); String hex = "";
privatestaticbooleanisPalindrome(String s, int low, int high){ if (high <= low) returntrue; elseif (s.charAt(low) != s.charAt(high)) returnfalse; else return isPalindrome(s, low + 1, high - 1); }
publicstaticvoidmain(String[] args){ System.out.println("Is moon a palindrome? " + isPalindrome("moon")); System.out.println("Is noon a palindrome? " + isPalindrome("noon")); System.out.println("Is a a palindrome? " + isPalindrome("a")); System.out.println("Is aba a palindrome? " + isPalindrome("aba")); System.out.println("Is ab a palindrome? " + isPalindrome("ab")); } }
判断回文数
/* * 判断回文数 **/ @Test publicvoidpalindromeInt(){ Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt();
if (number < 0 || (number % 10 == 0 && number != 0)) System.out.println(false);
int reversedNumber = 0; while (number > reversedNumber) { reversedNumber = reversedNumber * 10 + number % 10; number /= 10; }
System.out.println(number == reversedNumber || number == reversedNumber / 10); }
求 100 以内质数
/* * 输出 100 以内的质数 * */ @Test publicvoidprintNumber(){ label:for (int i = 2; i <= 100; ++i) { for (int j = 2; j <= Math.sqrt(i); ++j) { if (i % j == 0) { continue label; } } System.out.println(i); } }
打印环形矩阵
/* * 打印环形矩阵 * Enter an integer: 4 * 01 02 03 04 * 12 13 14 05 * 11 16 15 06 * 10 09 08 07 * */ @Test publicvoidprintCircularMatrix(){ Scanner input = new Scanner(System.in); System.out.println("Enter an integer: "); int n = input.nextInt(); int[][] array = newint[n][n];
int count = 0; int maxX = n - 1; int maxY = n - 1; int minX = 0; int minY = 0;
while (minX <= maxX) { for (int x = minX; x <= maxX; ++x) array[minY][x] = ++count; minY++; for (int y = minY; y <= maxY; ++y) array[y][maxX] = ++count; maxX--; for (int x = maxX; x >= minX; --x) array[maxY][x] = ++count; maxY--; for (int y = maxY; y >= minY; --y) array[y][minX] = ++count; minX++; }
for (int[] eArray : array) { for (int e : eArray) { String space = (e + "").length() == 1 ? "0" : ""; System.out.print(space + e + "\t"); } System.out.println(); } }
for (int i = 0; i < deck.length; ++i) deck[i] = i;
for (int i = 0; i < deck.length; ++i) { int index = (int)(Math.random() * deck.length); int temp = deck[i]; deck[i] = deck[index]; deck[index] = temp; }
for (int i = 0; i < 4; ++i) { String suit = suits[deck[i] / 13]; String rank = ranks[deck[i] % 13]; System.out.println("Card number " + deck[i] + ": " + rank + " of" + suit); } }
构建一个随机元素数组列表
要求数组列表中各元素不重复
ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i < 9; ++i) { int temp = (int)(Math.random() * 9) + 1; if (arrayList.contains(temp)) { i--; continue; } arrayList.add(i, temp); }
privatestaticlonggcd(long n, long d){ long n1 = Math.abs(n); long n2 = Math.abs(d); int gcd = 1;
for (int k = 1; k <= n1 && k <= n2; ++k) { if (n1 % k == 0 && n2 % k == 0) gcd = k; }
return gcd; }
publiclonggetNumerator(){ return numerator; }
publiclonggetDenominator(){ return denominator; }
public Rational add(Rational secondRational){ long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); returnnew Rational(n, d); }
public Rational subtract(Rational secondRational){ long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); returnnew Rational(n, d); }
public Rational multiply(Rational secondRational){ long n = numerator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); returnnew Rational(n, d); }
public Rational divide(Rational secondRational){ long n = numerator * secondRational.getDenominator(); long d = denominator * secondRational.getNumerator(); returnnew Rational(n, d); }
@Override public String toString(){ if (denominator == 1) return numerator + ""; else return numerator + "/" + denominator; }