2020.1.3计导全面复习

//用宏定义替换文本 
#include<iostream>
#include<cstdio>
#define p(m,n) printf("Hi,"#m"!\n""...And "#n"~\n")
int main(){
    p(John,Amy);
}

 

//条件编译
#include<iostream>
#include<cstdio>
#ifdef WIN32
#define PLL "%I64d"
#else
#define PLL "%lld"
#endif
int main(){
    long long x;
    scanf("%lld",&x);
    printf(PLL"\n",x);
}

 

//用带参宏计算圆的面积和周长
#include<iostream>
#include<cstdio>
#define PI 3.1415926
#define C(r) (2*PI*(r))
#define S(r) (PI*(r)*(r))
int main(){
    double x=1.2;
    printf("%lf\n%lf\n",C(x),S(x));
    return 0;
}

 

//线性同余产生随机数
#include<iostream>
#include<cstdio>
#include<ctime>
#define maxn 100010
const int a=975323;
const int b=2333333;
const int mod=152487967;
using namespace std;
int X[maxn];
int main(){
    X[0]=time(0);
    for(int i=1;i<=10;i++){
        X[i]=(1LL*a*X[i-1]+b)%mod;
    }
    for(int i=1;i<=10;i++)
        printf("%d ",X[i]);
    return 0;
}

 

