题目如下:
中国古代数学家张丘建在他的《算经》中提出了著名的“百钱买百鸡问题”:鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡, 问翁、母、雏各几何?并统计出情况总数。
代码将在36楼贴出!!!
中国古代数学家张丘建在他的《算经》中提出了著名的“百钱买百鸡问题”:鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡, 问翁、母、雏各几何?并统计出情况总数。
代码将在36楼贴出!!!
调试欢乐多
* 有人不知道和谁结婚,于是询问了六位新人中的三位,但听到的回答是这样的:
* A说他将和X结婚;
* X说他的未婚夫是C;
* C说他将和Z结婚。
* 这人听后知道他们在开玩笑,全是假话。
* 请编程找出谁将和谁结婚。
public static void main(String args[]){
int cock;
int hen;
int chicken;
for(cock=1;cock<20;cock++){
for(hen=1;hen<33;hen++){
chicken=100-cock-hen;
if((cock*5+hen*3+chicken/3.0)==100){
System.out.println("cock:"+cock+" hen:"+hen+" chicken:"+chicken);
}
}
}
}
}
public static void main(String[] args) {
find(new int[] { 15, 9, 1 }, 100, 300);
}public static void find(int[] weight, int count, int sum) {
find(weight, count, sum, new int[weight.length], 0);
}public static void find(int[] weight, int count, int sum, int[] num,
int level) {
if (level < 0 || level >= weight.length)
return;
int tempCount = 0;
int tempSum = 0;
for (int i = 0; i < level; i++) {
tempCount += num[i];
tempSum += weight[i] * num[i];
} if (level == weight.length - 1) {
num[level] = count - tempCount;
tempSum += weight[level] * num[level];
if (tempSum == sum)
System.out.println(Arrays.toString(num));
return;
}
for (int i = 0; i <= count - tempCount
&& i <= (sum - tempSum) / weight[level]; i++) {
num[level] = i;
find(weight, count, sum, num, level + 1);
}
}
for(int i=0;i<=14;i++)
{
if((100-7*i)>=0 && (100+i)%4==0)
System.out.println("cock=" + i + ";hen=" + (100-7*i)/4 + ";chicken=" + (100+i)/4*3);
}
这段编码的原则是:当人能够稍稍动动脑子和手就能够轻易地减少程序运行的消耗时,不要把它们留给电脑去做。这在大数据量的程序中相当有用。
int MONEY=100;
int NUMBER=100;
int FLAG=0;
for(int i=0;i<=MONEY/5;i++){
for(int j=0;j<=(MONEY-5*i)/3;j++){
if((i+j+(MONEY-5*i-3*j)*3)==NUMBER){
System.out.println((FLAG++)+"--"+i+":"+j+":"+(100-5*i-3*j)*3);
}
}
}
public class BuyChicken {
public static void main(String[] args) {
int aChicken = 0;
int bChicken = 0;
int cChicken = 0;
//由7X+4Y=100可知X只能是小于13的偶数
for(int X=13;X>=2;X--) {
if(X%2 == 0) {
aChicken = X;
bChicken = (100 - 7*X)/4;
cChicken = 100 - aChicken - bChicken;
if(cChicken%3 == 0)
System.out.println(aChicken+" "+bChicken+" "+cChicken);
}
}
}
}
b x
c y
额,不太喜欢自己的这个解法……
好吧,发下,依然很长,虽然能缩很短……public static void main(String[] args) {
int[] arr = new int[3];
for (int i = 0; i < 3 * 3 * 3; i++) {
toArray(i, arr);
if (condition(arr)) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 3; j++) {
sb.append((char) ('A' + j));
sb.append(':');
sb.append((char) ('X' + arr[j]));
sb.append('\n');
}
System.out.println(sb);
}
}
}public static void toArray(int i, int[] arr) {
if (i < 0 || i > 3 * 3 * 3)
return;
arr[0] = (i / 3 / 3) % 3;
arr[1] = (i / 3) % 3;
arr[2] = i % 3;
}public static boolean condition0(int[] arr) {
return arr[0] != arr[1] && arr[0] != arr[2] && arr[1] != arr[2];
}public static boolean condition1(int[] arr) {
return arr[0] % 3 != 0;
}public static boolean condition2(int[] arr) {
return arr[2] != 0;
}public static boolean condition3(int[] arr) {
return arr[2] != 2;
}public static boolean condition(int[] arr) {
return condition0(arr) && condition1(arr) && condition2(arr)
&& condition3(arr);
}
第二个数组["X","Y","Z"];对第二个求全排列,共6种,对每一种按条件排除就可以了。
public class MoneyAndChicken {
private void Calculating(int TotalM,int TotalC)
{ int cot=0;
int ctt=0;
int ctht=0;
for (cot=0;cot<=TotalC;cot++)
{
for (ctt=0;ctt<=TotalC-cot;ctt++)
{
for (ctht=0;ctht<=TotalC-cot-ctt;ctht++)
{
if (((ctht/3.0+cot*5+ctt*3)==TotalM)&&(cot+ctt+ctht==TotalC))
{
System.out.println("鸡男:"+cot+" "+"鸡女:"+ctt+" "+"鸡仔:"+ctht);
}
}
}
}
}
public static void main(String[] args) {
MoneyAndChicken mc= new MoneyAndChicken();
mc.Calculating(100,100);
}
}
鸡男:0 鸡女:25 鸡仔:75
鸡男:4 鸡女:18 鸡仔:78
鸡男:8 鸡女:11 鸡仔:81
鸡男:12 鸡女:4 鸡仔:84
for(x = 1; x <= 3; x++) {
for(y = 1; y <= 3; y++) {
for(z = 1; z <= 3; z++) {
if(x!=A && x!=C && z!=C && x!=y && x!=z && y!=z) {
System.out.println(x+" "+y+" "+z);
}
}
}
}
static String[] strs1 = {"A","B","C"};
static String[] strs2 = {"X","Y","Z"};
public static void main(String[] args) {
String[] result = new String[strs2.length];
resort(strs2,result,0);
}
public static void resort(String[] strs,String[] result,int index){
if(result[result.length-1]!=null){
if(isResult(result)){
for(int i =0;i<result.length;i++){
System.out.println(strs1[i]+"和"+result[i]+"结婚");
}
}
return;
}
for(int i =0;i<strs.length;i++){
if(strs[i]!=null){
result[index]=strs[i];
strs[i]=null;
resort(strs,result,index+1);
strs[i]=result[index];
result[index]=null;
}
}
}
public static boolean isResult(String[] result){
if(result[0].equals("X")||result[2].equals("X")||result[2].equals("Z"))
return false;
return true;
}
/*
* 甲乙丙丁四个人,每个人只会英、法、德、汉四种语言中的两种。
* 没有一种语言大家都会,但有一种语言三个人都会。
* 另外,甲不会法语,但当乙与丙交流时需要他当翻译。
* 乙会汉语,丁虽然不懂但他们能交流。
* 没有一种语言甲乙丙三个人都会。
* 没有人即懂德语又懂汉语,由此可推三个人都会的语言是什么?
*/
public class Language {
public static void main(String[] args) {
/*
* 1 1 1 1 中 英 法 德 甲 乙 丙 丁
*/
for (int jia = 0; jia <= 15; jia++) {
for (int yi = 0; yi <= 15; yi++) {
for (int bin = 0; bin <= 15; bin++) {
for (int ding = 0; ding <= 15; ding++) {
boolean[][] result = new boolean[4][4];
for (int i = 0; i < 4; i++) {
int num = (int) Math.pow(2, 3 - i);
result[0][i] = ((jia & num) != 0);
result[1][i] = ((yi & num) != 0);
result[2][i] = ((bin & num) != 0);
result[3][i] = ((ding & num) != 0);
}
checkResult(result);
}
}
}
}
} public static int getCount(boolean[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i]) {
count++;
}
}
return count;
} public static boolean hasSameLanguage(boolean[] nums1, boolean[] nums2) {
for (int i = 0; i < nums1.length; i++) {
if (nums1[i] && nums2[i])
return true;
}
return false;
} public static void checkResult(boolean[][] result) {
// 每个人只会英、法、德、汉四种语言中的两种。
for (int i = 0; i < 4; i++) {
if (getCount(result[i]) != 2) {
return;
}
}
int count = 0;
int index = 0;
for (int i = 0; i < 4; i++) {
boolean[] nums = new boolean[4];
nums[0] = result[0][i];
nums[1] = result[1][i];
nums[2] = result[2][i];
nums[3] = result[3][i];
// 没有一种语言大家都会
if (getCount(nums) == 4) {
return;
}
if (getCount(nums) == 3) {
count++;
index = i;
}
}
// 但有一种语言三个人都会
if (count != 1) {
return;
}
// 甲不会法语
if (result[0][2]) {
return;
}
// 当乙与丙交流时需要他当翻译
// 乙与丙无共同语言
if (hasSameLanguage(result[1], result[2])) {
return;
}
// 甲乙、甲丙均有共同语言
if (!hasSameLanguage(result[0], result[1])
|| !hasSameLanguage(result[0], result[3])) {
return;
}
// 乙会汉语,丁虽然不懂但他们能交流。
if(!result[1][0]||!hasSameLanguage(result[1], result[3])||result[3][0]){
return;
}
//没有一种语言甲乙丙三个人都会。
for(int i = 0;i<4;i++){
if(result[0][i]&&result[1][i]&&result[2][i]){
return;
}
}
//没有人即懂德语又懂汉语
for(int i = 0;i<4;i++){
if(result[i][0]&&result[i][3]){
return;
}
}
String str = "中英法德";
System.out.println(str.charAt(index));
}
}
!(a == x || x == c) <==> x==b当然 c == y 这样的话就剩下了 a 和 z 了。结果就是 [a , z] ,[b, x] , [c , y]
这个题我解出来是双解
甲会:英汉
乙会:法汉
丙会:英德
丁会:英法甲会:英德
乙会:英汉
丙会:法德
丁会:英法public static void main(String[] args) {
for (int i = 0; i < 0xffff; i++) {
if (condition(i)) {
print(i);
}
}
}public static boolean condition1(int i) {
for (int j = 0; j < 4; j++) {
int k = ((i >> (4 * j)) & 1) + ((i >> (4 * j + 1)) & 1)
+ ((i >> (4 * j + 2)) & 1) + ((i >> (4 * j + 3)) & 1);
if (k != 2)
return false;
}
return true;
}public static boolean condition2(int i) {
int count3 = 0;
for (int j = 0; j < 4; j++) {
int k = ((i >> j) & 1) + ((i >> (4 + j)) & 1)
+ ((i >> (8 + j)) & 1) + ((i >> (12 + j)) & 1);
if (k == 4)
return false;
if (k == 3)
count3++;
}
return count3 == 1;
}public static boolean condition3(int i) {
int i1 = (i >> 12) & 0xf;
int i2 = (i >> 8) & 0xf;
int i3 = (i >> 4) & 0xf;
return ((i1 >> 2) & 1) == 0 && (i1 & i2) != 0 && (i1 & i3) != 0
&& (i2 & i3) == 0;
}public static boolean condition4(int i) {
int i2 = (i >> 8) & 0xf;
int i4 = i & 0xf;
return (i2 & 1) == 1 && (i4 & 1) == 0 && (i2 & i4) != 0;
}public static boolean condition5(int i) {
int i1 = (i >> 12) & 0xf;
int i2 = (i >> 8) & 0xf;
int i3 = (i >> 4) & 0xf;
return (i1 & i2 & i3) == 0;
}public static boolean condition6(int i) {
for (int j = 0; j < 4; j++) {
if (((i >> (4 * j)) & 1) == 1 && ((i >> (4 * j + 1)) & 1) == 1)
return false;
}
return true;
}public static boolean condition(int i) {
return condition1(i) && condition2(i) && condition3(i) && condition4(i)
&& condition5(i) && condition6(i);
}public static void print(int i) {
int i1 = (i >> 12) & 0xf;
int i2 = (i >> 8) & 0xf;
int i3 = (i >> 4) & 0xf;
int i4 = i & 0xf;
StringBuilder sb = new StringBuilder();
sb.append("甲会:");
if (((i1 >> 3) & 1) == 1)
sb.append('英');
if (((i1 >> 2) & 1) == 1)
sb.append('法');
if (((i1 >> 1) & 1) == 1)
sb.append('德');
if ((i1 & 1) == 1)
sb.append('汉');
sb.append('\n');
sb.append("乙会:");
if (((i2 >> 3) & 1) == 1)
sb.append('英');
if (((i2 >> 2) & 1) == 1)
sb.append('法');
if (((i2 >> 1) & 1) == 1)
sb.append('德');
if ((i2 & 1) == 1)
sb.append('汉');
sb.append('\n');
sb.append("丙会:");
if (((i3 >> 3) & 1) == 1)
sb.append('英');
if (((i3 >> 2) & 1) == 1)
sb.append('法');
if (((i3 >> 1) & 1) == 1)
sb.append('德');
if ((i3 & 1) == 1)
sb.append('汉');
sb.append('\n');
sb.append("丁会:");
if (((i4 >> 3) & 1) == 1)
sb.append('英');
if (((i4 >> 2) & 1) == 1)
sb.append('法');
if (((i4 >> 1) & 1) == 1)
sb.append('德');
if ((i4 & 1) == 1)
sb.append('汉');
sb.append('\n');
System.out.println(sb);
}
不晓得是不是解错掉了……
char[] language = { '英', '法', '德', '汉' };
for (int i = 0; i < 0xffff; i++) {
if (condition(i) >= 0) {
System.out.println("三个人会的语言是: " + language[condition2(i)]);
}
}
}public static boolean condition1(int i) {
for (int j = 0; j < 4; j++) {
int k = ((i >> (4 * j)) & 1) + ((i >> (4 * j + 1)) & 1)
+ ((i >> (4 * j + 2)) & 1) + ((i >> (4 * j + 3)) & 1);
if (k != 2)
return false;
}
return true;
}public static int condition2(int i) {
int count3 = 0;
int index3 = 0;
for (int j = 0; j < 4; j++) {
int k = ((i >> j) & 1) + ((i >> (4 + j)) & 1)
+ ((i >> (8 + j)) & 1) + ((i >> (12 + j)) & 1);
if (k == 4)
return -1;
if (k == 3) {
count3++;
index3 = 3 - j;
}
}
return count3 == 1 ? index3 : -1;
}public static boolean condition3(int i) {
int i1 = (i >> 12) & 0xf;
int i2 = (i >> 8) & 0xf;
int i3 = (i >> 4) & 0xf;
return ((i1 >> 2) & 1) == 0 && (i1 & i2) != 0 && (i1 & i3) != 0
&& (i2 & i3) == 0;
}public static boolean condition4(int i) {
int i2 = (i >> 8) & 0xf;
int i4 = i & 0xf;
return (i2 & 1) == 1 && (i4 & 1) == 0 && (i2 & i4) != 0;
}public static boolean condition5(int i) {
int i1 = (i >> 12) & 0xf;
int i2 = (i >> 8) & 0xf;
int i3 = (i >> 4) & 0xf;
return (i1 & i2 & i3) == 0;
}public static boolean condition6(int i) {
for (int j = 0; j < 4; j++) {
if (((i >> (4 * j)) & 1) == 1 && ((i >> (4 * j + 1)) & 1) == 1)
return false;
}
return true;
}public static int condition(int i) {
return condition1(i) && (condition2(i) >= 0) && condition3(i)
&& condition4(i) && condition5(i) && condition6(i) ? condition2(i)
: -1;
}话说34楼你题目中的解有问题,丙的数组下标应该是2,你在甲丙有共同语言里写的是3……
public class Main {
public static void main(String[] args) {
/*做一个二维数组,行为A、B、C,列为X、Y、Z,这样就会有9个值,全部初始化为
* true,意味着所有人之间都可以结婚(当然这是假设的),然后根据调查的三个
* 人的话,确定他们之间的关系,如果没有关系,则置为false。一行或一列只能
* 有一个true和两个false, 因为一个人只能和另外一个人结婚。根据最后剩下
* 的三个true,就可以知道谁和谁结婚。
*/
//初始化
boolean[][] relation = new boolean[3][3];
for(int i=0; i<relation.length; i++) {
for(int j=0;j<relation[i].length; j++) {
relation[i][j] = true;
}
}
//因为3个人都说的是假话,所以就有
relation['A'-'A']['X'-'X'] = false;//A说他将和X结婚;
relation['C'-'A']['X'-'X'] = false;//X说他的未婚夫是C;
relation['C'-'A']['Z'-'X'] = false;//C说他将和Z结婚。
boolean flag = true;
while(flag) {
//处理
for(int i=0; i<relation.length; i++) {
int count_false = 0;
int row = 0;
int col = 0;
for(int j=0;j<relation[i].length; j++) {
if(!relation[i][j])
count_false ++;
else {
row = i;
col = j;
}
}
if(count_false == 2) {//说明剩下的一个肯定为true,他所在列的其余两个都为false
for(int k=0; k<relation[i].length; k++) {
relation[k][col] = false;
}
relation[row][col] = true;
}
}
//判断是否处理结束
boolean end = true;
for(int i=0; i<relation.length; i++) {
int count_false = 0;
for(int j=0; j<relation[i].length; j++) {
if(!relation[i][j]) {
count_false ++;
}
}
if(count_false != 2) {
end = false;
break;
}
}
if(end) {
break;
}
}
//打印结果
for(int i=0; i<relation.length; i++) {
for(int j=0; j<relation[i].length; j++) {
if(relation[i][j])
System.out.println((char)('A' + i) + "和" + (char)('X' + j) + "结婚");
}
}
}
}
public class Test{
static String COUPLE_SEPRATOR = ",";
static String SEX_SEPRATOR = ":";
public static List<String> blinddate(String[] men, String[] women){
List<String> result = new ArrayList<String>();
int p = men.length * (women.length - 1);
for (int i = 0; i < p; i++){
String line = "";
for (int ii = 0; ii < men.length; ii++){
line = line + men[ii]+SEX_SEPRATOR+women[ii];
if (ii != men.length - 1){
line = line + COUPLE_SEPRATOR;
}
}
result.add(line);
swapArray(men, i % (men.length - 1));
}
return result;
}
public static void swapArray(Object[] array, int index){
if (index >= array.length) return;
Object o = array[index];
array[index] = array[index + 1];
array[index + 1] = o;
}
public static List<String> except(List<String> couples, List<String> exceptions){
for (String exception:exceptions){
for (int i = 0; i < couples.size();){
if (couples.get(i).contains(exception))
couples.remove(i);
else
i++;
}
}
return couples;
}
public static void main(String[] args){
String[] men = {"A","B","C"};
String[] women = {"X","Y","Z"};
String[] exceptcouple = {"A:X","C:X","C:Z"};
System.out.print(except(blinddate(men,women),Arrays.asList(exceptcouple)));
}
}
public static int condition(int i) {
int j;
return condition1(i) && (j = condition2(i)) >= 0 && condition3(i)
&& condition4(i) && condition5(i) && condition6(i) ? j : -1;
}第二处是输出结果,同样去掉再次执行condition2:
for (int i = 0, j = -1; i < 0xffff; i++) {
if ((j = condition(i)) >= 0) {
System.out.println("三个人会的语言是: " + language[j]);
}
}
第一题:using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x, y, z;//定义鸡公、鸡婆和小鸡的数目
int count = 0;//定义情况数目
for (x = 0; x <= 100; x++)
{
for (y = 0; y <= 100; y++)
{
if (x + y > 100)
continue;
else
{
z = 100 - x - y;
if (z % 3 != 0)
continue;
else if (5 * x + 3 * y + z/3 == 100)
{
count++;
Console.WriteLine("鸡公数为:" + x.ToString() + " 鸡婆数为:" + y.ToString() + " 小鸡数为:" + z.ToString());
}
}
}
}
Console.WriteLine("情况为:"+count.ToString());
}
}
}第二题:using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Class1
{
public static void Main(string[] args)
{
char[] xinlang ={ 'A', 'B', 'C' };
char[] xinniang ={ 'X', 'Y', 'Z' };
for (int i = 0; i < 3; i++)
{
if (i != 0)
{
char temp = xinniang[0];
xinniang[0] = xinniang[1];
xinniang[1] = xinniang[2];
xinniang[2] = temp;
}
if (xinlang[0] == 'A' && xinniang[0] == 'X')
continue;
else if (xinlang[2] == 'C' && xinniang[2] == 'X')
continue;
else if (xinlang[2] == 'C' && xinniang[2] == 'Z')
continue;
else
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("第"+j.ToString()+"对:" + xinlang[j].ToString() + "与" + xinniang[j].ToString());
}
}
}
}
}
}
public static void main(String args[]){
int cock;
int hen;
int chicken;
for(cock=1;cock<20;cock++){
for(hen=1;hen<33;hen++){
chicken=100-cock-hen;
if((cock*15+hen*9+chicken)==300){
System.out.println("cock:"+cock+" hen:"+hen+" chicken:"+chicken);
}
}
}
}
}
{
public static int num;
public static int money;
public static int x;
public static int y;
public static int z;
public static void main(String args[]){
for(x = 1;x<=100;x++){
for(y = 1;y<=100;y++){
for(z = 3;z<=100;z++){
if(z%3 == 0){
num = x + y +z;
money = 5*x + 3*y +z/3;
//System.out.println(z);
if((num == 100)&&(money == 100)){
System.out.println("公鸡数:" + x + ",母鸡数:" + y + ",小鸡数:" + z + "; 总钱数:" + money + ",总鸡数:" + num);
}
}
}
}
}
}
}
输出结果是:
公鸡数:4,母鸡数:18,小鸡数:78; 总钱数:100,总鸡数:100
公鸡数:8,母鸡数:11,小鸡数:81; 总钱数:100,总鸡数:100
公鸡数:12,母鸡数:4,小鸡数:84; 总钱数:100,总鸡数:100
import java.util.ArrayList;
import java.util.List;public class Pair { public Pair(String name){
this.name = name;
}
private String name ; public String getName(){
return this.name;
}
public boolean equals(Pair p){
//设置已知条件
if("a".equals(this.name)){
if("x".equals(p.name)){
return false;
}
}
if("x".equals(this.name)){
if("c".equals(p.name)){
return false;
}
}
if("c".equals(this.name)){
if("x".equals(p.name)){
return false;
}
}
return true;
} public static void main(String[] args){
List<Pair> abcList = new ArrayList<Pair>();
List<Pair> xyzList = new ArrayList<Pair>();
abcList.add(new Pair("a"));
abcList.add(new Pair("b"));
abcList.add(new Pair("c"));
xyzList.add(new Pair("x"));
xyzList.add(new Pair("y"));
xyzList.add(new Pair("z"));
for(Pair pabc : abcList){
for(Pair pxyz : xyzList){
if(pabc.equals(pxyz)){
System.out.println("新郎 : " + pabc.getName() + " , 新娘 : " + pxyz.getName());
xyzList.remove(pxyz);
break;
}
}
}
}
结果:新郎 : a , 新娘 : y
新郎 : b , 新娘 : x
新郎 : c , 新娘 : z
假设公鸡为x,母鸡为y,小鸡为z,
则有:5*x+3*y+1/3*z=100
x+y+z=100
经过化简得:y=(100-7*x)/4
下面根据这个算式再写程序就简单多了public static void main(String[] args) {
for (int x = 1; x < 13; x++) {
if((100-7*x)%4==0){
int y = (100-7*x)/4;
int z = 100 - x - y;
System.out.println("公鸡:"+ x + ";母鸡:" + y + ";小鸡:" + z);
}
}
}
{
String name;void setN(String n)
{
name=n;}boolean marry(female ob2)
{
if(name.equals("A")&&ob2.name.equals("X")){return false;}
else{
if(name.equals("C")&&ob2.name.equals("X")){return false;}
else{
if(name.equals("C")&&ob2.name.equals("Z")){return false;}
else{return true;}
}
}
}
}class female
{
String name;void setN(String n)
{
name=n;
}
}public class pro
{
public static void main(String args[])
{
//
male[] m=new male[3];
for(int i=0;i<m.length;i++)
{
m[i]=new male();
}
m[0].setN("A");
m[1].setN("B");
m[2].setN("C");
//
female[] f=new female[3];
for(int j=0;j<m.length;j++)
{
f[j]=new female();
}
f[0].setN("X");
f[1].setN("Y");
f[2].setN("Z");
//
for(int iF=0;iF<=2;iF++)
{
for(int iS=0;iS<=2;iS++)
{
int iT=3-iF-iS;
try
{
if( (m[0].marry(f[iF])==true) && (m[1].marry(f[iS])==true) && (m[2].marry(f[iT])==true) && (iF!=iS) &&(iT>=0))
{
System.out.println("A与"+f[iF].name);
System.out.println("B与"+f[iS].name);
System.out.println("C与"+f[iT].name);
}
else
{
if(iT<0){break;}
}
}
catch(Exception e)
{System.out.println("end by "+e);}}
}}
}
28的逻辑让人眼前一亮啊
public static void main(String[] args){
//gongji x,muji y,xiaoji 300-15x-9y
int x,y,z,count=0;
x=20;
y=33;
int maXx = 0,maXy = 0;
while(x>0){
z=300-x*15;
if(z+x<100)
x--;
else{
maXx=x;
break;
}
}
while(y>0){
z=300-y*9;
if(z+y<100)
y--;
else{
maXy=y;
break;
}
}
for(x=maXx;x>0;x--){
y=0;
while(7*x+4*y<100&&y<maXy)
y++;
if(7*x+4*y==100)
count++;
}
System.out.println(maXy);
System.out.println(maXx);
System.out.println(count);
}
}第一题
long time = System.nanoTime();
int[] arr = new int[3];
for (int i = 0; i < 3; i++) {
arr[0] = i;
for (int j = 0; j < 3; j++) {
if (i == j)
continue;
arr[1] = j;
arr[2] = 3 - i - j;
if (arr[0] != 0 && arr[2] != 0 && arr[2] != 2) {
StringBuilder sb = new StringBuilder();
for (int k = 0; k < 3; k++) {
sb.append((char) ('A' + k)).append(':').append(
(char) ('X' + arr[k])).append('\n');
}
System.out.print(sb);
}
}
}
time = System.nanoTime() - time;
System.out.println(time);
}第二题再玩一个……
28楼的逻辑很正常啊,我觉得大部分人应该都是这个思路吧
public static void main(String[] args)
int cock,hen,chicken;
for(cock=0;cock<20;cock++){
for(hen=0;hen<33;hen++){
chicken=100-cock-hen;
if(chicken%3!=0)continue;
if((cock*5+hen*3+chicken/3)==100){
System.out.println("cock:"+cock+" hen:"+hen+" chicken:"+chicken);
}
}
}
}
/**
* 三对情侣参加婚礼,三个新郎为A\B\C,三个新娘为X\Y\Z。
* 有人不知道和谁结婚,于是询问了六位新人中的三位,但听到的回答是这样的:
* A说他将和X结婚;
* X说他的未婚夫是C;
* C说他将和Z结婚。
* 这人听后知道他们在开玩笑,全是假话。
* 请编程找出谁将和谁结婚。
*/
public class Test3 { public static boolean canMarry(char man, char woman) {
if ((man == 'A' && woman == 'X')
|| (man == 'C' && woman == 'X')
|| (man == 'C' && woman == 'Z')) {
return false; // 排除由题意中的3种情况
}else if ((man == 'A' && woman == 'Y')) {
return false; // 由题意得知隐含条件: C和Y成对,所以A不可能和Y成对
}else
return true;
} public static void main() {
char[] man = { 'A', 'B', 'C' };
char[] woman = { 'X', 'Y', 'Z' };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (canMarry(man[i], woman[j])) {
System.out.println(man[i] + " <--> " + woman[j]);
break; // 一个新郎找到自己的新娘后立即跳出内循环
}
}
}
}
}
canMarry根本不能称之为方法
而且如果不加入那个隐藏条件,就是错的,如果写程序中已经加入这种所谓隐藏条件,那程序和你直接
System.out.println("A <--> Z");
System.out.println("B <--> X");
System.out.println("C <--> Y");
没有太大区别