【luogu1012】拼数(字符串的操作+冒泡?)
2017-08-23 21:39
267 查看
题目:我是超链接
题解:
这破题怎么这么麻烦------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);
}
题解:
这破题怎么这么麻烦------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);
}
相关文章推荐
- P279_1012 用了典型的冒泡法对字符串排序。侧重指针应用,交换指针所指向的地址。
- C#字符串操作举例
- sap abap 对字符串的操作
- 标准c字符和字符串操作 atof() 函数
- C/C++ 内存块和字符串操作函数
- js 字符串操作函数
- c语言冒泡实现将一个字符串按照字典顺序输出
- C#之字符串格式化操作...
- NDK开发之字符串操作
- python字符串内建函数操作实例源码讲解
- 浅谈python中列表、字符串、字典的常用操作
- day05(补) 字符数组 字符串操作函数 宏定义
- python字符串操作
- 字符串类型的一些操作处理
- Java String字符串的操作
- 字符串操作
- Java字符串操作
- bash变量中字符串的操作
- php对字符串的操作
- JavaScript字符串截取、合并等操作