C语言IO流控制

 

#include <stdio.h>

int input(char *p, int length);
void clearstdin();
int main()
{
	char a[10];
	char b[10];

//	scanf("%s", a);
	input(a, 10);
	clearstdin();
	printf("%s\n", a);
	scanf("%s", b);
	printf("%s\n", b);
	return 0;
}

int input(char *p, int length)
{
	int count = 0;
	char buff;
	while (((buff = getchar ()) != '\n') && count < (length - 1))
	{
		p[count] = buff;
		count++;
	}
	p[count] = '\0';

	return count;
}

void clearstdin()
{
	char a;
	while ((a = getchar()) != '\n' && a != EOF);
}

迪杰特斯拉算法求最短路径

#ifndef GUIDE_H_INCLUDED   
#define GUIDE_H_INCLUDED   
#define MX 1000          //最大值 无穷   
#define NUM  6            //最大顶点个数   
typedef int adjmatrix[NUM][NUM];   
typedef int path[NUM][NUM];   
typedef int Dist[NUM];//v0到vi的的距离   
int ps[NUM]={0}; //最短路径值   
int final[NUM];//final[i]=1代表已经求出v0到vi的最短路径   
const int vexs[NUM] = {0,1,2,3,4,5};   
int arcs[NUM][NUM] = {   
    {MX,MX,10,MX,30,100},   
    {MX,MX,5,MX,MX,MX},   
    {MX,MX,MX,50,MX,MX},   
    {MX,MX,MX,MX,MX,10},   
    {MX,MX,MX,20,MX,60},   
    {MX,MX,MX,MX,MX,MX}   
    };   
  
#endif  

 

#include <iostream>   
using namespace std;   
#include "guide.h"  
  
//=====================================================   
void ShorttestPath_DIJ(adjmatrix arc,int v,path &p,Dist &d)   
{   
    //用迪杰斯特拉算法求解v0到其余个点的最短路径,p[v][w]代表v0到v经过w   
       
       
    int w=0;   
    for(int nv=0;nv<NUM;nv++)   
    {   
      final[nv]=0;   
      d[nv]=arc[v][nv];   
      ps[nv]=d[nv];   
      for(int w=0;w<NUM;w++) p[nv][w]=false;   
      if(d[nv]<MX)    
      {   
          p[nv][v]=1;   
          p[nv][nv]=1;   
      }   
    }//for   
    d[v]=0;   
    final[v]=1;   
    int min=MX;   
    //开始主循环   
    for(int i=1;i<NUM;++i)   
    {   
        min=MX;   
        for(w=0;w<NUM;++w)   
        {   
            if(final[w]==0)   
            {   
              if(d[w]<min)   
              {   
                 v=w;   
                 min=d[w];   
              }   
            }   
               
        }//for   
            final[v]=1;   
            for(w=0;w<NUM;w++)   
            {   
                if(final[w]==0&&(min+arc[v][w])<d[w])   
                {   
                    d[w]=min+arc[v][w];   
                    ps[w]=ps[v]+arc[v][w];   
                     for(int pos=0;pos<NUM;pos++)   
                     {   
                       p[w][pos]=p[v][pos];//借助最短路径到达w点   
        
                     }   
                     p[w][w]=1;//经过w点   
                }//endif   
               
            }//for   
       
           
    }//for   
       
       
}   
  
void DIJ_Print(int start,path &P)   
{   
  for(int i=1;i<NUM;i++)   
  {   
         int u=i;   
            
        if(final[i]==1)   
        {   
         cout<<"距离:"<<ps[i]<<"\t";   
          cout<<start;   
          int m=start;   
           for(int j=1;j<NUM;j++)   
           {   
                  
              if( P[u][j]==1)   
              {   
                  if(arcs[m][j]>0 && arcs[m][j]<MX)    
                  {   
                      cout<<"->"<<j;   
                      m=j;   
                      j=1;   
                  }   
                     
              }   
           }   
          cout<<endl;   
        }//endif   
      }//endfor   
}   
void ShortestPath()   
{   
    int start = 5;   
       
    Dist D;       //D[i]表示从start到i的最短距离;   
    path P;       //P[i,j]表示从start到i的最短路径上会经过j   
    int t[NUM]={0};   
    int n=0;   
    cout << "输入出发点 (0~5 空格分隔)" << endl;   
    cin >> start;   
    if(start>=0 && start<6)   
    {   
       //调用迪杰斯特拉算法   
      ShorttestPath_DIJ(arcs,start,P,D);   
      cout <<"从"<< start;   
      cout << "到其他各点的最短路径长度 :"<<endl ;   
      //调用迪杰斯特拉打印算法   
       DIJ_Print(start,P);   
        
    }//endif   
    else   
        cout << "没有这个地方!" << endl;   
}   
  
  
//============== mian文件 =============   
  
int main()   
{   
    char choose=0;   
    cout << "************************" << endl;   
    cout << "    a.x到其他点的最短路径        " << endl;   
    cout << "    b.退出            " << endl;   
    cout << "    版本号v1.8        " << endl;   
    cout << "************************" << endl;   
    cin >> choose;   
    while( choose!='b' )   
    {   
        if( choose=='a' )   
        {   
            ShortestPath();   
            cout << "===========================" << endl;   
            cout << "  a.x到y的最短路径 b.退出  " << endl;   
            cout << "===========================" << endl;   
        }   
        else if(choose!='a'||choose!='b')    cout << "输入错误,请重新输入:";   
        cin >> choose;   
    }   
    return 0;   
}  

二叉树层序遍历

