日期:2014-05-20 浏览次数:20756 次
import java.util.Random; public class Test { public static void main(String[] args) { // 1. 百元买百鸡 // 5X + 3Y + Z/3 = 100; // X + Y + Z = 100; -> // 15X + 9Y + (100 - X - Y) = 300; -> // 14X + 8Y = 200; -> // 7X + 4Y = 100; for (int i = 0; i < 100 / 4; i++) { if ((100 - i * 4) % 7 == 0) { int x = (100 - i * 4) / 7; int y = i; int z = 100 - x - y; System.out.println(x + ":" + y + ":" + z); } } // 2.彩票 int[] caipiao = new int[7]; for (int i = 0; i < 7; i++) { caipiao[i] = -1; } for (int i = 0; i < 6; i++) { int ran = new Random().nextInt(33) + 1; while (haveSame(caipiao, ran)) { ran = new Random().nextInt(33) + 1; } caipiao[i] = ran; } caipiao[6] = new Random().nextInt(16) + 1; // 打印 for (int i : caipiao) { System.out.println(i); } } public static boolean haveSame(int[] caipiao, int ran) { for (int i = 0; i < 6; i++) { if (caipiao[i] == ran) { return true; } } return false; } }
------解决方案--------------------
// 题目4 public static void main(String args[]) { int m = 0; int s = 0; int i = 0; int a[] = new int[1000]; for (s = 2; s < 1000; s++) { m = 0; for (i = 1; i < s; i++) { if (s % i == 0) a[i - 1] = i; else a[i - 1] = 0; } for (i = 1; i < s; i++) m = m + a[i - 1]; if (m == s) System.out.println(s); } }
------解决方案--------------------
import java.util.Random;
public class Test
{
public static void main(String[] args)
{
// 1. 百元买百鸡
// 5X + 3Y + Z/3 = 100;
// X + Y + Z = 100; ->
// 15X + 9Y + (100 - X - Y) = 300; ->
// 14X + 8Y = 200; ->
// 7X + 4Y = 100;
for (int i = 0; i < 100 / 4; i++)
{
if ((100 - i * 4) % 7 == 0)
{
int x = (100 - i * 4) / 7;