PAT自主训练记录 甲级1038 Recover the Smallest Number

A1038 Recover the Smallest Number (30分)

题目描述

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (≤ 1 0 4 10 ^4 104
​​ ) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

22932132143287

题目大意

给N个数,找出使得最后数字值最小的一种排列结果。如果全是0,则只输出一个0

最初思路
将数字用字符的形式输入,按照字典序排序输出结果,前导0不输出。

过程中犯的错误

  • 纯粹按照字典序排序是错误的,例如5 32 321 3214 0299 87这个测试例子,按照字典序排的话,32会排在321前面,这样最后的结果就不是最小的;
  • 忽略了前导0可能不止一个的情况;
  • 没有考虑当输入的结果全是0的情况下,只输出一个0的情况;
  • 前面三个地方都修改了之后,改成下面这个样子提交,测试点5还是答案错误。想了很久,甚至搜别人的正确代码,感觉自己的思路就是对的,一直没发现自己错哪了…估计是排序那边的思路的问题,就不停地举反例,终于找到一个反例。
    例:32 3231这两个数应该 是 3231 32。但是按照我下面这个错误代码的思路,323排在32313前面导致结果是32 3231。这样就出错了(想出这个反例太不容易了…┭┮﹏┭┮)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct Num{
    char str[9];
    int len;
}num[10001];

bool cmp(Num &s1,Num &s2){
    char temp1[20],temp2[20];
    strcpy(temp1,s1.str);
    temp1[s1.len]=s2.str[0];
    strcpy(temp2,s2.str);
    temp2[s2.len]=s1.str[0];
    if(strcmp(temp1,temp2)<0){
        return true;
    }
    else
        return false;
}

int main(){
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%s",num[i].str);
        num[i].len=strlen(num[i].str);
    }

    sort(num,num+N,cmp);
    //for(int i=0;i<N;i++)
        //puts(num[i].str);
    int si=0,sj=0;
    for(si=0;si<N;si++){
        for(sj=0;sj<num[si].len;sj++){
            if(num[si].str[sj]!='0')
                break;
        }
        if(sj<num[si].len)
            break;
    }
    if(si==N)
        printf("0");
    else{
        for(int j=sj;j<num[si].len;j++){
            printf("%c",num[si].str[j]);
        }
        for(int i=si+1;i<N;i++){
            for(int j=0;j<num[i].len;j++)
                printf("%c",num[i].str[j]);
        }
    }
    return 0;
}

  • 最后看大家都是用a+b<b+a来进行排序的,索性就改了一下通过了。

最终正确代码

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct Num{
    char str[9];
    int len;
}num[10001];

bool cmp(Num &s1,Num &s2){
    char temp1[20],temp2[20];
    strcpy(temp1,s1.str);
    strcat(temp1,s2.str);
    //temp1[s1.len]=s2.str[0];
    strcpy(temp2,s2.str);
    strcat(temp2,s1.str);
    //temp2[s2.len]=s1.str[0];
    if(strcmp(temp1,temp2)<0){
        return true;
    }
    else
        return false;
}

int main(){
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%s",num[i].str);
        num[i].len=strlen(num[i].str);
    }

    sort(num,num+N,cmp);
    //for(int i=0;i<N;i++)
        //puts(num[i].str);
    int si=0,sj=0;
    for(si=0;si<N;si++){
        for(sj=0;sj<num[si].len;sj++){
            if(num[si].str[sj]!='0')
                break;
        }
        if(sj<num[si].len)
            break;
    }
    if(si==N)
        printf("0");
    else{
        for(int j=sj;j<num[si].len;j++){
            printf("%c",num[si].str[j]);
        }
        for(int i=si+1;i<N;i++){
            for(int j=0;j<num[i].len;j++)
                printf("%c",num[i].str[j]);
        }
    }
    return 0;
}

此次发现自己不熟悉的知识点

  • strcmp函数 x<y返回负数,x>y返回正数
  • strcpy函数 字符串复制
  • strcat函数 字符串拼接
  • c++里面的string型变量

算法笔记里的思路

大致排序思路都是一样的,不过使用的是C++里的string型来方便排序,后续结果字符拼接也很方便(因为字符串数组是不便直接用sort来排序的)使用erase函数去除前导0。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值