//冒泡排序+二分查找
#include<iostream>
#include<cstdio>
using namespace std;
int n,a[1000];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
        for(int j=1;j<=n-i;j++)
            if(a[j]>a[j+1])swap(a[j],a[j+1]);
    int l=1,r=n,ans=0;
    for(int i=1;i<=n;i++)printf("%d ",a[i]);puts("");
    int x;scanf("%d",&x);
    while(l<=r){
        int mid=(l+r)>>1;
        if(a[mid]>=x){
            r=mid-1;
            ans=mid;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);
    return 0;
} 
//蒙特卡洛算法求积分
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
using namespace std;
double l,r,up,down;
double M,N;
int main(){
    srand(time(0));rand();rand();
    double x,y;
    l=1;r=2;up=1;down=0;
    for(int i=1;i<=1000000;i++){
        x=1LL*rand()*rand()*rand()%10000000;
        y=1LL*rand()*rand()*rand()%10000000;
        x+=10000000;
        x=x/10000000.0;
        y=y/10000000.0;
        if(x>=1&&x<=2&&y>=0&&y<=1)M++;
        if(x>=1&&x<=2&&y>=0&&y<1.0/x)N++;
    }
    double ans=1.0*N/M;
    printf("%f\n",ans);
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
using namespace std;
int main(){
    srand(time(0));rand();rand();
    double x,y,M=0,N=0;
    for(int i=1;i<=10000000;i++){
        x=1LL*rand()*rand()*rand()%10000000;
        y=1LL*rand()*rand()*rand()%10000000;
        x/=10000000.0;
        y/=10000000.0;
        if(x<=1&&x>=0&&y<=1&&y>=0){
            M++;
            if(x*x+y*y<=1)N++;
        }
    }
    double ans=N*4.0/M;
    printf("%f",ans);
    return 0;
}
蒙特卡洛计算Π

 

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
void add(int x,int y){
    int ans=x+y;
    printf("%d\n",ans);
}
void muli(int x,int y){
    int ans=x*y;
    printf("%d\n",ans);
}
void power(int x,int y){
    int ans=pow(x,y);
    printf("%d\n",ans);
}
void process(int x,int y,void (*pf)(int,int)){
    pf(x,y);
}
int main(){
    int x,y;
    scanf("%d%d",&x,&y);
    process(x,y,add); 
    process(x,y,muli);
    process(x,y,power);
    return 0;
}
函数指针
#include<iostream>
#include<cstdio>
#include<cstring>
#include<malloc.h>
struct list{
    int data;
    list* next;
};
list* head;
void creat_list(){
    head=(list*)malloc(sizeof(list));
    if(head==NULL)return;
    else {
        head->data=0;
        head->next=NULL;
    }
}
list* Insert(list* h,int x){
    list* t;
    t=(list*)malloc(sizeof(list));
    t->data=x;
    if(h==NULL||h->next==NULL){
        t->next=NULL;
        h->next=t;
        return h;
    }
    if(h->next->data>x){
        t->next=h->next;
        h->next=t;
        return h;
    }
    else {
        list* p=h->next;
        list* q=p->next;
        bool flag=0;
        while(q!=NULL){
            if(p->data<x&&q->data>=x){
                t->next=q;
                p->next=t;
                flag=1;
                break;
            }
            p=q;
            q=q->next;
        }
        if(!flag){
            t->next=NULL;
            p->next=t;
        }
        return h;
    }
}
void remove(list* h,int x){
    if(h==NULL||h->next==NULL)return;
    list* q=head->next;
    if(q->data==x){
        h->next=q->next;
        free(q);
        return;
    }
    else {
        if(q->next==NULL){
            if(q->data==x){
                free(q);
                h->next=NULL;
            }
            else return;
        }
        else {
            list* p=q->next;
            while(p!=NULL){
                if(p->data==x){
                    q->next=p->next;
                    free(p);
                    return;
                }
            }
        }
    }
}
void check(list* h){
    if(h==NULL||h->next==NULL)return;
    list* p=h->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }
    puts("");
}
int main(){
    creat_list();
    int n,x;
    puts("输入数字的个数");
    scanf("%d",&n);
    puts("输入每个数字");
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        Insert(head,x);
    }
    check(head);
    puts("输入要删除的数字的个数");
    scanf("%d",&n);
    puts("输入要删除的数字");
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        remove(head,x);
    }
    check(head);
    return 0;
}
链表实现插入排序

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<malloc.h>
#define Abs(x) ((x)>=0?(x):(-x))
using namespace std;
struct node{
    int a,b;//ϵÊýΪa£¬Ö¸ÊýΪb 
    node* next;
};
node *head[4];
void creat_node(node* &h){
    h=(node*)malloc(sizeof(node));
    if(h==NULL)return;
    else{
        h->a=0;
        h->b=0;
        h->next=NULL;
        return;
    }
}
void Insert(node* &h,int x,int y){
    if(h==NULL||h->next==NULL){
        node *q;
        q=(node*)malloc(sizeof(node));
        q->a=x;
        q->b=y; 
        q->next=NULL;
        h->next=q;
        return;
    }
    else {
        node* p=h->next;
        node* q=h;
        node* t;
        t=(node*)malloc(sizeof(node));
        t->a=x;
        t->b=y;
        t->next=NULL;
        while(p!=NULL){
            q=p;
            p=p->next;
        }
        q->next=t;
        return;
    }
}
void reverse(node* &h){//°ÑÁ´±í·´×ª 
    if(h==NULL||h->next==NULL)return;
    node* p=h->next;
    node* q=h;
    node* t;
    bool flag=0;//ÅжÏÊÇ·ñÊÇÁ´±íµÄµÚÒ»¸öÔªËØ 
    while(p!=NULL){
        t=p->next;
        if(!flag)p->next=NULL,flag=1;
        else p->next=q;
        q=p;
        p=t;
    }
    h->next=q;
}
void calc1(node* &h1,node* &h2,node* &h3){
    creat_node(h3);
    node* p1=h1->next;
    node* p2=h2->next;
    node* p3;
    node* q=h3;
    while(p1!=NULL&&p2!=NULL){
        int x=(p1->a)+(p2->a);
        if(x!=0){
            p3=(node*)malloc(sizeof(node));
            p3->a=x;
            p3->b=p1->b;
            p3->next=NULL;
            q->next=p3;
            q=p3;
        }
        p1=p1->next;
        p2=p2->next;
    }
    while(p1!=NULL){//Ò×´í£¡²»ÄÜ°Ñp1Ö±½Ó¸³Öµ¸øq£¬·ñÔòhead[3]Á´±í»áÓÐÒ»²¿·ÖÖ¸Ïòhead[2]Á´±í£¬Á´±íÖ®¼äʧȥ¶ÀÁ¢ÐÔ 
        p3=(node*)malloc(sizeof(node));
        p3->a=p1->a;
        p3->b=p1->b;
        p3->next=p1->next;
        q->next=p3;
        q=q->next;
        p1=p1->next;
    }
    while(p2!=NULL){
        p3=(node*)malloc(sizeof(node));
        p3->a=p2->a;
        p3->b=p2->b;
        p3->next=p2->next;
        q->next=p3;
        q=q->next;
        p2=p2->next;
    }
}
void calc2(node* &h1,node* &h2,node* &h3){
    creat_node(h3);
    node* p1=h1->next;
    node* p2=h2->next;
    node* p3;
    node* q=h3;
    while(p1!=NULL&&p2!=NULL){
        int x=(p1->a)-(p2->a);
        if(x!=0){
            p3=(node*)malloc(sizeof(node));
            p3->a=x;
            p3->b=p1->b;
            p3->next=NULL;
            q->next=p3;
            q=p3;
        }
        p1=p1->next;
        p2=p2->next;
    }
    while(p1!=NULL){
        p3=(node*)malloc(sizeof(node));
        p3->a=p1->a;
        p3->b=p1->b;
        p3->next=p1->next;
        q->next=p3;
        q=q->next;
        p1=p1->next;
    }
    while(p2!=NULL){
        p3=(node*)malloc(sizeof(node));
        p3->a=-(p2->a);
        p3->b=p2->b;
        p3->next=p2->next;
        q->next=p3;
        q=q->next;
        p2=p2->next;
    }
}
void check(node* &h){
    if(h==NULL||h->next==NULL)return;
    node* q=h->next;
    bool flag=0;
    while(q->next!=NULL){
        if(!flag)printf("%d*x^%d",q->a,q->b),flag=1;
        else printf("%d*x^%d",Abs(q->a),q->b);
        if(q->next->a>0)printf(" + ");
        else printf(" - ");
        q=q->next;
    }
    if(q!=NULL)printf("%d*x^%d\n",Abs(q->a),q->b);
}
int main(){
    //freopen("Cola.txt","r",stdin);
    int n,x;
    creat_node(head[1]);
    creat_node(head[2]);
    //------------------------------------------------- 
    puts("ÇëÊäÈëµÚÒ»¸ö¶àÏîʽµÄÏîÊý:");
    scanf("%d",&n);
    puts("ÇëÒÀ´ÎÊäÈëÿһÏîµÄϵÊý:");
//    creat_node(head[1]);
    for(int i=n-1;i>=0;i--){
        scanf("%d",&x);
        Insert(head[1],x,i);
    }
//    check(head[1]); 
    reverse(head[1]); 
//    check(head[1]);
    //-------------------------------------------------- 
    puts("ÇëÊäÈëµÚ¶þ¸ö¶àÏîʽµÄÏîÊý:");
    scanf("%d",&n);
    puts("ÇëÒÀ´ÎÊäÈëÿһÏîµÄϵÊý:");
//    creat_node(head[2]);
    for(int i=n-1;i>=0;i--){
        scanf("%d",&x);
        Insert(head[2],x,i);
    }
//    check(head[2]);
    reverse(head[2]);
//    check(head[2]);
    //---------------------------------------------------- 
    puts("Á½¶àÏîʽ֮ºÍΪ:");
    calc1(head[1],head[2],head[3]);//¼Ó·¨ÔËËã 
    reverse(head[3]);
    check(head[3]);
    free(head[3]);
    //----------------------------------------------------- 
    puts("Á½¶àÏîʽ֮²îΪ:");
    calc2(head[1],head[2],head[3]);//¼õ·¨ÔËËã 
    reverse(head[3]);
    check(head[3]);
    
    return 0;
}
链表计算多项式加减法

 

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
void Creat(char *name,int sz){
    FILE *fp=fopen(name,"w+");
    if(fp!=NULL){
        puts("此文件已存在,是否覆盖该文件?");
        puts("1.是");
        puts("2.否");
        int op;
        scanf("%d",&op);
        if(op==2)return;
    }
    fseek(fp,sz*1024-1,SEEK_SET);
    putw(32,fp);
    puts("文件创建成功!");
    fclose(fp); 
}
int main(){
    char name[100];
    int sz;
    puts("请输入要创建的文件名称");
    scanf("%s",name);
    puts("请输入要预留的空间大小(单位为K)");
    scanf("%d",&sz);
    Creat(name,sz);
    return 0;
}
创建文件并预留空间
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
char name[10][10]; 
int main(){
    FILE *fp,*fq;
    puts("请输入第一个文件的名称"); 
    scanf("%s",name[1]);
    if((fopen(name[1],"r"))==NULL){//文件不存在 
        printf("文件%s不存在\n",name[1]);
//        puts("程序已退出");
        exit(0);
    }
    printf("已成功打开文件%s\n",name);
    fp=fopen(name[1],"r");
    puts("请输入第二个文件的名称");
    scanf("%s",name[2]);
    if(fopen(name[2],"r")){//文件已存在 
        printf("该文件已存在,是否允许覆盖文件%s\n",name[2]);
        puts("1.是");
        puts("2.否");
        int op;
        scanf("%d",&op);
        if(op==2){
//            puts("程序已退出");
            exit(0);
        }
        else {
            fq=fopen(name[2],"w");
        }
    }
    else fq=fopen(name[2],"w");
    char ch;
    do{
        ch=fgetc(fp);
        fputc(ch,fq);
    }while(ch!=EOF);
    puts("文件复制成功");
    return 0;
}
将一个文件复制到另一个文件中

 

