惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - Welfare

工作之余[续] A pretty story——登山的故事 ASCII表 推荐一款播放软件——KMPlayer SCJP认证考试<-->12-29[补] 微软主页改版,网易推出搜索 安装系统时出现的问题 比比谁的代码跑得更快!——Intel多核平台代码优化比赛 WEB2.0, 在线网络服务成为Internet时尚 [M$] .NET Framework 3.0及其相关产品正式发布 Sun Java 国际认证学生特惠计划 Imagine Cup 微软全球学生大赛[说明] 罗马数字 [推荐]程序员每天该做的事 - Welfare - 博客园 Lava-Lava即将开放源码 大整数运算[Java] 关于++与-- 上帝很公平 ——世界杯的感想 同济大学软件学院院长谈择业—关于嵌入式方向 [ZZ]
瓶子问题 [M$面试题]
Welfare · 2006-10-14 · via 博客园 - Welfare

Problem Set:  有12个瓶子, 其中有一个与其它瓶子重量不同.现在有一架天平,试用三次天平找出这个瓶子.

Prob Sloved: 我的思路是, 将12个瓶子分为四堆,选出一个不同重量的堆,然后从这个堆中,找出这个瓶子。

 1import java.util.*;
 2public class
 WeightBottle 
 3
{
 4    public static int selectBottle(int
 []array)
 5    
{
 6        int index =0
;
 7        int len =
 array.length;
 8        int []a = new int[4
];
 9        for(int i=0; i<4; i++
)
10        
{
11            for(int j=0; j<len; j++
)
12            
{
13                a[i] += array[j%3 + i*3
];            
14            }

15        }

16        
17        int numOfPart =0
;
18        for(int i=0; i<4; i++
)
19        
{
20            if(i+1<4 && a[i] != a[i+1
])
21            
{
22                numOfPart =
 i;
23                //System.out.println(a[i]+" "+a[i+1]);

24            }

25            //System.out.println(" "+a[i]);            
26        }

27        //找到哪一堆的瓶子
28        int count1 = 0;
29        int count2 = 0
;
30        for(int i=0; i<4; i++
)
31        
{
32            if(a[numOfPart] != a[i]) count1 ++
;
33            if(a[numOfPart+1!= a[i]) count2 ++
;
34            if(count1 ==3//则numOfPart堆的瓶子有问题

35            {
36                break
;
37            }

38            if(count2 ==3//则numOfPart堆的瓶子有问题
39            {
40                numOfPart = numOfPart+1
;
41                break
;
42            }

43        }

44        
45        for(int i=0; i<3; i++
)
46        
{
47            if(array[numOfPart*3+i%3!= array[numOfPart*3+i%3 +1
])
48            
{
49                index = numOfPart*3+i%3
;
50                break
;
51            }

52        }

53        if(index+1<12 && array[index]==array[index+2]) index = index +1;
54        else if(index+1==12 && array[index] == array[index -1]) index = index +1
;
55

56        return
 index;
57    }

58
59    public static void
 main(String[] args) 
60    
{
61        Random rand=new
 Random();
62        int randNum = Math.abs(rand.nextInt())%12+1
;
63

64        //随机产生12个瓶子的重量

65        int []array = new int [12];
66        for(int i=0; i<12; i++
)
67        
{
68            array[i] =
 randNum;
69        }

70        int n = Math.abs(rand.nextInt())%12;
71        array[n] = randNum*randNum+2
;
72        System.out.println("输出十二个瓶子的重量:"
);
73        for(int i=0; i<12; i++
)
74        
{
75            System.out.println((i+1)+""+
array[i]);
76        }

77        //找出这个不同的瓶子
78        int index = selectBottle(array);
79        System.out.println("这个瓶子是第"+(index+1)+"个!"
);
80    }

81}

82