感觉这篇博客写完,会方便更多同学。。。。
这些是做大一数据结构实验的记录
有些题目真的,很难啊,最后的几道题目,我也是谷歌的!做到吐血。。
A - B Problem II
这是个大数相加的问题,即高精度加法
贴一发高精度模板总结
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int L=110;
string sub(string a,string b)
{
string ans;
int na[L]={0},nb[L]={0};
int la=a.size(),lb=b.size();
for(int i=0;i<la;i++) na[la-1-i]=a[i]-'0';
for(int i=0;i<lb;i++) nb[lb-1-i]=b[i]-'0';
int lmax=la>lb?la:lb;
for(int i=0;i<lmax;i++)
{
na[i]-=nb[i];
if(na[i]<0) na[i]+=10,na[i+1]--;
}
while(!na[--lmax]&&lmax>0) ;lmax++;
for(int i=lmax-1;i>=0;i--) ans+=na[i]+'0';
return ans;
}
int main()
{
string a,b;
while(cin>>a>>b) cout<<sub(a,b)<<endl;
return 0;
}
陶陶摘苹果
这个就注意一下,多组数据测试就行了,没打过ACM的可能会出错<span style="color: red;">while(cin>>a[0])</span>
# include <iostream>
using namespace std;
int main(){
int a[10],h;
while(cin>>a[0]){
int sum=0;
for(int i=1;i<10;i++)
cin>>a[i];
cin>>h;
for(int i=0;i<10;i++){
if(h+30>=a[i])
sum++;
}
cout<<sum<<endl;
}
return 0;
}
校门外的树
这道题是参考洛洛师傅的,其中把要移走的树值赋为-1,然后计算值为-1的数目
个人感觉这个思想很好。。。。
#include <iostream>
using namespace std;
int main(){
int L,M,a,b,i,move,j;
while(cin>>L>>M)
{
int s[10001]={0};
while(M--){
cin>>a>>b;
for(i=a;i<=b;i++)
s[i]=-1;
}
move=0;
for(j=0;j<=L;j++){
if(s[j]==-1)
move+=1;
}
cout<<L-move+1<<endl;
}
return 0;
}
自动加密
这道题就是注意大小写都要写,然后字符对照ASCII,转换一下就行了
#include <iostream>
using namespace std;
int main(){
int i;
char s[100];
cin>>s;
for(i=0;s[i];i++)
{
if(s[i]>='a'&&s[i]<='z')
{
s[i]+=5;
if(s[i]>'z')
{
s[i]-=26;
}
}
else if(s[i]>='A'&&s[i]<='Z'){
s[i]+=5;
if(s[i]>'Z')
{
s[i]-=26;
}
}
}
cout<<s<<endl;
return 0;
}
计算秒数
这个和上一道题思路差不多,字符转数字
#include <iostream>
using namespace std;
int sum(char x,char y,int z){
int result;
if(x=='0'){
result=(y-48)*z;
}
else{
result=((x-48)*10+y-48)*z;
}
return result;
}
int main(){
int i,n,h,m,s;
char t[10];
while(cin>>n)
{
for(i=0;i<n;i++){
cin>>t;
h=sum(t[0],t[1],3600);
m=sum(t[3],t[4],60);
s=sum(t[6],t[7],1);
cout<<h+m+s<<endl;
}
}
return 0;
}
算法2-1:集合union
这道题给出了合并函数,补充表头,和定义类就行了,注意输出n+2行
#include<iostream>
using namespace std;
typedef struct List{
int L[205];
int length;
}List;
List La,Lb;
bool LocateElem(List &La,int e){
for(int i=0;i<La.length;i++){
if(La.L[i]==e)
return true;
}
return false;
}
void show(List &L){
cout<<L.L[0];
for(int i=1;i<L.length;i++)
cout<<" "<<L.L[i];
cout<<endl;
}
void Union(List &La,List &Lb){
int e;
for(int i=0;i<Lb.length;i++){
e=Lb.L[i];
if(!LocateElem(La,e)){
La.L[La.length]=e;
La.length++;
}
show(La);
}
}
int main(){
int n1,n2,i;
while(cin>>n1){
for(i=0;i<n1;i++)
cin>>La.L[i];
La.length=n1;
cin>>n2;
for(int i=0;i<n2;i++)
cin>>Lb.L[i];
Lb.length=n2;
show(La);
show(Lb);
Union(La,Lb);
cout<<endl;
}
return 0;
}
算法2-2:有序线性表的有序合并
上道题会了,这道题就会了,就是变一下主函数,并且注意空格格式,ACM要求很严格
#include<iostream>
using namespace std;
typedef struct List{
int date[200];
int length;
}List;
List La,Lb;
void InitList(List &L)
{
L.length=0;
}
int ListLength(List L){
return L.length;
}
int GetElem(List L,int p,int &e){
if(p<1||p>L.length)
return 0;
e=L.date[p];
return 0;
}
int ListInsert(List &L,int p,int e){
int i;
if(p<1||p>L.length||L.length==200-1)
return 0;
for(i=L.length;i>=p;--i)
L.date[i+1]=L.date[i];
L.date[p]=e;
return 0;
}
void MergeList(List La,List Lb,List &Lc){
int La_len,Lb_len;
int ai,bj;
int i=1,j=1,k=0;
InitList(Lc);
La_len=ListLength(La);
Lb_len=ListLength(Lb);
Lc.length=La.length+Lb.length;
while((i<=La_len)&&(j<=Lb_len)){
GetElem(La,i,ai);
GetElem(Lb,j,bj);
if(ai<=bj){
ListInsert(Lc,++k,ai);
++i;
}else{
ListInsert(Lc,++k,bj);
++j;
}
}
while (i<=La_len){
GetElem(La,i++,ai);
ListInsert(Lc,++k,ai);
}
while(j<=Lb_len){
GetElem(Lb,j++,bj);
ListInsert(Lc,++k,bj);
}
}
int main(){
List La,Lb,Lc;
InitList(La);
InitList(Lb);
while(cin>>La.length){
for(int i=1;i<=La.length;i++){
cin>>La.date[i];
}
cin>>Lb.length;
for(int i=1;i<=Lb.length;i++){
cin>>Lb.date[i];
}
MergeList(La,Lb,Lc);
if(Lc.length!=0){
cout<<Lc.date[1];
for(int i=2;i<=Lc.length;i++)
cout<<" "<<Lc.date[i];
}
cout<<endl;
}
return 0;
}
算法2-3~2-6:Big Bang
这是谷歌加修改的。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
char name[100];
}ElemType;
typedef struct
{
ElemType * elem;
int length;
int listsize;
}List;
int InitList(List &L)
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
return 0;
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return 1;
}
int ListInsert(List &L,int i,ElemType e)
{
ElemType *p;
if(i<1||i>L.length+1)
return 0;
if(L.length>=L.listsize)
{
ElemType *newbase;
newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
{
return 0;
}
L.elem = newbase;
L.listsize+=LISTINCREMENT;
}
ElemType *q = &(L.elem[i-1]);
for(p = &(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return 1;
}
int ListDlete (List &L,int i,ElemType e)
{
ElemType *p,*q;
if(i<1||i>L.length)
return 0;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
{
*(p-1)=*p;
}
--L.length;
return 1;
}
int LocateElem(List L,ElemType e,int(*compare)(ElemType,ElemType))
{
int i;
ElemType *p;
i=1;
p=L.elem;
while(i<=L.length&&!(*compare)(*p++,e))
++i;
if(i<=L.length)
return i;
else
return 0;
}
void listshow(List L)
{
int i;
for(i=0;i<L.length;i++)
{
if(i)
putchar(' ');
printf("%s",L.elem[i].name);
}
putchar('\n');
}
int cmp(ElemType e1, ElemType e2)
{
return (int)!strcmp(e1.name, e2.name);
}
int main (void)
{
char mark[10];
List nameList;
InitList(nameList);
int number;
ElemType e;
while(~scanf("%s",mark))
{
if(strcmp(mark,"insert")==0)
{
scanf("%d %s",&number,e.name);
ListInsert(nameList,number,e);
}
else if(strcmp(mark,"show")==0)
listshow(nameList);
else if(strcmp(mark,"delete")==0)
{
scanf("%s",e.name);
number=LocateElem(nameList,e,cmp);
ListDlete(nameList,number,e);
}
else if(strcmp(mark,"search")==0)
{
scanf("%s",e.name);
printf("%d\n",LocateElem(nameList,e,cmp));
}
}
return 0;
}
算法2-8~2-11:链表的基本操作
这个也是谷歌加修改的。
#include<string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status;
typedef int Boolean;
typedef int ElemType;
struct LNode
{
ElemType data;
struct LNode *next;
};
typedef struct LNode *LinkList;
Status GetElem(LinkList L,int i,ElemType *e)
{
int j=1;
LinkList p=L->next;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p||j>i)
return ERROR;
*e=p->data;
return OK;
}
Status ListInsert(LinkList L,int i,ElemType e)
{
int j=0;
LinkList p=L,s;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>i-1)
return ERROR;
s=(LinkList)malloc(sizeof(struct LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
Status ListDelete(LinkList L,int i,ElemType *e)
{
int j=0;
LinkList p=L,q;
while(p->next&&j<i-1)
{
p=p->next;
j++;
}
if(!p->next||j>i-1)
return ERROR;
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
return OK;
}
void CreateList(LinkList *L,int n)
{
int i;
LinkList p;
*L=(LinkList)malloc(sizeof(struct LNode));
(*L)->next=NULL;
for(i=n; i>0; --i)
{
p=(LinkList)malloc(sizeof(struct LNode));
scanf("%d",&p->data);
p->next=(*L)->next;
(*L)->next=p;
}
}
int ShowList(LinkList L)
{
int numOfList = 0;
LinkList p = L->next;
while(p)
{
if(numOfList)
{
putchar(' ');
}
numOfList++;
printf("%d",p->data);
p = p->next;
}
if(numOfList == 0)
{
return 0;
}
else
{
putchar('\n');
return numOfList;
}
}
int main()
{
int n;
int m;
char strInst[30];
int a;
LinkList L;
int e;
scanf("%d", &n);
CreateList(&L, n);
scanf("%d", &m);
while(m--)
{
scanf("%s", strInst);
if(strcmp(strInst, "get") == 0)
{
scanf("%d", &a);
if(GetElem(L, a, &e) == OK)
{
printf("%d\n", e);
}
else
{
puts("get fail");
}
}
else if(strcmp(strInst, "insert") == 0)
{
scanf("%d%d", &a, &e);
if(ListInsert(L, a, e) == OK)
{
puts("insert OK");
}
else
{
puts("insert fail");
}
}
else if(strcmp(strInst, "delete") == 0)
{
scanf("%d",&a);
if(ListDelete(L, a, &e) == OK)
{
puts("delete OK");
}
else
{
puts("delete fail");
}
}
else if(strcmp(strInst, "show") == 0)
{
if(ShowList(L) == 0)
{
puts("Link list is empty");
}
}
}
return 0;
}
算法2-13~2-16:静态链表
google+alter
Sample Input
show
insert 1 ZHAO
show
insert 2 QIAN
show
insert 3 SUN
show
insert 4 LI
insert 5 ZHOU
insert 6 WU
insert 7 ZHENG
insert 8 WANG
show
insert 1 ZHANG
show
search LI
#include <stdio.h>
#include <string.h>
#define MAXSIZE 11
typedef char ElemType[8];
typedef struct
{
ElemType data;
int cur;
} NodeType;
NodeType space[MAXSIZE];
typedef struct
{
int elem;
int length;
int listSize;
} SLinkList;
int LocateElem_SL(SLinkList& S, ElemType e)
{
int i;
i = S.elem;
while (i && strcmp(space[i].data, e))
i = space[i].cur;
return i;
}
void InitSpace_SL()
{
memset(space, 0 ,sizeof(space));
for (int i = 0; i < MAXSIZE - 1; ++i)
space[i].cur = i + 1;
space[MAXSIZE - 1].cur = 0;
}
int Malloc_SL()
{
int i = space[0].cur;
if (i)
space[0].cur = space[i].cur;
return i;
}
void Free_SL(int k)
{
space[k].cur = space[0].cur;
space[0].cur = k;
}
void Insert_SL(SLinkList& S, int i, ElemType e)
{
int cur = S.elem;
int j=0;
int newNodeIndex;
while(j<i-1)
{
cur = space[cur].cur;
++j;
}
newNodeIndex = Malloc_SL();
strcpy(space[newNodeIndex].data,e);
space[newNodeIndex].cur = 0;
space[newNodeIndex].cur = space[cur].cur;
space[cur].cur = newNodeIndex;
S.length++;
}
void Delete_SL(SLinkList& S, int i)
{
int cur = S.elem;
int j=0;
int delCur;
while(j<i-1)
{
cur = space[cur].cur;
++j;
}
delCur = space[cur].cur;
space[cur].cur = space[delCur].cur;
Free_SL(delCur);
S.length--;
}
void CreateList_SL(SLinkList& S)
{
S.elem = Malloc_SL();
space[S.elem].cur = 0;
S.length = 0;
S.listSize = 9;
}
void Show_space()
{
int i;
for(i=0; i<MAXSIZE; i++)
{
printf("%-8s%2d\n", space[i].data, space[i].cur);
}
}
int main()
{
SLinkList S;
char str[10];
int a;
ElemType e;
InitSpace_SL();
CreateList_SL(S);
while(scanf("%s", str) != EOF)
{
if(strcmp(str, "insert") == 0)
{
scanf("%d%s", &a, e);
Insert_SL(S, a, e);
}
else if(strcmp(str, "delete") == 0)
{
scanf("%d", &a);
Delete_SL(S, a);
}
else if(strcmp(str, "search") == 0)
{
scanf("%s", e);
printf("%2d\n********************\n", LocateElem_SL(S, e));
}
else if(strcmp(str, "show") == 0)
{
Show_space();
puts("********************");
}
}
return 0;
}
算法2-18~2-19:双向循环链表
google+alter
#include<stdio.h>
#include<stdlib.h>
typedef struct DLNode
{
int data;
struct DLNode *next;
struct DLNode *prior;
} DNLode;
void createDLNode(DLNode *&L)
{
L = (DLNode *)malloc(sizeof(DLNode));
L->prior = L;
L->next = L;
}
void printDLNode(DLNode *L)
{
DLNode *q;
q=L->next;
int i=0;
while(q!=L)
{
if(i++)//good idea!
{
putchar(' ');
}
printf("%d",q->data);
q=q->next;
}
printf("\n");
}
void insertDLNode(DLNode *&L,int i,int e)
{
DLNode *q,*qlen;
q=L;
qlen=L->next;
int num=0;
while(qlen!=L)
{
num++;
qlen=qlen->next;
}
int j=1;
while(j<i)
{
j++;
q=q->next;
}
if(i>num+1||i<1)
return ;
DLNode *s;
s=(DLNode *)malloc (sizeof(DLNode));
s->data = e;
s->next = q->next;
s->prior = q;
q->next = s;
q->next->prior = s;
return ;
}
void deleteDLNode (DLNode *&L,int i)
{
DLNode *q,*qlen;
qlen=L->next;
q=L;
int num=0;
while(qlen!=L)
{
qlen=qlen->next;
num++;
}
if(i<1||i>num)
{
return ;
}
int j=0;
while(j<i-1)
{
q=q->next;
j++;
}
DLNode *qfree;
qfree= q->next;
q->next=qfree->next;
qfree->next->prior=q;
free(qfree);
}
int main (void)
{
int n;
DNLode *L;
createDLNode(L);
while(~scanf("%d",&n))
{
switch(n)
{
case 0:
{
printDLNode(L);
break;
}
case 1:
{
int i,e;
scanf("%d%d",&i,&e);
insertDLNode(L,i,e);
// printDLNode(L);
break;
}
case 2:
{
int i;
scanf("%d",&i);
deleteDLNode(L,i);
// printDLNode(L);
break;
}
default:
break;
}
}
return 0;
}
算法2-23:一元多项式加法
来自洛洛师傅,没有用他给出的函数233333333333333333
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
#include<string.h>
using namespace std;
void hs(char*str,int *ss,int *sss)
{
char *p=NULL;
int zan;
zan=atoi(strtok(str," "));
for(int q=0;(p =strtok(NULL," "));q++)
{
if(q%2==0)
{
if(atoi(p)>=0)
ss[atoi(p)]=zan;
else
sss[-1*atoi(p)]=zan;
}
else
{
zan=atoi(p);
}
}
return;
}
void hss(char*str,int *ss,int *sss)
{ char *p1=NULL;
int zan1;
zan1=atoi(strtok(str," "));
//cout<<zan1<<endl;
for(int q=0;(p1 =strtok(NULL," "));q++)
{
if(q%2==0)
{
if(atoi(p1)>=0)
ss[atoi(p1)]+=zan1;
else
sss[-1*atoi(p1)]+=zan1;
//if(ss[atoi(p1)]>0)
// ss[atoi(p1)]+=zan1;
//else
// ss[atoi(p1)]=zan1;
//cout<<ss[atoi(p1)]<<endl;
}
else
{
zan1=atoi(p1);
//cout<<zan1<<endl;
}
}
return;
}
int main()
{
char strA[500],strB[500];
while(gets(strA) && strlen(strA))
{
int ss[300]={0},sss[300]={0};
hs(strA,ss,sss);
if(gets(strB)&& strlen(strB))
hss(strB,ss,sss);
int gg;
for(int j=0;j<150;j++)
{
if(ss[j]!=0)
gg=j;
}
//gg--;
for(;gg>=0;gg--)
if(ss[gg]!=0)
cout<<ss[gg]<<' '<<gg<<' ';
for(int y=0;y<300;y++)
if(sss[y]!=0)
cout<<sss[y]<<' '<<-1*y<<' ';
cout<<endl;
}
return 0;
}
算法3-1:八进制数
这道题目虽然放在了最后,但是并不难
#include<iostream>
#include<stack>
using namespace std;
#define MAXSIZE 1000
char str[MAXSIZE];
void Conversion(int num,int b){
stack<int>S;
do{
S.push(num%b);
num/= b;
}while(num);
while(!S.empty()){
cout<<S.top();
S.pop();
}
cout<<endl;
}
int main()
{
int a,b;
while(cin>>a){
b = 8;
Conversion(a,b);
}
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。