-
實(shí)現(xiàn)背包問題
package problem
1. 問題描述
假設(shè)有一個(gè)能裝入總體積為T的背包和n件體積分別為w1 , w2 , … , wn 的物品,能否從n件物品中挑選若干件恰好裝滿背包,即使w1 +w2 + … + wn=T,要求找出所有滿足上述條件的解。例如:當(dāng)T=10,各件物品的體積{1,8,4,3,5,2}時(shí),可找到下列4組解: (1,4,3,2)、(1,4,5)、(8,2)、(3,5,2)。
2. 基本要求
讀入T、n、w1 , w2 , … , wn
3.提示:
可利用遞歸方法:若選中w1 則問題變成在w2 , … , wn 中挑選若干件使得其重量之和為T- w1 ,若不選中w1,則問題變成在w2 , … , wn 中挑選若干件使得其重量之和為T 。依次類推。
也可利用回溯法的設(shè)計(jì)思想來解決背包問題。首先將物品排成一列,然后順序選取物品裝入背包,假設(shè)已選取了前i 件物品之后背包還沒有裝滿,則繼續(xù)選取第i+1件物品,若該件物品“太大”不能裝入,則棄之而繼續(xù)選取下一件,直至背包裝滿為止。但如果在剩余的物品中找不到合適的物品以填滿背包,則說明“剛剛”裝入背包的那件物品“不合適”,應(yīng)將它取出“棄之一邊”,繼續(xù)再從“它之后”的物品中選取,如此重復(fù),,直至求得滿足條件的解,或者無解。
注:沒壓縮密碼
標(biāo)簽:
package
problem
體積
w2
上傳時(shí)間:
2014-01-18
上傳用戶:yxgi5
-
代入法的啟發(fā)示搜索
我的代碼實(shí)現(xiàn)是:按照自然語言各字母出現(xiàn)頻率的大小從高到低(已經(jīng)有人作國統(tǒng)計(jì)分析了)先生成一張字母出現(xiàn)頻率統(tǒng)計(jì)表(A)--------(e),(t,a,o,i,n,s,h,r),(d,l),(c,u,m,w,f,g,y,p,b),(v,k,j,x,q,z)
,再對密文字母計(jì)算頻率,并按頻率從高到低生成一張輸入密文字母的統(tǒng)計(jì)表(B),通過兩張表的對應(yīng)關(guān)系,不斷用A中的字母去替換B中的字母,搜索不成功時(shí)就回退,在這里回朔是一個(gè)關(guān)鍵。
標(biāo)簽:
字母
頻率
搜索
代碼
上傳時(shí)間:
2015-10-24
上傳用戶:wanqunsheng
-
最優(yōu)服務(wù)次序問題 問題描述: 設(shè)有n 個(gè)顧客同時(shí)等待一項(xiàng)服務(wù)。顧客i需要的服務(wù)時(shí)間為t(i),i=1,…,n 。...個(gè)顧客等待服務(wù)時(shí)間的 總和除以n。 編程任務(wù): 對于給定的n個(gè)顧客需要的服務(wù)時(shí)間,編程計(jì)算最優(yōu)服務(wù)次序。
標(biāo)簽:
服務(wù)
等待
編程
上傳時(shí)間:
2013-12-19
上傳用戶:epson850
-
Program to plot x(t) =1/2+∑_(n=1)^∞▒ (sinc 〖n/2〗 〖 cos 〗 〖2πnt/4〗 )
Include n-value (Number of Terms) in graph on plot.
Show 2 graphs, original and simulated together.
標(biāo)簽:
8289
Program
Include
9618
上傳時(shí)間:
2014-01-03
上傳用戶:tb_6877751
-
learningMatlab
PhÇ n 1
c¬ së Mat lab
Ch ¬ ng 1:
Cµ i ® Æ t matlab
1.1.Cµ i ® Æ t ch ¬ ng tr×nh:
Qui tr×nh cµ i ® Æ t Matlab còng t ¬ ng tù nh viÖ c cµ i ® Æ t c¸ c ch ¬ ng tr×nh phÇ n mÒ m kh¸ c, chØ cÇ n theo c¸ c h íng dÉ n vµ bæ xung thª m c¸ c th« ng sè cho phï hî p.
1.1.1 Khë i ® éng windows.
1.1.2 Do ch ¬ ng tr×nh ® î c cÊ u h×nh theo Autorun nª n khi g¾ n dÜ a CD vµ o æ ® Ü a th× ch ¬ ng tr×nh tù ho¹ t ® éng, cö a sæ
標(biāo)簽:
learningMatlab
172
199
173
上傳時(shí)間:
2013-12-20
上傳用戶:lanwei
-
metricmatlab
ch ¬ ng 4
Ma trË n - c¸ c phÐ p to¸ n vÒ ma trË n.
4.1 Kh¸ i niÖ m:
- Trong MATLAB d÷ liÖ u ® Ó ® a vµ o xö lý d íi d¹ ng ma trË n.
- Ma trË n A cã n hµ ng, m cét ® î c gä i lµ ma trË n cì n m. § î c ký hiÖ u An m
- PhÇ n tö aij cñ a ma trË n An m lµ phÇ n tö n» m ë hµ ng thø i, cét j .
- Ma trË n ® ¬ n ( sè ® ¬ n lÎ ) lµ ma trË n 1 hµ ng 1 cét.
- Ma trË n hµ ng ( 1 m ) sè liÖ u ® î c bè trÝ trª n mét hµ ng.
a11 a12 a13 ... a1m
- Ma trË n cét ( n 1) sè liÖ u ® î c bè trÝ trª n 1 cét.
標(biāo)簽:
metricmatlab
203
184
tr
上傳時(shí)間:
2017-07-29
上傳用戶:來茴
-
設(shè)信號 ,用 對x(t)采樣得x(n),是否會(huì)發(fā)生頻譜混疊?現(xiàn)利用FFT分析其頻譜。
1.編程繪制該信號的波形。
2.若令N=16,編程對x(n)做FFT運(yùn)算,并繪制其幅頻特性曲線。
3.令N=1024,編程對x(n)做FFT運(yùn)算,并繪制其幅頻特性曲線。
4.分析2、3的運(yùn)算結(jié)果。
設(shè)計(jì)調(diào)試報(bào)告要求:
1.工作原理簡述;
2.設(shè)計(jì)思路;
3.難點(diǎn)及解決方法;
4.設(shè)計(jì)、調(diào)試結(jié)果及分析;
5.程序文本及操作步驟。
標(biāo)簽:
信號
上傳時(shí)間:
2014-01-12
上傳用戶:集美慧
-
算法的許多例子都是最優(yōu)化問題( optimization problem),每個(gè)最優(yōu)化問題都包含一組限制條件( c o n s t r a i n t)和一個(gè)優(yōu)化函數(shù)( optimization function),符合限制條件的問題求解方案稱為可行解( feasible solution),使優(yōu)化函數(shù)取得最佳值的可行解稱為最優(yōu)解(optimal solution)。
標(biāo)簽:
optimization
problem
算法
上傳時(shí)間:
2014-08-25
上傳用戶:123456wh
-
希爾排序算法:
基本思想:將整個(gè)無序序列分割成若干小的子序列分別進(jìn)行插入排序。 序列分割方法:將相隔某個(gè)增量h的元素構(gòu)成一個(gè)子序列。在排序過程中,逐次減小這個(gè)增量,最后當(dāng)h減到1時(shí),進(jìn)行一次插入排序,排序就完成。增量序列一般采用:ht=2t-1,1≤t≤[log2n],其中n為待排序序列的長度。
標(biāo)簽:
序列
排序
排序算法
分割
上傳時(shí)間:
2013-12-19
上傳用戶:kikye
-
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
#define MaxSize 30
typedef struct athletestruct /*運(yùn)動(dòng)員*/
{
char name[20];
int score; /*分?jǐn)?shù)*/
int range; /**/
int item; /*項(xiàng)目*/
}ATH;
typedef struct schoolstruct /*學(xué)校*/
{
int count; /*編號*/
int serial; /**/
int menscore; /*男選手分?jǐn)?shù)*/
int womenscore; /*女選手分?jǐn)?shù)*/
int totalscore; /*總分*/
ATH athlete[MaxSize]; /**/
struct schoolstruct *next;
}SCH;
int nsc,msp,wsp;
int ntsp;
int i,j;
int overgame;
int serial,range;
int n;
SCH *head,*pfirst,*psecond;
int *phead=NULL,*pafirst=NULL,*pasecond=NULL;
void create();
void input ()
{
char answer;
head = (SCH *)malloc(sizeof(SCH)); /**/
head->next = NULL;
pfirst = head;
answer = 'y';
while ( answer == 'y' )
{
Is_Game_DoMain:
printf("\nGET Top 5 when odd\nGET Top 3 when even");
printf("\n輸入運(yùn)動(dòng)項(xiàng)目序號 (x<=%d):",ntsp);
scanf("%d",pafirst);
overgame = *pafirst;
if ( pafirst != phead )
{
for ( pasecond = phead ; pasecond < pafirst ; pasecond ++ )
{
if ( overgame == *pasecond )
{
printf("\n這個(gè)項(xiàng)目已經(jīng)存在請選擇其他的數(shù)字\n");
goto Is_Game_DoMain;
}
}
}
pafirst = pafirst + 1;
if ( overgame > ntsp )
{
printf("\n項(xiàng)目不存在");
printf("\n請重新輸入");
goto Is_Game_DoMain;
}
switch ( overgame%2 )
{
case 0: n = 3;break;
case 1: n = 5;break;
}
for ( i = 1 ; i <= n ; i++ )
{
Is_Serial_DoMain:
printf("\n輸入序號 of the NO.%d (0<x<=%d): ",i,nsc);
scanf("%d",&serial);
if ( serial > nsc )
{
printf("\n超過學(xué)校數(shù)目,請重新輸入");
goto Is_Serial_DoMain;
}
if ( head->next == NULL )
{
create();
}
psecond = head->next ;
while ( psecond != NULL )
{
if ( psecond->serial == serial )
{
pfirst = psecond;
pfirst->count = pfirst->count + 1;
goto Store_Data;
}
else
{
psecond = psecond->next;
}
}
create();
Store_Data:
pfirst->athlete[pfirst->count].item = overgame;
pfirst->athlete[pfirst->count].range = i;
pfirst->serial = serial;
printf("Input name:) : ");
scanf("%s",pfirst->athlete[pfirst->count].name);
}
printf("\n繼續(xù)輸入運(yùn)動(dòng)項(xiàng)目(y&n)?");
answer = getchar();
printf("\n");
}
}
void calculate() /**/
{
pfirst = head->next;
while ( pfirst->next != NULL )
{
for (i=1;i<=pfirst->count;i++)
{
if ( pfirst->athlete[i].item % 2 == 0 )
{
switch (pfirst->athlete[i].range)
{
case 1:pfirst->athlete[i].score = 5;break;
case 2:pfirst->athlete[i].score = 3;break;
case 3:pfirst->athlete[i].score = 2;break;
}
}
else
{
switch (pfirst->athlete[i].range)
{
case 1:pfirst->athlete[i].score = 7;break;
case 2:pfirst->athlete[i].score = 5;break;
case 3:pfirst->athlete[i].score = 3;break;
case 4:pfirst->athlete[i].score = 2;break;
case 5:pfirst->athlete[i].score = 1;break;
}
}
if ( pfirst->athlete[i].item <=msp )
{
pfirst->menscore = pfirst->menscore + pfirst->athlete[i].score;
}
else
{
pfirst->womenscore = pfirst->womenscore + pfirst->athlete[i].score;
}
}
pfirst->totalscore = pfirst->menscore + pfirst->womenscore;
pfirst = pfirst->next;
}
}
void output()
{
pfirst = head->next;
psecond = head->next;
while ( pfirst->next != NULL )
{
// clrscr();
printf("\n第%d號學(xué)校的結(jié)果成績:",pfirst->serial);
printf("\n\n項(xiàng)目的數(shù)目\t學(xué)校的名字\t分?jǐn)?shù)");
for (i=1;i<=ntsp;i++)
{
for (j=1;j<=pfirst->count;j++)
{
if ( pfirst->athlete[j].item == i )
{
printf("\n %d\t\t\t\t\t\t%s\n %d",i,pfirst->athlete[j].name,pfirst->athlete[j].score);break;
}
}
}
printf("\n\n\n\t\t\t\t\t\t按任意建 進(jìn)入下一頁");
getchar();
pfirst = pfirst->next;
}
// clrscr();
printf("\n運(yùn)動(dòng)會(huì)結(jié)果:\n\n學(xué)校編號\t男運(yùn)動(dòng)員成績\t女運(yùn)動(dòng)員成績\t總分");
pfirst = head->next;
while ( pfirst->next != NULL )
{
printf("\n %d\t\t %d\t\t %d\t\t %d",pfirst->serial,pfirst->menscore,pfirst->womenscore,pfirst->totalscore);
pfirst = pfirst->next;
}
printf("\n\n\n\t\t\t\t\t\t\t按任意建結(jié)束");
getchar();
}
void create()
{
pfirst = (struct schoolstruct *)malloc(sizeof(struct schoolstruct));
pfirst->next = head->next ;
head->next = pfirst ;
pfirst->count = 1;
pfirst->menscore = 0;
pfirst->womenscore = 0;
pfirst->totalscore = 0;
}
void Save()
{FILE *fp;
if((fp = fopen("school.dat","wb"))==NULL)
{printf("can't open school.dat\n");
fclose(fp);
return;
}
fwrite(pfirst,sizeof(SCH),10,fp);
fclose(fp);
printf("文件已經(jīng)成功保存\n");
}
void main()
{
system("cls");
printf("\n\t\t\t 運(yùn)動(dòng)會(huì)分?jǐn)?shù)統(tǒng)計(jì)\n");
printf("輸入學(xué)校數(shù)目 (x>= 5):");
scanf("%d",&nsc);
printf("輸入男選手的項(xiàng)目(x<=20):");
scanf("%d",&msp);
printf("輸入女選手項(xiàng)目(<=20):");
scanf("%d",&wsp);
ntsp = msp + wsp;
phead = (int *)calloc(ntsp,sizeof(int));
pafirst = phead;
pasecond = phead;
input();
calculate();
output();
Save();
}
標(biāo)簽:
源代碼
上傳時(shí)間:
2016-12-28
上傳用戶:150501