一个很有意思的问题的算法

flyingfz 2004-12-07 04:31:12
这个问题我不知道放在哪个区,感觉C和JAVA都可以.但我感觉JAVA可能更简单点.
我正在学习编程,大家交流交流吧.
问题是这样的:有四个砝码,总重量是40克,砝码的质量是整数,且各不相等.请请确定它们的质量,使之能称出1到40克之间任何整数质量的物体.
...全文
385 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
hotforever 2004-12-11
  • 打赏
  • 举报
回复
我也来写一个c的程序。

#include<iostream>
#include<iomanip>
#define N 40
using namespace std;
bool isValidate(int,int,int,int);
int main()
{
for(int a = 1; a < N - 3; a++)
for(int b = a + 1; b < N - 2; b++)
for(int c = b + 1; c < N - 1; c++)
for(int d = c + 1; d < N ; d++)
if(isValidate(a,b,c,d))
cout << setw(4) << a << setw(4) <<b << setw(4) << c << setw(4) << d << endl;
return 0;
}
bool isValidate(int a,int b, int c, int d)
{
int arr[N];
for(int i = 0;i < N; i++)
arr[i] = 0;
for(int a1 = -1;a1 <=1; a1++)
for(int b1 = -1;b1 <= 1;b1++)
for(int c1 = -1;c1 <= 1; c1++)
for(int d1 = -1; d1 <=1;d1++)
{
int sum = a * a1 + b * b1 + c * c1 + d * d1;
if(sum > 0 && sum <= N)
arr[sum-1] = 1;
}
for( i = 0;i < N; i++)
if(arr[i] == 0)
return false;
return true;

}
dddeee 2004-12-11
  • 打赏
  • 举报
回复
ACM中应该很多这种题目
http://acm.zju.edu.cn/
flyingfz 2004-12-11
  • 打赏
  • 举报
回复
谢谢大家了,结帖。
narilee 2004-12-11
  • 打赏
  • 举报
回复
So Interesting!
superman421 2004-12-11
  • 打赏
  • 举报
回复
考虑算法就不要考虑具体语言!
virgin_killer 2004-12-10
  • 打赏
  • 举报
回复
bigpool(wtj) ( )
=========================
你的答案也正确,但算法复杂度较大,我的为63ms,你的在173左右,呵呵
virgin_killer 2004-12-10
  • 打赏
  • 举报
回复
刚刚发现程序有点问题,现在改进了一下,能找出正确答案了: 1,3,9,27
public class FindFama
{
int a ;
int b;
int c;
int d;
int num = 40;
int i = 0;
public FindFama()
{
long start = System.currentTimeMillis();
for(a = 0; a < num; a++)
for(b = 0; b < num - a ; b++)
for(c = 0; c < num - a - b; c++)
{
d = num - a - b - c;
i ++ ;
if (find(a, b, c, d))
{
System.out.print("a = " + a + ";");
System.out.print("b = " + b + ";");
System.out.print("c = " + c + ";");
System.out.println("d = " + d);
}
}
System.out.println("time cost " + (System.currentTimeMillis() - start));
System.out.println(i);
}
public boolean find(int a,int b, int c,int d)
{
for ( int weight = 1; weight < num + 1 ; weight++)
{
if( oneFama(a,b,c,d,weight)||twoFama(a,b,c,d,weight)
||threeFama(a,b,c,d,weight)||fourFama(a,b,c,d,weight)
){}
else
return false;

}
return true;
}
public boolean oneFama (int a, int b, int c, int d,int weight)
{
if( weight == a || weight == b || weight == c|| weight == d)
return true;
else
return false;
}
public boolean twoFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b || weight == a + c ||
weight == a + d || weight == b + c ||
weight == b + d ||
weight == c + d || weight + a == b ||
weight + a == c ||
weight + a == d || weight + b == a ||
weight + b == c || weight + b == d ||
weight + c == a || weight + c == b ||
weight + c == d || weight + d == a ||
weight + d == b || weight + d == c
)
return true;
else
return false;
}
public boolean threeFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c || weight == a + b + d ||
weight == b + c + d || weight == a + c + d ||
weight + a == b + c ||
weight + a == b + d || weight + a == c + d ||
weight + b == a + c || weight + b == a + d ||
weight + b == c + d || weight + c == a + b ||
weight + c == a + d || weight + c == b + d ||
weight + d == a + b || weight + d == a + c ||
weight + d == b + c || weight + b + c == a ||
weight + b + d == a || weight + c + d == a ||
weight + a + c == b || weight + a + d == b ||
weight + c + d == b || weight + a + b == c ||
weight + a + d == c || weight + b + d == c ||
weight + a + b == d || weight + a + c == d ||
weight + b + c == d )
return true;
else
return false;
}
public boolean fourFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c + d || weight + a == b + c + d ||
weight + b == a + c + d || weight + c == a + b + d ||
weight + b + c + d == a || weight + a + c + d == b ||
weight + a + b + d == c || weight + a + b + c == d ||
weight + d == a + b + c || weight + a + b == c + d ||
weight + c + d == a + b || weight + a + c == b + d ||
weight + b + d == a + c || weight + a + d == b + c ||
weight + b + c == a + d
)
return true;
else
return false;
}

