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

寫個演算法

發布時間: 2022-09-26 10:54:28

1. c語言寫個演算法

#include <stdio.h>
#include<stdlib.h>
#include<time.h>

int InArray(int a[],int N,int data)/*判斷data是否在數組a[N]中.是則返回1,否則返回0*/
{
int i;
for(i=0;i<N;i++)
if(data==a[i])
return 1;
return 0;
}

int main(void)
{
int i,j=1,temp,a[8]={0};
srand( (unsigned)time( NULL ) ); /*保證每次生成的隨機數不相同*/

a[0]=rand()%8+1; /*數組第一項*/
for(i=1;i<8;i++)
{
temp=rand()%8+1;
if(InArray(a,8,temp))//當產生的隨機數已存在
{i--;continue;}
else
a[j++]=temp;
}

for(i=0;i<8;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

2. 寫個演算法(c語言)

/* 單鏈表就地逆置演算法 */
void converse(NODEPTR L)
{
NODEPTR p,q;
p=L->next; q=p->next;
L->next=NULL;
while(p) /* 對於當前結點p,用頭插法將結點p插入到頭結點之後 */
{
p->next=L->next;
L->next=p;
p=q;
q=q->next;
}
}

買一送一吧:
-------------------------------
-------------------------------
/* 單鏈表就地逆置的C語言程序 */
#define NULL 0

/*定義單鏈表的數據類型 */
typedef struct node{
int data;
struct node * next;
}NODE,*NODEPTR;

/*創建單鏈表 */
NODEPTR createlink()
{
NODEPTR L,p,q;
int i,n,e;
L=(NODEPTR)malloc(sizeof(NODE));
L->next=NULL;
q=L;

printf("please input the length of the link list\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(NODEPTR)malloc(sizeof(NODE));
printf("please enter the value of the list element\n");
scanf("%d",&e);
p->data=e;
q->next=p;
q=p;
}
q->next=NULL;
return L;
}

/* 逐個輸出單鏈表數據元素的值 */
void travlist(NODEPTR L)
{ NODEPTR p;
p=L->next;
printf("the value of the linklist:\n");
while(p)
{
printf("%d%s",p->data,"-->");
p=p->next;
}
printf("\n");
}

/* 單鏈表就地逆置 */
void converse(NODEPTR L)
{
NODEPTR p,q;
p=L->next; q=p->next;
L->next=NULL;
while(p) /* 對於當前結點p,用頭插法將結點p插入到頭結點之後 */
{
p->next=L->next;
L->next=p;
p=q;
q=q->next;
}
}

main()
{
NODEPTR L;
L=createlink();
travlist(L);
converse(L);
travlist(L);

}

3. 誰能幫我寫一個演算法

這個題目很明顯的是用堆棧嘛。
先是手動輸入的數據依次進棧,後面的工作就是依次出棧,每出一個數據就乘以一個10。
假設(沒寫參數):InitStack();//初始化一個棧
Push();//進棧
Popsh();//出棧
EmptyStack();//判斷一個棧是否為空,空返回1,否則為0

演算法簡略如下:
int temp,result;
InitStack(s);
scanf("%d",&count); //輸入數據的個數
for(int i=0;i<count;i++)
{
scanf("%d",&data);
Push(s,data);
}

Pop(s,temp);
result=temp;
while(!stackempty(s))
{
Pop(s,temp);
result=10*result+temp;

}

4. 寫一個演算法,將一個順序棧中的元素依次取出,並列印元素

對,後進先出。列印的順序與原來輸入的相反。
演算法:
#define Stack_Size 20
typedef struct
{
int elem [Stack_Size];
int top ;/*存放棧頂元素的下標*/
} SeqStack;
void Pop (SeqStack *S,int x)
{
if(S->top==-1) /*棧為空*/
exit(0);
else
{
*x=S->elem[S->top];
printf("%d ",x); /*列印*/
S->top--; /*修改棧頂指針*/
}
}

5. 用c語言寫演算法

直接手寫

size_t lenT, lenP, lenS;
char *e;
if ( !T || !P || !S ) return;
e = strstr( T, P );

if ( !e ) return;
lenT = strlen( T );
lenP = strlen( P );
lenS = strlen( S );
memmove( e+lenS, e+lenP, lenT+1-(e-T)-lenP );
memcpy( e, s, lenS );

假定三個長度 t、p、s 。
strstr: O(t*p)
strlen*3: O(t+p+s)
memmove: O(t-p)
memcpy:O(s)
最終復雜度 O(t*p+2(t+s)) -> O(n^2)。
可以看出熱點在 strstr 函數。
如果將其通過 kmp 或類似的匹配演算法優化成 O(n) 的,那麼復雜度可以直接降為 O(n) 。

6. 寫一個遞歸演算法(數據結構)

簡單,實現用結構數組,三個成員域, 變數名,表達式, 默認值,當然這個檢索比較麻煩,我不能用變數名"A"直接找到,對應的其他值(必須要循環數組),用C#的的字典對象就好辦了<index,value>,這個不是關鍵
下面 寫個函數 叫 defaulvalue cal( 變數名) {
if 變數名.默認值<> null 則 cal (計算公式); -- 遞歸開始,計算公式要進行解析,按單獨變數來,還要解析運算符(有點復雜,如果涉及到運算符的優先判斷,這里的都是平級的,還有進行運算符優先判斷)
else reture 默認值 --遞歸終結
}
基本就這樣,有點簡單,還要仔細斟酌下

7. 寫一個有效的演算法來測試一個給定的數組A[1...n]是否是一個堆,該演算法的時間復雜性是多少

時間復雜度是O(n),可以從n到1,也可以從1到n,從n開始就看(k/2)下取整下標的元素(也就是堆中的雙親)是否滿足大根或者小根的條件,從1開始就看2k和2k+1下標的元素(就是堆中的左右孩子)是否滿足堆的條件

8. 寫一個演算法,求單鏈表中的最大值

可以參考下面的代碼:

public static int FindMax(Node head)

{

if (head == null)

return 0;

int Max = head.value;

while(head.next != null)

{

if (head.next.value > Max)

Max = head.next.value;

head = head.next;

}

return Max;

(8)寫個演算法擴展閱讀:

單鏈表的具體存儲

1、用一組任意的存儲單元來存放線性表的結點(這組存儲單元既可以是連續的,也可以是不連續的)

2、鏈表中結點的邏輯次序和物理次序不一定相同。為了能正確表示結點間的邏輯關系,在存儲每個結點值的同時,還必須存儲指示其後繼結點的地址(或位置)信息(稱為指針(pointer)或鏈(link))

鏈式存儲是最常用的存儲方式之一,它不僅可用來表示線性表,而且可用來表示各種非線性的數據結構。

熱點內容
免費手游掛機腳本 發布:2025-05-18 00:55:43 瀏覽:354
sd卡手機存儲系統存儲 發布:2025-05-18 00:55:28 瀏覽:636
pythonlistintstr 發布:2025-05-18 00:48:18 瀏覽:604
輕應用緩存 發布:2025-05-18 00:31:02 瀏覽:251
鳥存儲空氣 發布:2025-05-18 00:20:24 瀏覽:201
linux刻錄iso 發布:2025-05-18 00:16:15 瀏覽:663
php動態參數 發布:2025-05-18 00:12:05 瀏覽:425
安卓應用上傳 發布:2025-05-18 00:11:57 瀏覽:803
數對的演算法 發布:2025-05-18 00:11:02 瀏覽:382
linuxwhile 發布:2025-05-18 00:10:08 瀏覽:144