栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1520 Scramble Sort

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1520 Scramble Sort

#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 1005#define maxl 55struct Word{    char name[maxl];}word[maxn];int word_num, int_num;bool is_word[maxn];int number[maxn];void uni_case(char *st){    int len = strlen(st);    for (int i = 0; i < len; i++)        if (isupper(st[i])) st[i] -= 'A' - 'a';}bool operator < (const Word &a, const Word &b){    char aa[maxl];    char bb[maxl];    strcpy(aa, a.name);    strcpy(bb, b.name);    uni_case(aa);    uni_case(bb);    return strcmp(aa, bb) < 0;}void init(){    word_num = 0;    int_num = 0;    memset(is_word, 0, sizeof(is_word));}bool is_number(char *temp){    int len = strlen(temp);    for (int i = 0; i < len - 1; i++)        if (!((temp[i] >= '0' && temp[i] <= '9') || temp[i] == '-')) return false;    return true;}void insert(char *temp, int a){    if (is_number(temp))    {        sscanf(temp, "%d", &number[int_num]);        int_num++;    }    else    {        is_word[a] = true;        strcpy(word[word_num].name, temp);        word[word_num].name[strlen(word[word_num].name) - 1] = 0;        word_num++;    }}void output(int n){    int a = 0, b = 0;    for (int i = 0; i < n; i++)    {        if (is_word[i]) printf("%s", word[a++].name);        else printf("%d", number[b++]);        if (i != n - 1) printf(", ");        else printf(".n");    }}int main(){    char st[maxl];    while (scanf("%s", st), strcmp(st, "."))    {        init();        int a = 0;        while (st[strlen(st) - 1] != '.')        { insert(st, a++); scanf("%s", st);        }        insert(st, a++);        sort(number, number + int_num);        sort(word, word + word_num);        output(a);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号