hiho一下第214周《Sorting Photo Files》题目分析

3
0

《Sorting Photo Files》题目分析

本题是比较简单的字符串处理+排序题目。

只要能正确的把每个文件名分成字符串部分和整数部分,再排序即可。

  • 有问题讨论问题,不要说粗鄙之语。我看了一下你的程序,遍历HashMap没办法保证key是按字典序排列的吧?

  • 你说的对

  • 添加评论
  • reply

4 answer(s)

0
N=int(input()) photo=[] for i in range(N): photo.append(input()) photo.sort()
0

include

include

include

include

include

include

include

define N 100010

using namespace std;

struct node { char s[N]; char str[N]; int num; } d[110];

bool cmp(node a, node b) { if(strcmp(a.str, b.str) < 0) return true; else if(strcmp(a.str, b.str) > 0) return false; else { if(a.num < b.num) return true; else return false; } }

int main() { int n; scanf("%d\n", &n); for(int i = 0; i < n; i++) { char s[N]; scanf("%s", s); strcpy(d[i].s, s); int len = strlen(s); int flg = 0; d[i].num = 0; for(int j = 0; j < len; j++) { if((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z')) { d[i].str[j] = s[j]; } else { if(flg == 0) { d[i].str[j] = '\0'; flg = 1; } d[i].num = d[i].num * 10 + s[j] - '0'; } } } sort(d, d + n, cmp); for(int i = 0; i < n; i++) { puts(d[i].s); } return 0; }

0

N=int(input()) photo=[] for i in range(N): photo.append(input()) photo.sort()

  • 讨论区格式是markdown的,代码需要选中点一下“{}”图标(Code)

  • 添加评论
  • reply
0

Heading

write answer 切换为英文 切换为中文


转发分享