當前位置:首頁 » 操作系統 » born演算法

born演算法

發布時間: 2023-06-15 09:49:00

① C語言編程:母牛的問題

#include<stdio.h>
#define Max 20//0記做第一年(2頭牛),20記做第二十年
void main()
{

int sum=1,i,year,a[100]={0};//sum記錄總數,a[]儲存每頭牛的存活年數
for(year=0;year<=Max;year=year+4)//年數按4年遞增
{
//此循環目的是悶升查看每頭牛的年齡,並作出判斷操作
for(i=sum-1;i>=0;i--)//因為數組從0開始所以sum要減一
{
a[i]+=4;//每頭牛成長4歲
if(a[i]>15&&a[i]<20)//表示在這一年死過的牛
sum-=1;//總數減一

}
sum=2*sum;//再生一批牛
}
printf("%d\n",sum);
}
第16年才是26頭李汪,第20年應為48頭,而不是50頭,前面程序有算得50頭的,應該不正確,請再檢查演算法
思路呢就是:
比如說螞擾老第一年有兩頭牛,先讓他們的年齡+4,然後查看是否死掉,死掉的從總數中減去再乘以2,依次類推

② 我想知道隊列演算法能幹什麼

隊列是一種先進先出的數據結構,由於這一規則的限制,使得隊列有區別於棧等別的數據結構。
作為一種常用的數據結構,同棧一樣,是有著豐富的現實背景的。以下是幾個典型的例子。
[例5-2] 一個旅行家想駕駛汽車以最少的費用從一個城市到另一個城市(假設出發時油箱是空的).給定兩個城市之間的距離D1,汽車油箱的容量C(以升為單位),每升汽油能行駛的距離D2,出發點每升汽油價格P和沿途油站數N(N可以為零),油站i離出發點的距離Di,每升汽油價格Pi(i=1,2,……N).
計算結果四捨五入至小數點後兩位.
如果無法到達目的地,則輸出"No Solution".
樣例:
INPUT
D1=275.6 C=11.9 D2=27.4 P=2.8 N=2
油站號I
離出發點的距離Di
每升汽油價格Pi
1
102.0
2.9
2
220.0
2.2
OUTPUT
26.95(該數據表示最小費用)
[問題分析]
看到這道題,許多人都馬上判斷出窮舉是不可行的,因為數據都是以實數的形式給出的.但是,不用窮舉,有什麼方法是更好的呢 遞推是另一條常見的思路,但是具體方法不甚明朗.
既然沒有現成的思路可循,那麼先分析一下問題不失為一個好辦法.由於汽車是由始向終單向開的,我們最大的麻煩就是無法預知汽車以後對汽油的需求及油價變動;換句話說,前面所買的多餘的油只有開到後面才會被發覺.
提出問題是解決的開始.為了著手解決遇到的困難,取得最優方案,那就必須做到兩點,即只為用過的汽油付錢;並且只買最便宜的油.如果在以後的行程中發現先前的某些油是不必要的,或是買貴了,我們就會說:"還不如當初不買."由這一個想法,我們可以得到某種啟示:假設我們在每個站都買了足夠多的油,然後在行程中逐步發現哪些油是不必要的,以此修改我們先前的購買計劃,節省資金;進一步說,如果把在各個站加上的油標記為不同的類別,我們只要在用時用那些最便宜的油並為它們付錢,其餘的油要麼是太貴,要麼是多餘的,在最終的計劃中會被排除.要注意的是,這里的便宜是對於某一段路程而言的,而不是全程.
[演算法設計]由此,我們得到如下演算法:從起點起(包括起點),每到一個站都把油箱加滿(終點除外);每經過兩站之間的距離,都按照從便宜到貴的順序使用油箱中的油,並計算花費,因為這是在最優方案下不得不用的油;如果當前站的油價低於油箱中仍保存的油價,則說明以前的購買是不夠明智的,其效果一定不如購買當前加油站的油,所以,明智的選擇是用本站的油代替以前購買的高價油,留待以後使用,由於我們不是真的開車,也沒有為備用的油付過錢,因而這樣的反悔是可行的;當我們開到終點時,意味著路上的費用已經得到,此時剩餘的油就沒有用了,可以忽略.
數據結構採用一個隊列:存放由便宜到貴的各種油,一個頭指針指向當前應當使用的油(最便宜的油),尾指針指向當前可能被替換的油(最貴的油).在一路用一路補充的過程中同步修改數據,求得最優方案.
注意:每到一站都要將油加滿,以確保在有解的情況下能走完全程.並假設出發前油箱里裝滿了比出發點貴的油,將出發點也看成一站,則程序循環執行換油,用油的操作,直到到達終點站為止.
本題的一個難點在於認識到油箱中油的可更換性,在這里,突破現實生活中的思維模式顯得十分重要.
[程序清單]
program ex5_2(input,output);
const max=1000;
type recordtype=record price,content:real end;
var i,j,n,point,tail:longint;
content,change,distance2,money,use:real;
price,distance,consume:array[0..max] of real;
oil:array [0..max] of recordtype;
begin
write('Input DI,C,D2,P:'); readln(distance[0],content,distance2,price[0]);
write('Input N:'); readln(n); distance[n+1]:=distance[0];
for i:=1 to n do
begin
write('Input D[',i,'],','P[',i,']:');
readln(distance[i],price[i])
end;
distance[0]:=0;
for i:=n downto 0 do consume[i]:=(distance[i+1]-distance[i])/distance2;
for i:=0 to n do
if consume[i]>content then
begin writeln('No Solution'); halt end;
money:=0; tail:=1; change:=0;
oil[tail].price:=price[0]*2; oil[tail].content:=content;
for i:=0 to n do
begin
point:=tail;
while (point>=1) and (oil[point].price>=price[i]) do
begin
change:=change+oil[point].content;
point:=point-1
end;
tail:=point+1;
oil[tail].price:=price[i];
oil[tail].content:=change;
use:=consume[i]; point:=1;
while (use>1e-6) and (point=oil[point].content
then begin use:=use-oil[point].content;
money:=money+oil[point].content*oil[point].price;
point:=point+1 end
else begin oil[point].content:=oil[point].content-use;
money:=money+use*oil[point].price;
use:=0 end;
for j:=point to tail do oil[j-point+1]:=oil[j];
tail:=tail-point+1;
change:=consume[i]
end;
writeln(money:0:2)
end.
[例5-3] 分油問題:設有大小不等的3個無刻度的油桶,分別能夠存滿,X,Y,Z公升油(例如X=80,Y=50,Z=30).初始時,第一個油桶盛滿油,第二,三個油桶為空.編程尋找一種最少步驟的分油方式,在某一個油桶上分出targ升油(例如targ=40).若找到解,則將分油方法列印出來;否則列印信息"UNABLE"等字樣,表示問題無解.
[問題分析] 這是一個利用隊列方法解決分油問題的程序.分油過程中,由於油桶上沒有刻度,只能將油桶倒滿或者倒空.三個油桶盛滿油的總量始終等於開始時的第一個油桶盛滿的油量.
[演算法設計] 分油程序的演算法主要是,每次判斷當前油桶是不是可以倒出油,以及其他某個油桶是不是可以倒進油.如果滿足以上條件,那麼當前油桶的油或全部倒出,或將另一油桶倒滿,針對兩種不同的情況作不同的處理.
程序中使用一個隊列Q,記錄每次分油時各個油桶的盛油量和傾倒軌跡有關信息,隊列中只記錄互不相同的盛油狀態(各個油桶的盛油量),如果程序列舉出倒油過程的所有不同的盛油狀態,經考察全部狀態後,未能分出TARG升油的情況,就確定這個倒油問題無解.隊列Q通過指針front和rear實現倒油過程的控制.
[程序清單]
program ex5_3(input,output);
const maxn=5000;
type stationtype=array[1..3] of integer;
elementtype=record
station:stationtype;
out,into:1..3;
father:integer
end;
queuetype=array [1..maxn] of elementtype;
var current,born:elementtype;
q:queuetype;
full,w,w1:stationtype;
i,j,k,remain,targ,front,rear:integer;
found:boolean;
procere addQ(var Q:queuetype;var rear:integer; n:integer; x:elementtype);
begin
if rear=n
then begin writeln('Queue full!'); halt end
else begin rear:=rear+1; Q[rear]:=x end
end;
procere deleteQ(var Q:queuetype;var front:integer;rear,n:integer;var x:elementtype);
begin
if front=rear
then begin writeln('Queue empty!'); halt end
else begin front:=front+1; x:=Q[front] end
end;
function p(w:stationtype;rear:integer):boolean;
var i:integer;
begin
i:=1;
while (i<=rear) and ((w[1]q[i].station[1]) or
(w[2]q[i].station[2]) or (w[3]q[i].station[3])) do i:=i+1;
if i0 then
begin
print(q[k].father);
if k>1 then write(q[k].out, ' TO ',q[k].into,' ')
else write(' ':8);
for i:=1 to 3 do write(q[k].station[i]:5);
writeln
end
end;
begin {Main program}
writeln('1: ','2: ','3: ','targ');
readln(full[1],full[2],full[3],targ);
found:=false;
front:=0; rear:=1;
q[1].station[1]:=full[1];
q[1].station[2]:=0;
q[1].station[3]:=0;
q[1].father:=0;
while (front begin
deleteQ(q,front,rear,maxn,current);
w:=current.station;
for i:=1 to 3 do
for j:=1 to 3 do
if (ij) and (w[i]>0) and (w[j]remain
then begin w1[j]:=full[j]; w1[i]:=w[i]-remain end
else begin w1[i]:=0; w1[j]:=w[j]+w[i] end;
if not(p(w1,rear)) then
begin
born.station:=w1;
born.out:=i;
born.into:=j;
born.father:=front;
addQ(q,rear,maxn,born);
for k:=1 to 3 do
if w1[k]=targ then found:=true
end
end
end;
if not(found)
then writeln('Unable!')
else print(rear)
end.

③ 希爾排序演算法

希爾排序(Shell Sort)是插入排序的一種。因D.L.Shell於1959年提出而得名。

希爾排序基本思想

基本思想:

先取一個小於n的整數d1作為第一個增量,把文件的全部記錄分成d1個組。所有距離為dl的倍數的記錄放在同一個組中。先在各組內進行直接插入排序;然後,取第二個增量d2<d1重復上述的分組和排序,直至所取的增量dt=1(dt<dt-l<…<d2<d1),即所有記錄放在同一組中進行直接插入排序為止。

詳細資料:http://bk..com/view/178698.htm

java計算年齡

import java.util.Calendar;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;

public class H {
public static void main(String args[]) {
new Time("年齡計算器");

}
}

class Time extends Frame implements ActionListener {
Calendar calendar;
Button button;
TextField t1, t2, t3;
Label l, l1, l2, l3;

Time(String s) {
super(s);
setLayout(new FlowLayout());
button = new Button("確定");
button.addActionListener(this);
t1 = new TextField(2);
t2 = new TextField(2);
t3 = new TextField(2);
l = new Label(" 請輸入您的生日 ");
l.setBackground(Color.cyan);
l1 = new Label("年");
l2 = new Label("月");
l3 = new Label("日");
add(l);
add(t1);
add(l1);
add(t2);
add(l2);
add(t3);
add(l3);
add(button);
setBounds(100, 100, 280, 100);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
validate();
}

public void actionPerformed(ActionEvent e) {
calendar = Calendar.getInstance();
calendar.setTime(new Date());
NumberFormat f = NumberFormat.getInstance();
long time = calendar.getTimeInMillis();
if (e.getSource() == button) {
try {
int n = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int r = Integer.parseInt(t3.getText());
calendar.set(n, y - 1, r);
double time1 = calendar.getTimeInMillis();
double c = (time - time1) / (1000 * 60 * 60 * 24);
double d = c/365;
f.setMaximumFractionDigits(2);
String s = f.format(d);
l.setText("您的年齡約為" + s + " 歲");
} catch (NumberFormatException ee) {
l.setText("請正確輸入");
}
}
}
}

功底淺薄,如果有問題,還望指教。

熱點內容
正在連接外設伺服器是什麼意思 發布:2025-03-24 13:40:34 瀏覽:334
安卓怎麼模仿蘋果彈窗 發布:2025-03-24 13:33:47 瀏覽:17
游戲官網源碼 發布:2025-03-24 13:14:04 瀏覽:572
九游原神是什麼伺服器 發布:2025-03-24 13:12:32 瀏覽:271
伺服器可以用自己的電腦做嗎 發布:2025-03-24 13:11:09 瀏覽:4
python取進程pid 發布:2025-03-24 13:09:36 瀏覽:244
高質量c編程 發布:2025-03-24 13:07:33 瀏覽:236
輸送帶緩存 發布:2025-03-24 12:57:16 瀏覽:698
資源配置一般有哪些方式 發布:2025-03-24 12:54:13 瀏覽:262
領勢FTP 發布:2025-03-24 12:48:17 瀏覽:489