问题:有1根27厘米的细木杆,在第3厘米、7厘米、11厘米、18厘米、23厘米这5个位置上各有1只蚂蚁。木杆很细,不能同时通过两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假定蚂蚁们每秒钟可以走1厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
思路:由于没有告知蚂蚁的初始朝向,所以要对初始化蚂蚁朝向。用0表示朝左,1表示朝右,用2进制00000(0)表示5只蚂蚁都是朝向左,然后每次加1,直到加到11111(31)时,表示5只蚂蚁初始的所有朝向。让后随时间的推移,有些蚂蚁可能碰头,这是就的对蚂蚁的朝向就行修改,即:如原来朝向为0,则变成1,原来为1的变成0,。做到这里就会想到如何判断蚂蚁朝向问题,这个可以通过异或来解决问题。如:当要判断第1只蚂蚁朝向时,可以用2进制10000(16)与原来朝向异或,若结果为0,则表示朝向为1,朝右,否则相反;用2进制01000(8)与原来朝向异或,可以判断第2个蚂蚁的朝向,以此类推。
代码实现:
public class Ant{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("输入5只蚂蚁的位置(厘米)(大于0小于27且不能有相同的):");
while(sc.hasNext()){
int[] q=new int[5];//{3,7,11,18,23};输入5只蚂蚁的初始位置
for(int b=0;b<q.length;b++){
q[b]=sc.nextInt();
}
int maxtime=⑴,mintime=10000,signmax=0,signmin=0;
for(int num=0;num<=31;num++){//5只蚂蚁初始的朝向
int i=num;//1种初始朝向后,直到所有蚂蚁掉下的进程中中途朝向的变化
int[] p=new int[5];//{3,7,11,18,23};
for(int b=0;b<p.length;b++){//保存q[]的初值
p[b]=q[b];
}
int sum=0;//所有蚂蚁掉下去的时间
while(1>0){
if(f(p)==p.length){//直到5只蚂蚁都掉下去时,退出循环,进行下1种5只蚂蚁初始朝向的计算
break;
}
sum++;
for(int j=0;j<p.length;j++){//通过判断朝向 对位置进行加减
int k=(int)Math.pow(2,j);
if((i&k)==0){
p[j]-=1;
}else{
p[j]+=1;
}
}
for(int j=0;j<p.length⑴;j++){//判断有无碰头 然后重新记录5只蚂蚁的朝向
if(p[j]==p[j+1]){
int k=(int)Math.pow(2,j);
if((i&k)==0){
i-=k;
}else{
i+=k;
}
}
}
}
if(sum>maxtime){
maxtime=sum;signmax=num;
}
if(sum<mintime){
mintime=sum;signmin=num;
}
//System.out.print(sum+" ");
}
System.out.println("maxtime= "+maxtime+" 秒 ");
System.out.println("mintime= "+mintime+" 秒
");
System.out.println("输入5只蚂蚁的位置(厘米)(大于0小于27且不能有相同的):");
}
}
public static int f(int a[]){//记录蚂蚁掉下去的个数
int k=0;
for(int i=0;i<a.length;i++){
if(a[i]>=27||a[i]<=0){
k++;
}
}
return k;
}
}
波比源码 » 蚂蚁过杆问题
order levaquin 250mg without prescription buy levofloxacin generic
order avodart 0.5mg pills cost avodart purchase zofran for sale
order spironolactone 25mg for sale buy finasteride 5mg pill order generic diflucan 100mg
order ampicillin 250mg for sale order erythromycin 500mg for sale order generic erythromycin
order sildenafil 50mg cost tamoxifen 20mg methocarbamol canada
sildenafil order online buy estrace 1mg online cheap estradiol brand
order lamotrigine 200mg generic buy prazosin 2mg for sale generic retin
isotretinoin cheap amoxicillin 250mg cheap order azithromycin without prescription
buy indocin terbinafine sale oral amoxicillin 250mg
cialis 5mg pas cher vrai cialis 10mg prix acheter 25mg viagra en ligne
order accutane 10mg generic buy azithromycin generic ivermectin 1 cream generic
buy modafinil 100mg without prescription order modafinil 200mg order diamox 250 mg generic
doxycycline buy online buy generic furosemide buy lasix sale
brand altace order clobetasol online buy astelin 10ml sprayer
clonidine order online brand spiriva 9mcg spiriva 9 mcg pill
buy buspar 5mg online cheap buspar tablet ditropan for sale
buy generic terazosin purchase pioglitazone online cheap order sulfasalazine 500 mg sale
alendronate 70mg generic brand motrin buy pepcid
order zyban 150mg generic buy quetiapine 50mg online cheap quetiapine 50mg tablet
tadalafil 5mg sale Best cialis price sildenafil overnight
revia online order aripiprazole us buy aripiprazole for sale
luvox online buy glucotrol pills glipizide 5mg for sale
buy tadalafil 40mg sale sildenafil canada viagra online buy
purchase lasix for sale doxycycline 100mg uk plaquenil brand
order chloroquine 250mg pill order cenforce 50mg generic order olumiant 4mg for sale
lopressor for sale online purchase metoprolol generic order levitra 20mg sale
clomiphene 50mg ca order clomid generic online slot machines real money
cialis 5mg without prescription cost sildenafil 100mg viagra on line
buy aristocort 4mg sale triamcinolone 10mg brand order desloratadine generic
priligy 90mg cost levothyroxine usa buy synthroid 100mcg
order orlistat 60mg without prescription acyclovir cost brand acyclovir
cheap tadalafil 20mg buy cialis 20mg online plavix us
order allopurinol 300mg sale zetia online zetia uk
cheap methotrexate 5mg buy reglan pills purchase metoclopramide online
buy ranitidine 150mg pill celecoxib ca order celebrex 200mg online cheap
online casino games real money free slot games no download roulette game
order tamsulosin 0.4mg for sale purchase flomax online cost spironolactone
buy tadalafil 10mg for sale goodrx cialis purchase cipro online cheap
buy simvastatin 20mg pill order simvastatin 10mg without prescription purchase proscar online cheap
buy metronidazole 200mg sale clavulanate without prescription bactrim 960mg brand
purchase fluconazole generic fluconazole 100mg for sale buy generic sildenafil 100mg
buy viagra 100mg pills order viagra 100mg cialis next day
live blackjack online casino games order cialis 5mg
desyrel over the counter buy trazodone 50mg sale sildenafil 50mg usa
best college paper writing service good academic writing ivermectin ebay
sildalis price sildenafil 100mg price lamotrigine pill
prednisone 10mg ca amoxil 250mg canada buy amoxicillin for sale
zithromax 500mg sale neurontin 800mg cheap buy gabapentin 600mg without prescription
best online casinos that payout real casino online cheap cialis generic
gambling games buy stromectol 12mg generic provigil 200mg over the counter
purchase lasix online cheap cheap hydroxychloroquine 400mg order plaquenil 400mg for sale
sildenafil cost brand nolvadex 20mg buy rhinocort online cheap
buy generic tretinoin avana 200mg over the counter generic avanafil 100mg
order generic tadalafil 10mg order tadalafil sale order indocin for sale
lamisil drug order cefixime online cheap amoxicillin brand
naprosyn 500mg over the counter order naprosyn 500mg without prescription lansoprazole 15mg canada
order clarithromycin 250mg cheap meclizine antivert pills
order actos 15mg without prescription viagra sildenafil 50mg cheap viagra sale
buy altace pill oral etoricoxib buy etoricoxib 120mg sale
doxycycline ca doxycycline 100mg without prescription cleocin 150mg cheap
pay for assignments australia azulfidine buy online azulfidine without prescription
acetazolamide canada order diamox 250mg pills order generic azathioprine
buy lanoxin sale molnupiravir 200mg cost molnunat 200mg price
fosamax 35mg without prescription cost ibuprofen 400mg brand motrin 400mg
buy dapoxetine 30mg pill purchase priligy generic order domperidone 10mg online cheap
Nice article inspiring thanks. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
nortriptyline 25mg usa order paxil 10mg generic order paxil sale
ropinirole 1mg oral buy rocaltrol 0.25 mg online order trandate 100mg
tadacip 20mg uk order trimox 250mg pills trimox online buy
tadalafil 10mg drug Cialis online us cost sildenafil
provigil ca purchase phenergan pill promethazine 25mg pills
clomiphene us brand lipitor 10mg purchase prednisolone for sale
fda approved over the counter ed pills lyrica medication propecia buy online
order zofran for sale buy ondansetron without prescription purchase bactrim
isotretinoin generic cheap generic amoxicillin azithromycin drug
cheap prednisolone for sale gabapentin 600mg us buy lasix 100mg for sale
sildenafil india cialis medication tadalafil 20mg usa
levofloxacin online order oral bupropion 150mg zyban 150mg without prescription
generic zyrtec 5mg cetirizine brand buy generic sertraline
cenforce 100mg oral aralen ca purchase glucophage without prescription
escitalopram 20mg over the counter revia cost generic revia
purchase atorvastatin online cheap viagra 50mg cost buy viagra pill
purchase letrozole online cheap buy letrozole 2.5mg viagra fast shipping
order tadalafil 10mg for sale cialis 20mg without prescription cheap erectile dysfunction pills online
provigil 200mg over the counter phenergan where to buy deltasone 20mg price
amoxil uk buy azithromycin 250mg sale prednisolone 40mg canada
purchase accutane online purchase absorica online how to get zithromax without a prescription
buy gabapentin pills for sale doxycycline cost purchase doxycycline generic
generic prednisolone 40mg prednisolone without prescription buy lasix
serophene brand clomid buy online buy plaquenil online
tenormin 100mg brand buy generic letrozole over the counter letrozole over the counter
praziquantel 600mg canada buy microzide 25mg generic cyproheptadine usa
order methotrexate 2.5mg online cheap buy generic reglan 20mg buy reglan 20mg generic
xenical usa order orlistat 120mg generic buy allopurinol 100mg for sale
rosuvastatin 20mg oral order zetia online domperidone 10mg usa
order generic tetracycline 250mg baclofen 25mg pills buy lioresal generic
buy cheap ketorolac colcrys brand order inderal for sale
buy ranitidine online cheap buy celecoxib pills for sale celebrex 100mg cost
where to buy tamsulosin without a prescription purchase aldactone spironolactone 100mg drug
buy generic ipratropium linezolid 600mg us order zyvox for sale
nebivolol cheap order generic bystolic 5mg clozapine 100mg over the counter
starlix 120mg us captopril uk order candesartan 16mg pills
simvastatin 10mg sale cheap sildenafil sildenafil next day delivery
carbamazepine online buy ciprofloxacin uk lincocin online
best cialis sites online cialis 10mg sildenafil cheap
purchase duricef pill order proscar without prescription buy finasteride 1mg generic
buy estradiol no prescription buy generic lamictal online prazosin 1mg canada
order diflucan online cheap ciprofloxacin 1000mg tablet ciprofloxacin cheap
mebendazole cost tadalis ca brand tadalafil 20mg
metronidazole 400mg drug cephalexin pill cephalexin over the counter
buy indomethacin lamisil pills order suprax 200mg without prescription
nolvadex 10mg price buy budesonide tablets buy ceftin for sale
amoxicillin price biaxin 500mg cost order biaxin online
buy clonidine 0.1mg for sale catapres 0.1mg generic order spiriva 9mcg sale
isotretinoin for sale amoxil 250mg us zithromax medication
tadalafil us otc cialis cialis 40mg without prescription
cheap furosemide 40mg buy doxycycline sale ventolin inhalator sale
buy carvedilol 6.25mg online cheap cenforce 50mg cheap chloroquine cheap