c演算法iiv
#include <stdio.h>
int main(void)
{
unsigned long long A,B,C;
int cnt=0;
scanf("%ld%ld",&A,&B);
C=A^B;//異或運算,不同位置一
do{
cnt+=(int)(C&1); //末位為1計數
C>>=C;//右移一位
}while(C); //全零結束
printf("number of diff bit:%d\n",cnt );
}
2. 二叉樹C語言演算法,急!!!!
清華大學
嚴蔚敏
的<數據結構里>都有完整的代碼,解釋的也很清楚
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
typedef
struct
tree
{
struct
tree
*left;
int
date;
struct
tree
*right;
}treenode,*b_tree;
///////插入節點/////////////////////
b_tree
insert(b_tree
root,int
node)
{
b_tree
newnode;
b_tree
currentnode;
b_tree
parentnode;
newnode=(b_tree)malloc(sizeof(treenode));
newnode->date=node;
newnode->right=NULL;
newnode->left=NULL;
if(root==NULL)
return
newnode;
else
{
currentnode=root;
while(currentnode!=NULL)
{
parentnode=currentnode;
if(currentnode->date>node)
currentnode=currentnode->left;
else
currentnode=currentnode->right;
}
if(parentnode->date>node)
parentnode->left=newnode;
else
parentnode->right=newnode;
}
return
root;
}
//////建立樹///////////////////
b_tree
creat(int
*date,int
len)
{
b_tree
root=NULL;
int
i;
for(i=0;i<len;i++)
root=insert(root,date[i]);
return
root;
}
//////中序列印////////////////
void
print1(b_tree
root)
{if(root!=NULL)
{
print1(root->left);
printf("%d->",root->date);
print1(root->right);
}
}
//////後序列印////////////////
void
print2(b_tree
root)
{if(root!=NULL)
{
print2(root->left);
print2(root->right);
printf("%d->",root->date);
}
}
//////前序列印////////////////
void
print3(b_tree
root)
{if(root!=NULL)
{
printf("%d->",root->date);
print3(root->left);
print3(root->right);
}
}
//////////在二叉樹中查找給定關鍵字
////////////
b_tree
lookfor(b_tree
root,int
e)
{
b_tree
p1,p2;
if(root!=NULL)
{
if(root->date==e)
return
root;
else
p1=lookfor(root->left,e);
p2=lookfor(root->right,e);
if(p1!=NULL)
return
p1;
else
if(p2!=NULL)
return
p2;
else
return
NULL;
}
else
return
NULL;
}
///////測試函數//////////////////
void
main()
{
b_tree
root=NULL;
int
i,index;
int
value;
int
nodelist[20];
cout<<"輸入樹的節點,輸入0結束\n";
index=0;
cin>>value;
while(value!=0)
{
nodelist[index]=value;
index=index+1;
cin>>value;
}
root=creat(nodelist,index);
printf("\n中序列印\n");
print1(root);
printf("\n後序列印\n");
print2(root);
printf("\n前序列印\n");
print3(root);
printf("\n查找的詞:\n");
int
a;
scanf("%d",&a);
b_tree
p3=lookfor(root,a);
if(p3!=NULL)
printf("%d\n",p3->date);
else
printf("沒你要找的詞");
}
3. 【C語言演算法】求最優解
#include<stdio.h>
voidmain()
{
doubleV;
printf_s("請輸入V: ");
scanf_s("%lf",&V);
intm,n,p;
intM,N,P;
doubledelta=10000.0;
for(m=0;m<=16;m++)
{
for(n=0;n<=256;n++)
{
for(p=1;p<=4096;p++)
{
doubled=m*n/(double)p-V;
if(d<0)
d=-d;
if(d<delta)
{
delta=d;
M=m;
N=n;
P=p;
}
}
}
}
printf_s("最優解:M=%d,N=%d,P=%d ",M,N,P);
}
4. 來個c語言演算法大神幫幫忙!
#include<stdio.h>
#include<string.h>
void makeLDR(char DLR[],char LDR[],char LRD[],int n1)
{ int n2;
char *p=strchr(LDR,DLR[0]);
n2=p-LDR; //左子樹節點數
LRD[n1-1]=DLR[0]; //賦後序根節點
if(n2>0)makeLDR(DLR+1,LDR,LRD,n2); //遞歸建立左子樹的後序
if(n1-n2>1)makeLDR(DLR+1+n2,LDR+n2+1,LRD+n2,n1-n2-1); //遞歸建立右子樹的後序
}
int main()
{ int i,n;
char DLR[50],LDR[50],LRD[50],c;
for(i=0,c=' '; c!=' ';) //讀入左子樹序列
scanf("%c%c",&DLR[i++],&c);
for(i=0,c=' '; c!=' ';) //讀入右子樹序列
scanf("%c%c",&LDR[i++],&c);
DLR[n=i]=LDR[i]=LRD[i]='