public static void main(String[] args)
{
new FindFama();

}
}
flyforlove 2004-12-10
  • 打赏
  • 举报
回复
有个缺点,就是没有考虑单个数括号的问题。
flyforlove 2004-12-10
  • 打赏
  • 举报
回复
结果是
1
3
9
27
81
2
1=1
2=3-(1)
3=3
4=3+(1)
5=9-(3+(1))
6=9-(3)
7=9-(3-(1))
8=9-(1)
9=9
10=9+(1)
11=9+(3-(1))
12=9+(3)
13=9+(3+(1))
14=27-(9+(3+(1)))
15=27-(9+(3))
16=27-(9+(3-(1)))
17=27-(9+(1))
18=27-(9)
19=27-(9-(1))
20=27-(9-(3-(1)))
21=27-(9-(3))
22=27-(9-(3+(1)))
23=27-(3+(1))
24=27-(3)
25=27-(3-(1))
26=27-(1)
27=27
28=27+(1)
29=27+(3-(1))
30=27+(3)
31=27+(3+(1))
32=27+(9-(3+(1)))
33=27+(9-(3))
34=27+(9-(3-(1)))
35=27+(9-(1))
36=27+(9)
37=27+(9+(1))
38=27+(9+(3-(1)))
39=27+(9+(3))
40=27+(9+(3+(1)))
41=81-(27+(9+(3+(1))))
42=81-(27+(9+(3)))
43=81-(27+(9+(3-(1))))
44=81-(27+(9+(1)))
45=81-(27+(9))
46=81-(27+(9-(1)))
47=81-(27+(9-(3-(1))))
48=81-(27+(9-(3)))
49=81-(27+(9-(3+(1))))
50=81-(27+(3+(1)))
51=81-(27+(3))
52=81-(27+(3-(1)))
53=81-(27+(1))
54=81-(27)
55=81-(27-(1))
56=81-(27-(3-(1)))
57=81-(27-(3))
58=81-(27-(3+(1)))
59=81-(27-(9-(3+(1))))
60=81-(27-(9-(3)))
61=81-(27-(9-(3-(1))))
62=81-(27-(9-(1)))
63=81-(27-(9))
64=81-(27-(9+(1)))
65=81-(27-(9+(3-(1))))
66=81-(27-(9+(3)))
67=81-(27-(9+(3+(1))))
68=81-(9+(3+(1)))
69=81-(9+(3))
70=81-(9+(3-(1)))
71=81-(9+(1))
72=81-(9)
73=81-(9-(1))
74=81-(9-(3-(1)))
75=81-(9-(3))
76=81-(9-(3+(1)))
77=81-(3+(1))
78=81-(3)
79=81-(3-(1))
80=81-(1)
81=81
82=81+(1)
83=81+(3-(1))
84=81+(3)
85=81+(3+(1))
86=81+(9-(3+(1)))
87=81+(9-(3))
88=81+(9-(3-(1)))
89=81+(9-(1))
90=81+(9)
91=81+(9+(1))
92=81+(9+(3-(1)))
93=81+(9+(3))
94=81+(9+(3+(1)))
95=81+(27-(9+(3+(1))))
96=81+(27-(9+(3)))
97=81+(27-(9+(3-(1))))
98=81+(27-(9+(1)))
99=81+(27-(9))
100=81+(27-(9-(1)))
101=81+(27-(9-(3-(1))))
102=81+(27-(9-(3)))
103=81+(27-(9-(3+(1))))
104=81+(27-(3+(1)))
105=81+(27-(3))
106=81+(27-(3-(1)))
107=81+(27-(1))
108=81+(27)
109=81+(27+(1))
110=81+(27+(3-(1)))
111=81+(27+(3))
112=81+(27+(3+(1)))
113=81+(27+(9-(3+(1))))
114=81+(27+(9-(3)))
115=81+(27+(9-(3-(1))))
116=81+(27+(9-(1)))
117=81+(27+(9))
118=81+(27+(9+(1)))
119=81+(27+(9+(3-(1))))
120=81+(27+(9+(3)))
121=81+(27+(9+(3+(1))))
122=2+(81+(27+(9+(3))))
123=2+(81+(27+(9+(3+(1)))))
flyforlove 2004-12-10
  • 打赏
  • 举报
