数组A中每个元素向后移动K位
//数组A中每个元素向后移动K位
#include<stdio.h>
#include<stdlib.h>
#define N 3
#define K 2
#define MAXSIZE (N+K)
void Enter(int *p){
printf("Please Enter Number\n");
int i=0;
for(i=0;i<N;i++){
scanf("%d",p+i);
printf("%d\n",*(p+i));
}
}
void Move_K(int *p){
p = p+N-1;
int i=0;
for(int i=N-1;i>=0;i--){
*(p+K) = *p;
*p=-1;
p--;
}
}
void Show(int *p){
int i;
for(i=0;i<N+K;i++){
printf("%d\n",*(p+i));
}
}
int main(){
int A[MAXSIZE];
Enter(A);
Move_K(A);
Show(A);
return 0;
}
No.2
//
typedef struct BITNode{
BiTNode *parent;
BiTNode *lchild;
BiTNode *rchild;
DATATYPE data;
int id;
}BITNode,*BiTree;
int maxID = 0;
Status preOrderTraverse(BiTree T,BiTNode *parent){
if(T == NULL){
retrun False;
}else{
if(T != parent){//非根节点,当前节点的父亲域指向parent
T->id = maxID+1;
T->parent = parent;
}else{//当前节点为父节点
maxID++;
T->id = maxID;
T->parent = NULL;
}
preOrderTravese(T->lchild,T);
preOrderTravese(T->rchild ,T);
return true;
}
}
void searchAncesters(BiTNode T ,int maxID){//找祖先
if(T!=NULL){
if(T->id == maxID){//是最大节点,递归向上找祖先
BiTNode *p = T->parent;
while(p != NULL){
p = p->parent;
}
}
else{//不是最大节点,就去左子树,右子树去找最大节点
searchAncesters(T->lchild,maxID);
searchANcesters(T->rchild,maxID);
}
}
}
void preOrderTraverse(int completeBiTNode[],int rtIndex,int n){
if(rtIndex < n){
printf("%d ",completeBiTree[rtIndex]);
preOrderTraverse(completeBiTree,2*rtIndex,n);
preOrderTraverse(completeBiTree,2*rtIndex+1,n);
}else if(rtIndex == n){
printf("%d \n",completeBiTree[rtIndex]);
for(int parentIndex = rtIndex / 2;parentIndex >= 1;parentIndex /=2){
printf("%d \n",completeBiTree[rtIndex]);
}
}
}
int main(){
int completeBiTree[] = {0,1,2,3,4,5,6,7};
preOrderTravese(comp;eteBiTree,1,7);
}
模拟大数乘
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_NUMBER_LEN 100
void bigNumberMulti(char *num1,char *num2,int len1,int len2){
int i,j;
int res[len1+len2];
memset(res,0,sizeof(res));//全置为0
// 123--2
// 1--0
// 2+0+1=3
for(i=len1-1;i>=0;i--){
for(j=len2-1;j>=0;j--){
res[i+j+1] += (num1[i]-'0')*(num2[j]-'0');
}
}
for(i=len1+len2-1;i>=0;i--){
if(res[i]>10){
res[i-1]+=res[i]/10;
res[i]=res[i]%10;
}
}
i=0;
while(i<len1+len2){
if(res[i]!=0){
break;
}
i++;
}
while(i<len1+len2){
printf("%d",res[i]);
i++;
}
}
int main(){
char num1[11];
char num2[11];
scanf("%s%s",num1,num2);
int len1 = strlen(num1);
int len2 = strlen(num2);
bigNumberMulti(num1,num2,len1,len2);
}