关于c语言的数据结构问题
题目:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1216不知道怎么做题目:...
题目:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1216
不知道怎么做
题目:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1214
这题上交后超时怎么改?
#include<stdio.h> #include<string.h>#include<stdlib.h>#define max 1002typedef struct SqList{ int data[max]; int length;}SqList;SqList a;int post,e;void InitList(SqList &a){a.length=0;}void ListInsert(SqList &a,int post,int e){ int j; for(j=a.length;j>=post;j--) a.data[j+1]=a.data[j]; a.data[post]=e; a.length=a.length+1;}int ListDelete(SqList &a,int post,int &e){ int j; if(post<1||post>a.length) return 0; e=a.data[post]; for(j=post+1;j<=a.length;j++) a.data[j-1]=a.data[j]; a.length--; return 1;}void ListGetelem(SqList &a,int post,int &e){ e=a.data[post];}int main(){ char ru[10]={"\0"}; int i,hh; InitList(a); scanf("%s",ru); fflush(stdin); while(strcmp(ru,"exit")!=0) { if(strcmp(ru,"insert")==0) { scanf("%d",&hh); for(i=0;i<hh;i++) { scanf("%d %d",&post,&e); ListInsert(a,post,e);} } else if(strcmp(ru,"delete")==0) { scanf("%d",&post); if(ListDelete(a,post,e)==1) printf("%d\n",e); } else if(strcmp(ru,"clear")==0) InitList(a); else if(strcmp(ru,"getelem")==0){ scanf("%d",&post); ListGetelem(a,post,e); printf("%d\n",e); } fflush(stdin); scanf("%s",ru); fflush(stdin); } return 0;} 展开
不知道怎么做
题目:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1214
这题上交后超时怎么改?
#include<stdio.h> #include<string.h>#include<stdlib.h>#define max 1002typedef struct SqList{ int data[max]; int length;}SqList;SqList a;int post,e;void InitList(SqList &a){a.length=0;}void ListInsert(SqList &a,int post,int e){ int j; for(j=a.length;j>=post;j--) a.data[j+1]=a.data[j]; a.data[post]=e; a.length=a.length+1;}int ListDelete(SqList &a,int post,int &e){ int j; if(post<1||post>a.length) return 0; e=a.data[post]; for(j=post+1;j<=a.length;j++) a.data[j-1]=a.data[j]; a.length--; return 1;}void ListGetelem(SqList &a,int post,int &e){ e=a.data[post];}int main(){ char ru[10]={"\0"}; int i,hh; InitList(a); scanf("%s",ru); fflush(stdin); while(strcmp(ru,"exit")!=0) { if(strcmp(ru,"insert")==0) { scanf("%d",&hh); for(i=0;i<hh;i++) { scanf("%d %d",&post,&e); ListInsert(a,post,e);} } else if(strcmp(ru,"delete")==0) { scanf("%d",&post); if(ListDelete(a,post,e)==1) printf("%d\n",e); } else if(strcmp(ru,"clear")==0) InitList(a); else if(strcmp(ru,"getelem")==0){ scanf("%d",&post); ListGetelem(a,post,e); printf("%d\n",e); } fflush(stdin); scanf("%s",ru); fflush(stdin); } return 0;} 展开
2个回答
展开全部
第一题,用的排序做的,参考下AC的代码哈:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<sstream>
#include<cstring>
#include<math.h>
#include<stdio.h>
#include<map>
#include<set>
#include<stack>
using namespace std;
struct Data{
int a;
double b;
}data[200010];
bool comp(Data a,Data b){
return a.a<b.a;
}
int main(){
int t,i,n,m,b;
double a;
cin>>t;
while(t--){
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf%d",&data[i].b,&data[i].a);
scanf("%d",&m);
m+=n;
for(i=n;i<m;i++)
scanf("%lf%d",&data[i].b,&data[i].a);
sort(data,data+m,comp);
data[m].a=0; data[m].b=0;
for(i=0;i<m;i++){
if(data[i].a==data[i+1].a){
data[i].b+=data[i+1].b;
if(fabs(data[i].b)>1e-6)
printf("%.2lf %d\n",data[i].b,data[i].a);
i++;
}
else{
if(fabs(data[i].b)>1e-6)
printf("%.2lf %d\n",data[i].b,data[i].a);
}
}
if(t)
printf("\n");
}
return 0;
}
第二题,把flush这种注释掉就可以AC哈。
#include<string.h>
#include<stdlib.h>
#define max 1002
typedef struct SqList{
int data[max];
int length;
}SqList;
SqList a;
int post,e;
void InitList(SqList &a){
a.length=0;
}
void ListInsert(SqList &a,int post,int e){
int j;
for(j=a.length;j>=post;j--)
a.data[j+1]=a.data[j];
a.data[post]=e;
a.length=a.length+1;
}
int ListDelete(SqList &a,int post,int &e){
int j;
if(post<1||post>a.length)
return 0;
e=a.data[post];
for(j=post+1;j<=a.length;j++)
a.data[j-1]=a.data[j];
a.length--;
return 1;
}
void ListGetelem(SqList &a,int post,int &e){
e=a.data[post];
}
int main(){
char ru[10]={"\0"};
int i,hh;
InitList(a);
scanf("%s",ru);
// fflush(stdin);
while(strcmp(ru,"exit")!=0){
if(strcmp(ru,"insert")==0){
scanf("%d",&hh);
for(i=0;i<hh;i++){
scanf("%d %d",&post,&e);
ListInsert(a,post,e);
}
}
else if(strcmp(ru,"delete")==0){
scanf("%d",&post);
if(ListDelete(a,post,e)==1)
printf("%d\n",e);
}
else if(strcmp(ru,"clear")==0)
InitList(a);
else if(strcmp(ru,"getelem")==0){
scanf("%d",&post);
ListGetelem(a,post,e);
printf("%d\n",e);
}
// fflush(stdin);
scanf("%s",ru);
// fflush(stdin);
}
return 0;
}
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询