#include <stdio.h>   
#include <stdlib.h>   
  
//树节点的定义   
struct TNode{   
    int value;   
    struct TNode *left;   
    struct TNode *right;   
};   
  
//队列节点的定义   
struct QNode{   
    struct TNode *p;   
    struct QNode *next;   
};   
  
//队列的定义   
struct Queue{   
    struct QNode *front;   
    struct QNode *rear;   
};   
  
//初始化队列   
void InitQueue(struct Queue *Q){   
    Q->front=Q->rear=(struct QNode*)malloc(sizeof(struct QNode));   
    Q->front->next=NULL;   
}   
  
//入队列   
void EnQueue(struct Queue *Q, struct TNode *node){   
    printf("en-node-value:%d\n",node->value);   
    struct QNode *nd=(struct QNode*)malloc(sizeof(struct QNode));   
    nd->next=NULL;   
    nd->p=node;   
    Q->rear->next=nd;   
    Q->rear=nd;   
}   
  
//出队列,注意判定快为空时,应有 rear=front   
struct TNode* DeQueue(struct Queue *Q){   
    struct QNode *p=Q->front->next;   
    Q->front->next=Q->front->next->next;   
    struct TNode *t=p->p;   
    if(Q->rear==p){   
        Q->rear=Q->front;   
    }   
    free(p);   
    printf("de-node-value:%d\n",t->value);   
    return t;   
}   
  
//建立二叉树   
void createLR(struct TNode *node){   
    if(node->value<20){   
        struct TNode *n1=(struct TNode*)malloc(sizeof(struct TNode));   
        struct TNode *n2=(struct TNode*)malloc(sizeof(struct TNode));   
        node->left=n1;   
        n1->value=node->value*2;   
        node->right=n2;   
        n2->value=node->value*2+1;       
        createLR(n1);   
        createLR(n2);   
    }else{   
        node->left=NULL;   
        node->right=NULL;   
    }   
}   
  
//主程序   
int main(int argc, char **argv){   
  
    struct TNode *head=(struct TNode*)malloc(sizeof(struct TNode));   
    head->value=1;   
    createLR(head);   
  
                //输出左、右边,测试树是否正确建立   
    struct TNode *p=head;   
    while(p){   
        printf("%d\n",p->value);   
        p=p->left;   
    }   
    p=head;   
    while(p){   
        printf("%d\n",p->value);   
        p=p->right;   
    }   
    printf("\n");   
  
                //用一个辅助的链队列,广度遍历树   
    p=head;   
    struct Queue *q;   
    InitQueue(q);   
    printf("%d\n",p->value);   
    EnQueue(q,p);   
    while(q->front->next!=NULL){   
        p=DeQueue(q);   
        if(p->left!=NULL){   
            printf("%d\n",p->left->value);   
            EnQueue(q,p->left);   
        }   
        if(p->right!=NULL){   
            printf("%d\n",p->right->value);   
            EnQueue(q,p->right);   
        }   
    }   
       
                //以下代码没有别的意思,就是测试命令行参数   
    int i=0;   
    for(i=0; i<argc; i++){   
        printf("%s\n",argv[i]);   
    }   
  
    return 0;   
}  

课程设计的算法题

这两周我们这里搞课程设计,全是算法题。出题和验题的方式就和ACM差不多。

我没有研究过ACM之类的算法,因为开始我觉得像我们这些搞Linux运维的对算法要求应该也不高吧,于是乎,上个学期的那一本课本现在还是新的。

那些题目与ACM题目比起来,简直是小儿科,但是对于我这样的算法低能来说写出来不难,修改错误就有点复杂了,半个小时写完的程序估计要4个小时才能改好。好了不说废话了。我把代码发上来,各位读者帮我看看哪里需要改进下。

题目我不发了,因为要是发了题目,其他的同学可能会通过搜索引擎找到我的代码。

代码如下:

 

/*
作者:恒亮+
*/
#include <stdio.h>

int n,m;
int num[2][100000];

int sort_num(const void *a, const void *b)
{
    if(*(int*)a>*(int*)b)
        return 1;
    else if(*(int*)a<*(int*)b)
        return -1;
    else
        return 0;
}

int find(int a)
{	
	int p_one = n-1, p_two = 0, p;
	while (p_two <= p_one)
	{
		p = (p_one+p_two)/2;
		if (a == num[0][p])
		{
			printf("yes\n");
			return 1;
		}
		if (a > num[0][p])
			p_two=p+1;
		else
			p_one=p-1;
	}
	printf("no\n");
	return 1;

}


int main()
{
	int i,j;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		int temp = 0;
		for (temp = 0; temp < n; temp++)
			scanf("%d", &num[0][temp]);
		for (temp = 0; temp < m; temp++)
			scanf("%d", &num[1][temp]);
			
		/*快速法排序*/
		qsort((void *)num[0], n, sizeof(int), sort_num);
		
		/*二分法从num[0]中找某个数字*/
		for (temp = 0; temp < m; temp++)
			find(num[1][temp]);
	}
	return 0;
}

 

注意:除了排序和二分法查找,其他的地方都不能改

尝试着学一点ACM算法

来学校一年了,学了一个学期的C语言,学了一个学期的Linux,大一就这样结束了。

写程序的时候发现自己好多东西都不会,对着电脑又不知道干什么。

没有人指导。。。。

于是乎,我发现我迷失了方向。

我正尝试着学算法。因为我发现我的算法实在是太弱了。

开了个博客

我已经有自己的wordpress博客了。今天突然想在这里开通一个博客。