【luogu1012】拼数(字符串的操作+冒泡?)

来源:互联网 发布:日本人彻底疯了 知乎 编辑:程序博客网 时间:2024/05/18 06:15

题目:我是超链接

题解:

这破题怎么这么麻烦------Shallwe

因为考虑了341 34这种情况,而且字符串又不按照我想象的方法排序,必须一个一个冒泡走起啦

代码:

#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>using namespace std;int n,i,j;char xx[100050],yy[100050];struct word{char st[100050];}ww[22];int cmp(word a,word b){return strcmp(a.st,b.st)>0;}int main(){scanf("%d",&n);for (i=1;i<=n;i++) scanf("%s",&ww[i].st);sort(ww+1,ww+n+1,cmp);bool bo=false;i=1;while (!bo){bo=true;for (j=1;j<=n-i;j++){memset(xx,0,sizeof(xx));      memset(yy,0,sizeof(yy));strcat(xx,ww[j].st);      strcat(xx,ww[j+1].st);      strcat(yy,ww[j+1].st);      strcat(yy,ww[j].st);      if (strcmp(xx,yy)<0) swap(ww[j+1],ww[j]);bo=false;}    i++;}for (i=1;i<=n;i++) printf("%s",ww[i].st);}


原创粉丝点击