回复
下面这个程序可以计算任何总克数砝码的组合。


import java.util.ArrayList;
public class poise {
private int count;
private ArrayList A=new ArrayList();
private ArrayList B=new ArrayList();
poise(int count) {
if (count > 0) {
this.count = count;
}
}
public void prtGroup(){
int Left,Inserted,temp;
Left=count;
Inserted=0;
temp=Inserted*2+1;
int index=0;
int i;
while(Left>0){
temp=Math.min(temp,Left);
A.add(index,new Integer(temp));
for(i=Inserted+1;i<=Inserted+temp;i++){
if(i<temp){
B.add(i-1,new String(temp+"-("+B.get(temp-i-1)+")"));
}else if(i==temp){
B.add(i-1,Integer.toString(temp));
}else if(i>temp){
B.add(i-1,new String(temp+"+("+B.get(i-temp-1)+")"));
}
}
Inserted=Inserted+temp;
Left=Left-temp;
temp=Inserted*2+1;
index++;
}
for(i=0;i<A.size();i++){
System.out.println(A.get(i));
}
for(i=0;i<B.size();i++){
System.out.println((i+1)+"="+B.get(i));
}
}
public static void main(String[] args) {
int count=123;
poise PS=new poise(count);
PS.prtGroup();
}
}
tiger_nugget 2004-12-10
  • 打赏
  • 举报
回复
如果是我来想就是这个样子:
(1)一个砝码重量生成器:生成4个砝码各个重量的所有组合。
(2)一个验证程序:验证此砝码组合是否符合标准。
生成从1到40克的物体重量,每一种物体重量验证一次是否可行。
砝码的使用方法只有两种:1.物体重量=砝码重量;2.砝码重量[左]+物体重量=砝码重量[右]。
第一种情况的所有组合:从4个砝码里面任意选取出1个砝码,或者2个,或者3个,或者4个。
依次套用第一种情况公式,有一次成功则符合,若否则看第二种情况;
第二种情况的所有组合:从4个砝码里面任意取出1个砝码,或者2个,或者3个,或者4个,总重量赋给砝码重量[左];从剩下的4个砝码里面任意取出1个砝码,或者2个,或者3个(小于剩余砝码总数),赋给砝码重量[右],依次套用第二种情况公式,有依次成功则符合,若否则不符合,换下一个砝码重量组合。
(3)显示通过验证程序的砝码组合。
不知道大家的算法的复杂度有多少。能不能在程序后面都加个复杂度。
virgin_killer 2004-12-10
  • 打赏
  • 举报
回复
System.currentTimeMillis()
返回一个long.
你在程序首位和末位转入,作一个减法就出来了
bigpool 2004-12-10
  • 打赏
  • 举报
回复
我没考虑这么多
怎么算时间的?
bigpool 2004-12-09
  • 打赏
  • 举报
