每周题目杂记(2)

1、链表除去重复数字

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s
{
	int d;
	struct s*next;
}sto;
sto*creat()
{
	sto*h,*p1,*p2;
	h=0;
	int m=4;
	while(m--)
	{
		p1=(sto*)malloc(sizeof(sto));
		p1->next=h;
		h=p1;
	}
	p1=h;
	p1->d=20;
	p1=p1->next;
	p1->d=20;
	p1=p1->next;
	p1->d=12;
	p1=p1->next;
	p1->d=31;
	p1=p1->next;
	return h;
}
int cnt(sto*h,int n)
{
	int c=0;
	sto*p1=h,*p2;
	while(p1)
	{
		if(n==p1->d)
		c++;
		p1=p1->next;
	}
	return c;
}
sto*cp(sto*A)
{
	sto*p1=A,*p2,*B=0;
	int a[1000]={0};
	int i=0,j,c;
	while(p1)
	{
		if(a[p1->d]==0)
		{
			i++;
			a[p1->d]=i;
		}
		p1=p1->next;
	}
	p1=0;
	j=i;
	while(j)
	{
		for(c=0;c<50;c++)
		{
			if(a[c]==j) 
			{
				p1=(sto*)malloc(sizeof(sto));
				p1->d=c;
				p1->next=B;
				B=p1;
				break;
			}
		}
		j--;
	}
	
	return B;
}
void print(sto*h)
{
	while(h)
	{
		printf("%d\n",h->d);
		h=h->next;
	}
}
int main()
{
	sto*h1,*h2;
	h1=creat();
	h2=cp(h1);
	print(h2);
	return 0;
}
#include <stdio.h>                  //调用cnt函数做法
#include <stdlib.h>
#include <string.h>
typedef struct s
{
	int d;
	struct s*next;
}sto;
sto*creat()
{
	sto*h,*p1,*p2;
	h=0;
	int m=4;
	while(m--)
	{
		p1=(sto*)malloc(sizeof(sto));
		p1->next=h;
		h=p1;
	}
	p1=h;
	p1->d=20;
	p1=p1->next;
	p1->d=20;
	p1=p1->next;
	p1->d=12;
	p1=p1->next;
	p1->d=31;
	p1=p1->next;
	return h;
}
int cnt(sto*h,int n)
{
	int c=0;
	sto*p1=h,*p2;
	while(p1)
	{
		if(n==p1->d)
		c++;
		p1=p1->next;
	}
	return c;
}
sto*cp(sto*A)
{
	sto*p1=A,*p2,*p3,*B=0;
	int t,i=1,j=0,k=0;
	int a[1000];
	B=p2=p3=(sto*)malloc(sizeof(sto));
	t=p1->d;
	a[0]=t;
	p3->d=t;
	p1=p1->next;
	while(p1)
	{
		t=p1->d;
		p3=(sto*)malloc(sizeof(sto));
		if(cnt(A,t)>1)
		{
			k=1;
			for(j=0;j<=i;j++)
			if(t==a[j])
			{
				k=0;
				break;
			}
			if(k==1)
			{
				i++;
				a[i]=t;
				p3->d=t;
				p2->next=p3;
				p2=p3;
			}
			
		}
		else 
		{
			p3->d=t;
			p2->next=p3;
			p2=p3;
		}
		p1=p1->next;
		
	}
	p2->next=0;
	return B;
}
void print(sto*h)
{
	while(h)
	{
		printf("%d\n",h->d);
		h=h->next;
	}
}
int main()
{
	sto*h1,*h2;
	h1=creat();
	h2=cp(h1);
	print(h2);
	return 0;
}

