求值法

求值法是一种最简单的问题求解方法,也是常用的设计算法方法,它是根据给定条件,运用基本的顺序、选择、循环控制结构结局问题。

一、判断闰年

由键盘输入任意一个年份year,通过程序判断,输出这个年份是否是闰年。

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int year = scanner.nextInt();
		Boolean result = leap(year);
		System.out.println(result);
	}

	private static Boolean leap(int year) {
		if (year % 4 == 0 && year % 100 != 0) {
			return true;
		}
		if (year % 400 == 0) {
			return true;
		}
		return false;
	}
}

二、孪生数

孪生数:如果整数A的全部因子(包括1,不包括A本身)之和等于B,并且整数B的全部因子(包括1,不包括B本身)之和等于A,则A,B为孪生数
因子:数i从1到i-1的所有数取余,为0则为因子

给定搜索范围m,n(1<=m<n<=20000),找出指定范围的孪生数。
例如:
输入:100 300
输出:220 284

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int m = scanner.nextInt();
		int n = scanner.nextInt();
		if (m < n && n <= 20000) {
			isTwins(m, n);
		}
	}

	private static void isTwins(int m, int n) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = m + 1; i < n; i++) {
			int count = factor(i);
			map.put(i, count);
//			System.out.println(i+" "+map.get(i));
		}
		for (int i = m + 1; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (map.get(i) == j && map.get(j) == i) {
					System.out.println(i + " " + j);
					break;
				}
			}
		}

	}

	private static int factor(int i) {
		int count = 0;
		for (int j = 1; j < i; j++) {
			if (i % j == 0)
				count += j;
		}
		return count;
	}

}

三、螺旋阵

任意输入一个整数n,按螺旋的方式输出n阶螺旋方阵。
例如:
4
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		spiralsquare(n);
	}

	private static void spiralsquare(int n) {
		int[][] array = new int[n][n];
		int k = 1;
		for (int i = 0; i < n / 2; i++) {
			for (int j = i; j <= n - 2 - i; j++) {
				array[j][i] = k++;
			}
			for (int j = i; j <= n - 2 - i; j++) {
				array[n - 1 - i][j] = k++;
			}
			for (int j = n - 1 - i; j >= i + 1; j--) {
				array[j][n - 1 - i] = k++;
			}
			for (int j = n - 1 - i; j >= i + 1; j--) {
				array[i][j] = k++;
			}
		}
		if (n % 2 == 1) {
			int i = n / 2;
			array[i][i] = n * n;
		}
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length; j++) {
				System.out.printf(array[i][j] + "\t");
			}
			System.out.println();
		}

	}

算法优化

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		spiralsquare(n);
	}

	private static void spiralsquare(int n) {
		int[][] array = new int[n][n];
		int i, j, k = n, r, s = 1, t = 1;
		int[] b = { -1, 0 };
		while (s < n * n) {
			for (r = 0; r < 2 * k - 1; r++) {
				b[r / k] = b[r / k] + t;
				array[b[0]][b[1]] = s++;
			}
			k--;
			t = -t;
		}

		// 输出螺旋方阵
		for (i = 0; i < array.length; i++) {
			for (j = 0; j < array.length; j++) {
				System.out.printf(array[i][j] + "\t");
			}
			System.out.println();
		}

	}

}

四、百灯判熄

有100盏灯,编号为1~100,对应100个开关,开始全为开,进行以下操作:编号为1的倍数反方向拨开关(关灯),编号为2的反方向拨开关····问最后状态为关的灯的编号。

public class Main {
	public static void main(String[] args) {
		judge();
	}

	private static void judge() {
		int i;
		for (i = 1; i <= Math.sqrt(100); i++)
			System.out.print(i * i + " ");
	}
}

五、大数阶乘

1!+2!+3!+ ···········+30!的值

import java.math.BigInteger;

public class Main {
	public static void main(String[] args) {
		BigInteger count = BigInteger.ZERO;
		
		for (int i = 1; i <= 30; i++) {
			BigInteger f = f(i);
			count=count.add(f);
		}
		System.out.println(count);
	}

	private static BigInteger f(int i) {
		BigInteger num = BigInteger.ONE;
		for (int j = 1; j <= i; j++) {
			num=num.multiply(BigInteger.valueOf(j));
			
		}
		return num;
	}
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值