回复
import java.util.HashSet;
import java.util.Set;

public class Balance {
private int a,b,c,d;
private static Set set;

public Balance(int weight){
set=new HashSet();
//System.out.print(set.size());
for(a=1;a<20;a++){
for(b=1;b<38;b++){
for(c=1;c<38;c++){
d=weight-a-b-c;
if(!set.isEmpty()) set.clear();
if(a>0&&b>0&&c>0&&d>0)
calculate(a,b,c,d,weight);
}
}
}
}

private void calculate(int a,int b,int c,int d,int weight){
//for 1 element
set.add(new Integer(a));
set.add(new Integer(b));
set.add(new Integer(c));
set.add(new Integer(d));

//for 2 elements
cal2ele(a,b);
cal2ele(a,c);
cal2ele(a,d);
cal2ele(b,c);
cal2ele(b,d);
cal2ele(c,d);

//for 3 elements
cal3ele(a,b,c);
cal3ele(a,b,d);
cal3ele(a,c,d);
cal3ele(d,b,c);

//for 4 elements
cal4ele(a,b,c,d);

if(a==1&&b==3&&c==9&&d==27)
System.out.println(set.size());
if(set.size()==weight){
System.out.print("a="+a+", ");
System.out.print("b="+b+", ");
System.out.print("c="+c+", ");
System.out.println("d="+d);
}
}

private void cal2ele(int i1,int i2){
int i=i1-i2;
if(i<0) set.add(new Integer(0-i));
else set.add(new Integer(i));

set.add(new Integer(i1+i2));
}

private void cal3ele(int i1,int i2,int i3){
set.add(new Integer(i1+i2+i3));

int i=i1+i2-i3;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1-i2-i3;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1-i2+i3;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));
}

private void cal4ele(int i1,int i2,int i3,int i4){
set.add(new Integer(i1+i2+i3+i4));

int i=i1-i2-i3-i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1+i2-i3-i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1+i2+i3-i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1-i2+i3-i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1-i2+i3+i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1+i2-i3+i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));

i=i1-i2-i3+i4;
if(i>0) set.add(new Integer(i));
else set.add(new Integer(0-i));
}

public static void main(String[] args) {
Balance a=new Balance(40);
}
}

会有重复的结果出来,刚写的,不知道对不对,帮忙测一下吧
murphy008 2004-12-09
  • 打赏
  • 举报
回复
我晕~~~这么多代码?数学问题!
FGhost33 2004-12-09
  • 打赏
  • 举报
回复
呵呵。up
gks_cn 2004-12-09
  • 打赏
  • 举报
回复
有意思
virgin_killer 2004-12-09
  • 打赏
  • 举报
回复
不好意思,刚刚发错了
public class FindFama
{
int a ;
int b;
int c;
int d;
int num = 27;
public FindFama()
{
for(a = 0; a < num; a++)
for(b = 0; b < num; b++)
for(c = 0; c < num; c++)
{
d = num - a - b - c;
if(d > 0)
if(find(a,b,c,d))
{
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
else{}
else
break;
}
System.out.println("end");
}
public boolean find(int a,int b, int c,int d)
{
for ( int weight = 1; weight < num + 1 ; weight++)
{
if( oneFama(a,b,c,d,weight)||twoFama(a,b,c,d,weight)
||threeFama(a,b,c,d,weight)||fourFama(a,b,c,d,weight)
){}
else
return false;

}
return true;
}
public boolean oneFama (int a, int b, int c, int d,int weight)
{
if( weight == a || weight == b || weight == c|| weight == d)
return true;
else
return false;
}
public boolean twoFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b || weight == a + c ||
weight == a + d || weight == b + c ||
weight == b + d ||
weight == c + d || weight + a == b ||
weight + a == c ||
weight + a == d || weight + b == a ||
weight + b == c || weight + b == d ||
weight + c == a || weight + c == b ||
weight + c == d || weight + d == a ||
weight + d == b || weight + d == c
)
return true;
else
return false;
}
public boolean threeFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c || weight + a == b + c ||
weight + a == b + d || weight + a == c + d ||
weight + b == a + c || weight + b == a + d ||
weight + b == c + d || weight + c == a + b ||
weight + c == a + d || weight + c == b + d ||
weight + d == a + b || weight + d == a + c ||
weight + d == b + c || weight + b + c == a ||
weight + b + d == a || weight + c + d == a ||
weight + a + c == b || weight + a + d == b ||
weight + c + d == b || weight + a + b == c ||
weight + a + d == c || weight + b + d == c ||
weight + a + b == d || weight + a + c == d ||
weight + b + c == d )
return true;
else
return false;
}
public boolean fourFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c + d || weight + a == b + c + d ||
weight + b == a + c + d || weight + c == a + b + d ||
weight + b + c + d == a || weight + a + c + d == b ||
weight + a + b + d == c || weight + a + b + c == d ||
weight + d == a + b + c || weight + a + b == c + d ||
weight + c + d == a + b || weight + a + c == b + d ||
weight + b + d == a + c || weight + a + d == b + c ||
weight + b + c == a + d
)
return true;
else
return false;
}