2、2019厦大期末

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char s[101]={0},t[9]={0};
	char a[9]={0};
	int b[9]={0};
	int i,j,k,n,p,q=0,m=1;
	scanf("%d %d",&n,&k);
	scanf("%s",s);
	p=n-1;
	while(1)
	{
		
		if(m)
		{
			
			for(j=k-1;j>=0;j--)
			{
				a[j]=s[p--];
				b[j]+=a[j]-'0';
			}
			puts(a);
			m=0;
		}
		else
		{
			for(j=0;j<k;j++)
			{
				a[j]=s[p--];
				b[j]+=a[j]-'0';
			}
			puts(a);
			m=1;
		}
		if(p+1-k<0)
		{
			for(i=0;i<k;i++)
			a[i]=0;
			if(m==1)
			{
				
				for(j=k-1;p>=0;j--)
				{
					a[j]=s[p--];
					b[j]+=a[j]-'0';
				}
				puts(a);
			}
			else
			{
				i=0;
				for(j=k-1;i<=p;j--)
				{
					a[j]=s[i++];
					b[j]+=a[j]-'0';
				}	
				puts(a+k-p-1);
			}
			break;
		}
	}
	for(i=k-1;i>0;i--)
	if(b[i]>=10)
	{
		b[i]-=10;
		b[i-1]++;
	}
	if(b[0]>=10)
	b[0]-=10;
	for(i=0;i<k;i++)
	q=10*q+b[i];
	printf("%d",q);
	return 0;
}

 2、

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	struct height
	{
		char na[32];
		int h;
	}a[21],t;
	int n,i,j,k;
	scanf("%d",&n);
	
	for(i=0;i<n;i++)
	scanf("%d%s",&a[i].h,a[i].na);
	for(i=0;i<n-1;i++)
	{
		k=i;
		for(j=k;j<n;j++)
		{
			if(a[k].h<a[j].h)
			k=j;
			else if((a[i].h==a[i+1].h)&&(strcmp(a[i].na,a[j].na)<0))
			k=j;
		}
		t=a[k];
		a[k]=a[i];
		a[i]=t;
	}
	if(n%2==1)
	{
		for(i=n-1;i>=0;i-=2)
		{
			printf("(%s,%d)",a[i].na,a[i].h);
		}
		for(i=1;i<=n-2;i+=2)
		{
			printf("(%s,%d)",a[i].na,a[i].h);
		}
	}
	else
	{
		for(i=n-2;i>=0;i-=2)
		{
			printf("(%s,%d)",a[i].na,a[i].h);
		}
		for(i=1;i<=n-1;i+=2)
		{
			printf("(%s,%d)",a[i].na,a[i].h);
		}
	}
	return 0;
}

 3、

 连带样例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct N
{
	char s[100];
	struct N*next;
}node;
int t(char*s)
{
	int m,i,c=0;
	char a=s[0];
	m=strlen(s);
	for(i=0;i<m;i++)
	{
		
		if(a>=*(s+i))
		a=s[i];
	}
	for(i=0;i<m;i++)
	if(*(s+i)==a)
	c++;
	return c;
}
node*ex(node*A,int n)
{
	node*p1=A,*B=0,*p2,*p3;
	int m=3;
	
	while(p1)
	{
		if(t(p1->s)==n)
		{
			p3=(node*)malloc(sizeof(node));    
			strcpy(p3->s,p1->s);
			p3->next=B;
			B=p3;
		}	

		p1=p1->next;
	}
	p1=B;
	while(p1) //一种方式,倒过来,再倒回去 另有一种处理方式
	{
		p3=(node*)malloc(sizeof(node));
		strcpy(p3->s,p1->s);
		p3->next=p2;
		p2=p3;
		p1=p1->next;
	}
	B=p2;
	return B;
}
int main()
{
	node *A,*B,*h,*p1,*p2;
	int n=3;
	h=0;
	int m=3;
	while(m--)
	{
		p1=(node*)malloc(sizeof(node));
		p1->next=A;
		A=p1;
	}
	h=A;
	strcpy(h->s,"000123");h=h->next;
	strcpy(h->s,"113234");h=h->next;
	strcpy(h->s,"222345");h->next=0;
	B=ex(A,n);p2=B;
	while(p2)
	{
		printf("%s   ",p2->s);
		p2=p2->next;
	}
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct N
{
	char s[100];
	struct N*next;
}node;
int t(char*s)
{
	int m,i,c=0;
	char a=s[0];
	m=strlen(s);
	for(i=0;i<m;i++)
	{
		
		if(a>=*(s+i))
		a=s[i];
	}
	for(i=0;i<m;i++)
	if(*(s+i)==a)
	c++;
	return c;
}
node*ex(node*A,int n)
{
	node*p1=A,*B=0,*p2,*p3;
	while(p1)
	{
		if(t(p1->s)==n)
		{
			p3=(node*)malloc(sizeof(node));
			strcpy(p3->s,p1->s);
			p3->next=0; 
			if(B==0) B=p3;  //注意此处条件及操作,是如何衔接的,又是如何避免与头节点重复
			else p2->next=p3;
			p2=p3;	
		}	
		p1=p1->next;
	}
	return B;
}
int main()
{
	node *A,*B,*h,*p1,*p2;
	int n=3;
	h=0;
	int m=3;
	while(m--)
	{
		p1=(node*)malloc(sizeof(node));
		p1->next=A;
		A=p1;
	}
	h=A;
	strcpy(h->s,"000123");h=h->next;
	strcpy(h->s,"113234");h=h->next;
	strcpy(h->s,"222345");h->next=0;
	B=ex(A,n);p2=B;
	while(p2)
	{
		printf("%s   ",p2->s);
		p2=p2->next;
	}
	return 0;
}

要练习顺序建立链表

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值