天天看点

数据结构1

oj一题  //http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1010      
#include<stdio.h>      
#include<string.h>
#define MAXD 100010
int N, ishead[MAXD], right[MAXD];
void init() //输入函数
{
 int i, j, k, x, y;
 memset(ishead, 0, sizeof(ishead));//每条链表的头
 memset(right, -1, sizeof(right));
 for(i = 0; i < N; i ++)
 {
 scanf("%d%d", &x, &y);
 if(x)
 right[x] = y;//连接的东西1->x:
 else
 ishead[y] = 1;//设置头
 }
}      
void solve()
{
 int i, j, k, min, flag;
 min = 1000000000;
 for(i = 1; i <= N; i ++)
 if(ishead[i])//找头
 {
 k = 0;
 for(j = i; right[j] != -1; j = right[j])//从头往后找
 k ++;
 if(k < min)
 {
 min = k;
 flag = j;
 }
 else if(k == min && j < flag)
 flag = j;
 }
 printf("%d\n", flag);
}
int main()
{
 while(scanf("%d", &N) == 1)
 {
 init();
 solve(); 
 }
 return 0; 
}      
二、自己做的白皮书6.2.2小球排列      
1.通过数组平移位置实现。      
#include<stdio.h>
#include<string.h>
#include<time.h>
const int MAXN = 1000;
int n, m,a[MAXN];
void left_xy(int x, int y)
{
 int t,i;
 for(i = 1; i <= n; i ++)
 if(a[i] == x) break;
 t = a[i];
 for(;;)
 {
 a[i] = a[i + 1];
 i ++;
 if(a[i + 1] == y)
 {
 a[i] = t;break;
 }
 }
}
void right_xy(int x, int y)
{
 int t,i;
 for(i = 1; i <= n; i ++)
 if(a[i] == x) break;
 t = a[i];
 for(;;)
 {
 a[i] = a[i + 1];
 if(a[i + 1] == y)
 {
 a[i] = a[i + 1];a[i + 1] = t;break;
 }
 i ++;
 }
}
void right_yx(int x, int y)
{
 int t,i;
 for(i = 1; i <= n; i ++)
 if(a[i] == x) break;
 t = a[i];
 for(;;)
 {
 a[i] = a[i - 1];
 i --;
 if(a[i - 1] == y)
 {
 a[i] = t;break;
 }
 }
}
void left_yx(int x, int y)
{
 int t,i;
 for(i = 1; i <= n; i ++)
 if(a[i] == x) break;
 t = a[i];
 for(;;)
 {
 a[i] = a[i - 1];
 if(a[i] == y)
 {
 a[i - 1] = a[i];a[i - 1] = t;break;
 }
 i --;
 }
}
int main()
{
 int p, b, k;
 char type;
 while(scanf("%d%d",&n,&m) == 2)
 {
 memset(a,0,sizeof(a));
 for(k = 1; k <= n; k ++)
 a[k] = k;
 for(int j = 0; j < m; j ++)
 {
 scanf("%s%d%d",&type, &p, &b);
 if(type == 'A')
 {
 if(p > b)
 left_yx(p,b);
 else
 left_xy(p,b);
 }
 else
 {
 if(p > b)
 right_yx(p,b);
 else
 right_xy(p,b);
 }
 }
 for(int q = 1; q <= n; q ++)
 printf("%d ",a[q]);
 printf("\n");
 printf("the time is %.2lf s\n",(double)clock()/CLOCKS_PER_SEC);
 }
 return 0;
}      
2.通过链表实现。      
#include<stdio.h>
#include<string.h>
#include<time.h>
#define MAXN 500010
int n, m, right[MAXN], left[MAXN];
void link()
{
 memset(right,0,sizeof(right));
 memset(left,0,sizeof(left));
 for(int i = 0; i <= n; i ++)
 {
 if(i < n)
 right[i] = i + 1;
 if(i > 0)
 left[i] = i - 1;
 }
}
void left_link(int x, int y)
{
 right[left[x]] = right[x];
 left[right[x]] = left[x];
 right[left[y]] = x;
 left[x] = left[y];
 right[x] = y;
 left[y] = x;
}
void right_link(int x,int y)
{
 right[left[x]] = right[x];
 left[right[x]] = left[x];
 left[right[y]] = x;
 right[x] = right[y];
 right[y] = x;
 left[x] = y;
}
void output()
{
 for(int j = 0; right[j] != 0; j = right[j])
 printf("%d ",right[j]);
 printf("\n");
}
int main()
{
 int x,y;
 char type;
 while(scanf("%d%d",&n, &m) == 2)
 {
 link();
 for(int i = 1; i <= m; i ++)
 {
 scanf("%s%d%d",&type,&x,&y);
 if(type == 'A')
 left_link(x,y);
 else if(type == 'B')
 right_link(x,y);
 } 
 output();
 printf("the time is %.2lf s\n",(double)clock()/CLOCKS_PER_SEC); 
 }
 return 0;
}      
上一篇: Tex括号