public static void main(String[] args)
{
new FindFama();

}
}
virgin_killer 2004-12-09
  • 打赏
  • 举报
回复
楼主,你40克,不能找到答案吧,我的程序计算出超过27克就没有答案了
public class FindFama
{
int a ;
int b;
int c;
int d;
int num = 40;
public FindFama()
{
for(a = 0; a < num; a++)
for(b = 0; b < num; b++)
for(c = 0; c < num; c++)
{
d = num - a - b - c;
a = 1; b = 5; c = 16; d = 18;
if(d > 0)
if(find(a,b,c,d))
{
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
else{}
else
break;
}
System.out.println("end");
}
public boolean find(int a,int b, int c,int d)
{
for ( int weight = 1; weight < num + 1 ; weight++)
{
if( oneFama(a,b,c,d,weight)||twoFama(a,b,c,d,weight)
||threeFama(a,b,c,d,weight)||fourFama(a,b,c,d,weight)
){}
else
return false;

}
return true;
}
public boolean oneFama (int a, int b, int c, int d,int weight)
{
if( weight == a || weight == b || weight == c|| weight == d)
return true;
else
return false;
}
public boolean twoFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b || weight == a + c ||
weight == a + d || weight == b + c ||
weight == b + d ||
weight == c + d || weight + a == b ||
weight + a == c ||
weight + a == d || weight + b == a ||
weight + b == c || weight + b == d ||
weight + c == a || weight + c == b ||
weight + c == d || weight + d == a ||
weight + d == b || weight + d == c
)
return true;
else
return false;
}
public boolean threeFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c || weight + a == b + c ||
weight + a == b + d || weight + a == c + d ||
weight + b == a + c || weight + b == a + d ||
weight + b == c + d || weight + c == a + b ||
weight + c == a + d || weight + c == b + d ||
weight + d == a + b || weight + d == a + c ||
weight + d == b + c || weight + b + c == a ||
weight + b + d == a || weight + c + d == a ||
weight + a + c == b || weight + a + d == b ||
weight + c + d == b || weight + a + b == c ||
weight + a + d == c || weight + b + d == c ||
weight + a + b == d || weight + a + c == d ||
weight + b + c == d )
return true;
else
return false;
}
public boolean fourFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c + d || weight + a == b + c + d ||
weight + b == a + c + d || weight + c == a + b + d ||
weight + b + c + d == a || weight + a + c + d == b ||
weight + a + b + d == c || weight + a + b + c == d ||
weight + d == a + b + c || weight + a + b == c + d ||
weight + c + d == a + b || weight + a + c == b + d ||
weight + b + d == a + c || weight + a + d == b + c ||
weight + b + c == a + d
)
return true;
else
return false;
}

public static void main(String[] args)
{
new FindFama();

}
}
virgin_killer 2004-12-09
  • 打赏
  • 举报
回复
回复人: xiaxuan92021(轩辉) ( ) 信誉:100
=============================================
你看看你程序的结果中,有没有能测9克的,我经过人工计算没有!
加载更多回复(8)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