136 条题解
-
52016wudi LV 7 @ 2017-10-10 22:34:33
#include <iostream> #include <string> #include <algorithm> using namespace std; struct P{ string name; string mark; }a[1005]; bool cmp(P p,P q){ if(p.mark.size()==q.mark.size()){ if(p.mark==q.mark){ return p.name<q.name; } return p.mark>q.mark; } return p.mark.size()>q.mark.size(); } int main(){ ios::sync_with_stdio(false); int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i].name>>a[i].mark; } sort(a,a+n,cmp); for(int i=0;i<n;i++){ cout<<a[i].name<<endl; } return 0; }
string类
-
32017-09-18 11:21:21@
在sort中的cmp中直接定义水过。。。。。。。
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; struct node{ char a[25]; char b[10005]; int len; }shui[1005]; int n; int work(char *x,char *y,int lan){//比较数字大小 for(int i=0;i<lan;i++){ if(x[i]<y[i]) return 2; if(x[i]>y[i]) return 1; } return 0; } bool work2(char *x,char*y){//比较名字的大小 int lenx=strlen(x); int leny=strlen(y); for(int i=0;i<min(lenx,leny);i++){ if(x[i]<y[i]) return true; if(x[i]>y[i]) return false; } if(lenx<leny) return true; return false; } bool cmp(node x,node y){//先比较数字长度 if(x.len>y.len) return true; else if(x.len<y.len) return false; else{ int aa; if((aa=work(x.b,y.b,x.len))==1) return true; else if(aa==2) return false; else return work2(x.a,y.a); } } int main(){ scanf("%d\n",&n); for(int i=1;i<=n;i++){ scanf("%s\n%s\n",shui[i].a,shui[i].b); shui[i].len=strlen(shui[i].b); } sort(shui+1,shui+n+1,cmp); for(int i=1;i<=n;i++){ for(int j=0;j<strlen(shui[i].a);j++) printf("%c",shui[i].a[j]); printf("\n"); } return 0; }
-
22018-08-18 14:47:41@
只是个结构体排序嘛。。。
sort+cmp+string水过
AC#include<iostream> #include<algorithm> #include<string> using namespace std; struct hehe{ string a; string b; }a[1001]; bool cmp(hehe a,hehe b) { if(a.b==b.b) return a.a<b.a; if(a.b.length()!=b.b.length()) return a.b.length()>b.b.length(); return a.b>b.b; } int main() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i].a>>a[i].b; } sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { cout<<a[i].a<<endl; } return 0; }
-
22017-11-03 22:14:07@
最顶上的没有这个格式,不方便,希望顶上去
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;struct node{
char a[25];
char b[10005];
int len;
}shui[1005];int n;
int work(char *x,char *y,int lan){//比较数字大小
for(int i=0;i<lan;i++){
if(x[i]<y[i]) return 2;
if(x[i]>y[i]) return 1;
}
return 0;
}bool work2(char *x,char*y){//比较名字的大小
int lenx=strlen(x);
int leny=strlen(y);
for(int i=0;i<min(lenx,leny);i++){
if(x[i]<y[i]) return true;
if(x[i]>y[i]) return false;
}
if(lenx<leny) return true;
return false;
}bool cmp(node x,node y){//先比较数字长度
if(x.len>y.len) return true;
else if(x.len<y.len) return false;
else{
int aa;
if((aa=work(x.b,y.b,x.len))==1) return true;
else if(aa==2) return false;
else return work2(x.a,y.a);
}
}int main(){
scanf("%d\n",&n);
for(int i=1;i<=n;i++){
scanf("%s\n%s\n",shui[i].a,shui[i].b);
shui[i].len=strlen(shui[i].b);
}
sort(shui+1,shui+n+1,cmp);
for(int i=1;i<=n;i++){
for(int j=0;j<strlen(shui[i].a);j++)
printf("%c",shui[i].a[j]);
printf("\n");
}
return 0;
} -
22017-08-17 11:03:19@
冒泡轻松过,用字符串,strcmp+strcpy+strlen!!!!!!
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> using namespace std; struct bla { char ID[30]; char number[10010]; }a[1010]; int main(){ int n,i,j; char L[10010]; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%s\n%s",a[i].ID,a[i].number); } for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) { if(strlen(a[j].number)==strlen(a[j+1].number)) { if(strcmp(a[j].number,a[j+1].number)<0) { strcpy(L,a[j+1].number); strcpy(a[j+1].number,a[j].number); strcpy(a[j].number,L); strcpy(L,a[j+1].ID); strcpy(a[j+1].ID,a[j].ID); strcpy(a[j].ID,L); } else if(strcmp(a[j].number,a[j+1].number)==0) { if(strcmp(a[j].ID,a[j+1].ID)>0) { strcpy(L,a[j+1].ID); strcpy(a[j+1].ID,a[j].ID); strcpy(a[j].ID,L); } } } else if(strlen(a[j].number)<strlen(a[j+1].number)) { strcpy(L,a[j+1].number); strcpy(a[j+1].number,a[j].number); strcpy(a[j].number,L); strcpy(L,a[j+1].ID); strcpy(a[j+1].ID,a[j].ID); strcpy(a[j].ID,L); } } } for(i=1;i<=n;i++) puts(a[i].ID); return 0; }
-
12021-03-18 13:16:55@
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #include <vector> #include <deque> #include <set> #include <limits> #include <string> #include <sstream> using namespace std; const int oo_min=0xcfcfcfcf,oo_max=0x3f3f3f3f; class ubigint{ public: vector<int> num; int operator == (ubigint b) { if (num.size()!=b.num.size()) return 0; else { for (unsigned long long i=0,size=num.size();i<size;i++) if (num[i]!=b.num[i]) return 0; return 1; } } int operator != (ubigint b) { return ((!(*this==b))?1:0); } int operator < (ubigint b) { if (num.size()<b.num.size()) return 1; else if (num.size()>b.num.size()) return 0; else { for (unsigned long long i=0,size=num.size();i<size;i++) { if (num[size-1-i]<b.num[size-1-i]) return 1; else if (num[size-1-i]>b.num[size-1-i]) return 0; } return 0; } } int operator > (ubigint b) { if (num.size()<b.num.size()) return 0; else if (num.size()>b.num.size()) return 1; else { for (unsigned long long i=0,size=num.size();i<size;i++) { if (num[size-1-i]<b.num[size-1-i]) return 0; else if (num[size-1-i]>b.num[size-1-i]) return 1; } return 0; } } int operator <= (ubigint b) { return ((!(*this>b))?1:0); } int operator >= (ubigint b) { return ((!(*this<b))?1:0); } ubigint mov(long long x) { ubigint ans; ans.num.clear(); if (x+num.size()>0) { ans.num.resize(num.size()+x,0); for (long long i=0,size=num.size();i<size;i++) if (i+x>=0) ans.num[i+x]=num[i]; } else ans=0; return ans; } ubigint operator = (long long b) { char s[20+1]; sprintf(s,"%lld",b); num.clear(); num.resize(strlen(s)); for (unsigned long long i=0,size=num.size();i<size;i++) num[i]=(s[size-1-i]-'0'); return (*this); } ubigint operator = (string b) { num.clear(); num.resize(b.size()); for (unsigned long long i=0,size=num.size();i<size;i++) num[i]=(b[size-1-i]-'0'); return (*this); } ubigint operator + (ubigint b) { ubigint ans; ans.num.clear(); ans.num.resize(max(num.size(),b.num.size())); for (unsigned long long i=0,size=ans.num.size();i<size;i++) ans.num[i]=((i<num.size())?num[i]:0)+((i<b.num.size())?b.num[i]:0); for (unsigned long long i=0,size=ans.num.size();i<size;i++) { if (ans.num[i]>=10&&i==size-1) ans.num.resize(++size); ans.num[i+1]+=(ans.num[i]/10); ans.num[i]%=10; } return ans; } ubigint operator += (ubigint b) { return (*this=(*this+b)); } ubigint operator - (ubigint b) { ubigint ans; if ((*this)==b) { ans=0; return ans; } ans.num.clear(); ans.num.resize(max(num.size(),b.num.size())); for (unsigned long long i=0,size=ans.num.size();i<size;i++) ans.num[i]=((i<num.size())?num[i]:0)-((i<b.num.size())?b.num[i]:0); for (unsigned long long i=0,size=ans.num.size();i<size;i++) while (ans.num[i]<0) ans.num[i]+=10,ans.num[i+1]--; while (ans.num[ans.num.size()-1]==0) ans.num.resize(ans.num.size()-1); return ans; } ubigint operator -= (ubigint b) { return (*this=(*this-b)); } ubigint tm (ubigint b,unsigned long long pos) { ubigint ans,num_0; num_0=0; ans.num.clear(); if (*this!=num_0&&b!=num_0) { ans.num.resize(num.size()+pos); for (unsigned long long i=0,size=num.size();i<size;i++) ans.num[i+pos]=num[i]*b.num[pos]; for (unsigned long long i=0,size=ans.num.size();i<size;i++) { if (ans.num[i]>=10&&i==size-1) ans.num.resize(++size); ans.num[i+1]+=(ans.num[i]/10); ans.num[i]%=10; } } else ans=0; return ans; } ubigint operator * (ubigint b) { ubigint ans,num_0; ans=0,num_0=0; ans.num.clear(); if (*this!=num_0&&b!=num_0) { for (unsigned long long i=0,size=b.num.size();i<size;i++) ans+=(*this).tm(b,i); } return ans; } ubigint operator *= (ubigint b) { return (*this=(*this*b)); } ubigint operator / (ubigint b) { ubigint ans,num_0; num_0=0; if (*this<b) return num_0; if (*this!=num_0&&*this>num_0) { ubigint x,y; x=*this,y=b.mov(num.size()-b.num.size()); ans.num.resize(num.size()-b.num.size()+1,0); for (long long i=ans.num.size()-1;i>=0;i--,y=y.mov(-1)) while (x>=y) x-=y,ans.num[i]++; for (unsigned long long i=0,size=ans.num.size();i<size;i++) { if (ans.num[i]>=10&&i==size-1) ans.num.resize(++size); ans.num[i+1]+=(ans.num[i]/10); ans.num[i]%=10; } while (ans.num.size()>1&&ans.num[ans.num.size()-1]==0) ans.num.resize(ans.num.size()-1); } else ans=0; return ans; } ubigint operator /= (ubigint b) { return (*this=(*this/b)); } void print() { for (unsigned long long i=0,size=num.size();i<size;i++) printf("%d",num[size-1-i]); } }; class waterking{ public: char name[1<<5]; ubigint water; }; int n,num[1<<10]; waterking wk[1<<10]; int cmpwk(waterking a,waterking b) { return (a.water>b.water)||(a.water==b.water&&strcmp(a.name,b.name)<0); } void qs(int l,int r) { int i,j; waterking mid=wk[num[(l+r)>>1]]; for (i=l,j=r;i<j;) { while (cmpwk(wk[num[i]],mid)) i++; while (cmpwk(mid,wk[num[j]])) j--; if (i<=j) { swap(num[i],num[j]); i++,j--; } } if (l<j) qs(l,j); if (i<r) qs(i,r); } int main() { scanf("%d\n",&n); for (int i=0;i<n;i++) { num[i]=i; char s[1<<14]; memset(s,0,sizeof(s)); string ss; memset(wk[i].name,0,sizeof(wk[i].name)); scanf("%s\n%s\n",wk[i].name,s); ss=s,wk[i].water=ss; } qs(0,n-1); for (int i=0;i<n;i++) printf("%s\n",wk[num[i]].name); }
-
12017-10-02 19:21:32@
#include<iostream>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define maxa 1000+30
#define inf 10000000
using namespace std;
struct node
{
string id,num;
}e[maxa];
bool comp(node a,node b)
{
int i;
if(a.num==b.num)
{
for(i=0;i<a.id.length()&&i<b.id.length();++i)
if(a.id[i]!=b.id[i])
return a.id[i]<b.id[i];
if(a.id.length()<b.id.length())
return true;
}
else
{
if(a.num.length()!=b.num.length())
return a.num.length()>b.num.length();
else for(i=0;i<a.num.length()&&i<b.num.length();++i)
if(a.num[i]!=b.num[i])
return a.num[i]>b.num[i];
}
return false;
}
int main()
{
int n,i = 0;
cin>>n;
for(i=0;i<n;++i){
cin>>e[i].id>>e[i].num;
}
sort(e,e+n,comp);
for(i=0;i<n;++i)
cout<<e[i].id<<endl;
return 0;
} -
02020-06-06 02:19:27@
用了自定义排序规则的multimap,第3个测试WA,其它都AC,请教为什么
#include<set>
#include<algorithm>
#include<cstring>
#include <iostream>using namespace std;
struct Up {
char name[20];
unsigned long long n;
};struct Rule {
bool operator()(const Up & u1, const Up & u2) const {
if (u1.n != u2.n) return u1.n > u2.n;
else return (strcmp(u1.name, u2.name) < 0);
}
};int main(void) {
multiset<Up, Rule> ms;
Up u;
int n = 0;scanf("%d", &n);
for (int i = 0; i < n; i++) {
//scanf("%s", u.name);
//getchar();
//scanf("%ll", &u.n);
cin >> u.name >> u.n;
ms.insert(u);
}multiset<Up, Rule>::iterator p;
for (p = ms.begin(); p != ms.end(); p++) {
printf("%s\n", p->name);
}
return 0;
} -
02018-08-12 18:51:59@
#include <iostream>
#include<string>using std::string;
using std::cin;
using std::cout;
using std::endl;int PARTITION(int p, int r);
void QUICKSORT(int p, int r);string name[10000]; //储存水王ID
unsigned long long totcomments[10000]; //储存水王发帖数int main()
{
int totnum; //水王总数
cin>>totnum;
int nail=0;// 标志,记录读取水王数据
while(nail<totnum)
{
cin>>name[nail];
cin>>totcomments[nail];
nail++;
}
QUICKSORT(0, totnum-1);
for(int i=totnum-1;i>=0;i--)
{
cout<<name[i]<<endl;
}
return 0;
}int PARTITION(int p, int r) //快速排序核心是找到分界点,因此对名字同样进行分解即可
{
unsigned long long tip = totcomments[r];
int i=p-1;
for(int j=p;j<r;j++)
{
if(totcomments[j]<tip)
{
i=i+1;
unsigned long long temp=totcomments[i];
totcomments[i]=totcomments[j];
totcomments[j]=temp;string str=name[i];
name[i]=name[j];
name[j]=str; //同时交换ID
}
else if(totcomments[j] == tip)
{
if(name[j]>name[r])
{
i=i+1;
unsigned long long temp=totcomments[i];
totcomments[i]=totcomments[j];
totcomments[j]=temp;string str=name[i];
name[i]=name[j];
name[j]=str; //发帖数相同,同时交换ID,按字典顺序,注意输出倒序,因此大的ID在前
}
}
}
unsigned long long temp=totcomments[r];
totcomments[r]=totcomments[i+1];
totcomments[i+1]=temp;string str=name[r];
name[r]=name[i+1];
name[i+1]=str; //对姓名进行分界点交换,对发帖数排序同时保证对应ID跟着变化
return i+1;
}void QUICKSORT(int p, int r)
{
if(p<r)
{
int q=PARTITION(p, r);
QUICKSORT(p, q-1);
QUICKSORT(q+1,r);
}
} -
02018-08-12 18:47:32@
#include <iostream>
#include<string>using std::string;
using std::cin;
using std::cout;
using std::endl;int PARTITION(int p, int r);
void QUICKSORT(int p, int r);string name[5000]; //储存水王ID
unsigned long long totcomments[5000]; //储存水王发帖数int main()
{
int totnum; //水王总数
cin>>totnum;
int nail=0;// 标志,记录读取水王数据
while(nail<totnum)
{
cin>>name[nail];
cin>>totcomments[nail];
nail++;
}
QUICKSORT(0, totnum-1);
for(int i=totnum-1;i>=0;i--)
{
cout<<name[i]<<endl;
}
return 0;
}int PARTITION(int p, int r) //快速排序核心是找到分界点,因此对名字同样进行分解即可
{
unsigned long long tip = totcomments[r];
int i=p-1;
for(int j=p;j<r;j++)
{
if(totcomments[j]<tip)
{
i=i+1;
unsigned long long temp=totcomments[i];
totcomments[i]=totcomments[j];
totcomments[j]=temp;string str=name[i];
name[i]=name[j];
name[j]=str; //同时交换ID
}
else if(totcomments[j] == tip)
{
if(name[j]>name[r])
{
i=i+1;
unsigned long long temp=totcomments[i];
totcomments[i]=totcomments[j];
totcomments[j]=temp;string str=name[i];
name[i]=name[j];
name[j]=str; //发帖数相同,同时交换ID,按字典顺序,注意输出倒序,因此大的ID在前
}
}
}
unsigned long long temp=totcomments[r];
totcomments[r]=totcomments[i+1];
totcomments[i+1]=temp;string str=name[r];
name[r]=name[i+1];
name[i+1]=str; //对姓名进行分界点交换,对发帖数排序同时保证对应ID跟着变化
return i+1;
}void QUICKSORT(int p, int r)
{
if(p<r)
{
int q=PARTITION(p, r);
QUICKSORT(p, q-1);
QUICKSORT(q+1,r);
}
} -
02018-05-13 16:41:07@
用一个数组优化空间,记录起始位置和长度,冒泡过
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
struct node{
char name[24];
int st,len;
}list[1008];
char s[10008];
int n,a[1008],t[10008];void init(){
cin >> n;
int p=0,k;
for (int i=1;i<=n;i++){
cin >> list[i].name;
cin >> s;
list[i].st=p+1;
k=strlen(s);list[i].len=k;
for (int j=1;j<=k;j++){
t[++p]=s[j-1]-48;
}
}
}bool small(node x,node y){
if(x.len==y.len){
int x1=x.st,y1=y.st;
for (int i=1;i<=x.len;i++,x1++,y1++){
if (t[x1]>t[y1])return false;
else if(t[y1]>t[x1])return true;
}
if (strcmp(x.name,y.name)<0)return false;
else return true;
}
else return (y.len>x.len);
}void sort1(){
for (int i=1;i<=n;i++)a[i]=i;
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
if(small(list[a[i]],list[a[j]]))
{int tmp=a[i];a[i]=a[j];a[j]=tmp;}
for (int i=1;i<=n;i++){
cout << list[a[i]].name << endl;
}
}int main(){
init();
sort1();
return 0;
} -
02017-12-07 20:15:29@
#include<stdio.h>
#include<string.h>
#define N 100000+5
#define ll long long
struct shui
{
char name[20];
char tie[50];
}ren[1005];
int strcmpp(char x[],char y[])
{
int hh;
int lx=strlen(x),ly=strlen(y);
if(lx>ly) hh=1;
else if(ly>lx) hh=-1;
else hh=strcmp(x,y);
return hh;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",ren[i].name);
scanf("%s",ren[i].tie);
}
for(int i=1;i<n;++i)
{
for(int j=n;j>i;--j)
{
if(strcmpp(ren[j].tie,ren[j-1].tie)>0)
{
struct shui x;
strcpy(x.name,ren[j].name);strcpy(x.tie,ren[j].tie);
strcpy(ren[j].name,ren[j-1].name);strcpy(ren[j].tie,ren[j-1].tie);
strcpy(ren[j-1].name,x.name);strcpy(ren[j-1].tie,x.tie);
}
else if(strcmpp(ren[j].tie,ren[j-1].tie)==0)
{
if(strcmp(ren[j].name,ren[j-1].name)<0)
{
struct shui x;
strcpy(x.name,ren[j].name);strcpy(x.tie,ren[j].tie);
strcpy(ren[j].name,ren[j-1].name);strcpy(ren[j].tie,ren[j-1].tie);
strcpy(ren[j-1].name,x.name);strcpy(ren[j-1].tie,x.tie);
}
else continue;
}
else continue;
}
}
for(int i=1;i<=n;++i) puts(ren[i].name);
return 0;
} -
02009-09-13 20:25:54@
编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:运行时错误...|错误号: 216
├ 测试数据 05:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Unaccepted 有效得分:75 有效耗时:0ms编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Accepted 有效得分:100 有效耗时:0ms同样的程序
前N次交 216?。。。
重启 AC?。。。倭寇啊。。。。
经典~~搞笑~~~吐血~~~爷微笑~~~ -
02009-08-18 22:55:36@
喝矿泉水有利健康~
-
02009-08-17 16:10:24@
编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Accepted 有效得分:100 有效耗时:0ms冒泡都可以秒杀,晕……
-
02009-08-12 09:44:48@
哪个大哥告诉我 ,哪里错了?
type shui=record
name:string;
liang:ansistring;
end;
var a:array[1..1000]of shui;
n,b,j,i,c:longint;
k:shui;
begin
readln(n);
for i:=1 to n do begin
readln(a[i].name);
readln(a[i].liang);
b:=length(a[i].liang);
if b>c then c:=b;
end;
for i:=1 to n do begin
for j:=1 to c-length(a[i].liang) do insert('0',a[i].liang,1);
end;
for i:=1 to n-1 do
for j:=n downto i+1 do begin
if a[j-1].liang>a[j].liang then begin
k:=a[j-1];
a[j-1]:=a[j];
a[j]:=k;
end;
end;
for i:=1 to n-1 do begin
if (a[i].name -
02009-08-01 12:16:29@
MMD...水题交了5遍才AC...
-
02009-07-31 22:37:18@
我竟然用了两遍Qsort来对付一道水题。。。
而且是用选择排序就0msAC的题。。。 -
02009-07-25 16:34:36@
编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Accepted 有效得分:100 有效耗时:0ms
此水我竟WA一次。。。⊙﹏⊙b汗
就先把ID冒泡排一遍
然后比较发帖数先比长度,再直接用大于号比
哦了 -
02009-07-24 17:22:23@
按字典序输出.......郁闷