#include<iostream>
#include<cstdio>
#include<cstdlib>
struct node{
    int data;
    node *next;
};
int main(){
    int i,j,k,m,n;
    node *h,*p,*q;
    scanf("%d%d",&n,&m);
    h=(node*)malloc(sizeof(node));
    h->data=1;
    h->next=h;
    p=h;
    for(i=2;i<=n;i++){
        q=(node*)malloc(sizeof(node));
        q->data=i;
        q->next=p->next;
        p->next=q;
        p=q;
    }
    p=h;
    k=1;//µ±Ç°Ëù±¨µÄÊý 
    while(p->next!=p){
        if(k<m-1){
            k++;
            p=p->next;
        }
        else if(k==m-1){
            q=p->next;
            p->next=p->next->next;
            printf("%d--",q->data);
            free(q);
            k=1;
            p=p->next;
        }
    }
    printf("%d\n",p->data);
    return 0;
}
malloc、结构体和指针实现约瑟夫环
#include<iostream>
#include<cstdio>
#include<malloc.h>
using namespace std;
int n;
struct Tree{
    int data,v;
    Tree *lson;
    Tree *rson;
};
Tree *p,*root;
void Insert(int x){
    Tree *q;
//    q=(Tree*)malloc(sizeof(Tree));
    q=root;
    while(1){
        if((p->v)>=(q->v)){
            if(q->rson==NULL){
                q->rson=p;
                return;
            }
            else {
                q=q->rson;
            }
        }
        else {
            if(q->lson==NULL){
                q->lson=p;
                return;
            }
            else {
                q=q->lson;
            }
        }
    }
}
void dfs(Tree *q){
    printf("%d ",q->v);
    if(q->lson!=NULL)
        dfs(q->lson);
    if(q->rson!=NULL)
        dfs(q->rson);
}
int main(){
    freopen("Cola.txt","r",stdin);
    int x;
    scanf("%d",&n);
    root=(Tree*)malloc(sizeof(Tree));
    scanf("%d",&x);
    root->data=1;
    root->v=x;
    root->lson=NULL;
    root->rson=NULL;
    for(int i=2;i<=n;i++){
        scanf("%d",&x);
        p=(Tree*)malloc(sizeof(Tree));
        p->data=i;
        p->v=x;
        p->lson=NULL;
        p->rson=NULL;
        Insert(x);
    }
    dfs(root);
    return 0;
}
malloc、结构体和指针实现二叉排序树

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a[1000],b[1000];
void merge(int l,int r){
    if(l==r)return;
    int mid=(l+r)>>1;
    merge(l,mid);merge(mid+1,r);
    int i=l,j=mid+1;
    memset(b,0,sizeof(b));
    int cnt=0;
    while(i<=mid&&j<=r){
        if(a[i]<a[j]){
            b[++cnt]=a[i++];
        }
        else {
            b[++cnt]=a[j++];
        }
    }
    while(i<=mid)b[++cnt]=a[i++];
    while(j<=r)b[++cnt]=a[j++];
    for(i=l,j=1;i<=r;i++,j++)a[i]=b[j];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    merge(1,n);
    for(int i=1;i<=n;i++)printf("%d ",a[i]);
    return 0;
} 
归并排序

 

posted @ 2020-01-03 18:20  Echo宝贝儿  阅读(206)  评论(0编辑  收藏  举报