首页
学习
活动
专区
圈层
工具
发布
29 篇文章
1
PAT (Basic Level) Practice (中文)1047 编程团体赛
2
PAT (Basic Level) Practice (中文)1083 是否存在相等的差
3
PAT (Basic Level) Practice (中文)1082 射击比赛
4
PAT (Basic Level) Practice (中文)1081 检查密码
5
PAT (Basic Level) Practice (中文)1077 互评成绩计算
6
PAT (Basic Level) Practice (中文)1076 Wifi密码
7
PAT (Basic Level) Practice (中文)1064 朋友数
8
PAT (Basic Level) Practice (中文)1063 计算谱半径
9
PAT (Basic Level) Practice (中文)1057 数零壹
10
PAT (Basic Level) Practice (中文)1056 组合数的和
11
PAT (Basic Level) Practice (中文)1042 字符统计
12
PAT (Basic Level) Practice (中文)1041 考试座位号
13
PAT (Basic Level) Practice (中文)1023 组个最小数
14
PAT (Basic Level) Practice (中文)1022 D进制的A+B
15
PAT (Basic Level) Practice (中文)1019 数字黑洞
16
PAT (Basic Level) Practice (中文)1007 素数对猜想
17
PAT (Basic Level) Practice (中文)1091 N-自守数
18
PAT (Basic Level) Practice (中文)1026 程序运行时间
19
PAT (Basic Level) Practice (中文)1061 判断题
20
PAT (Basic Level) Practice (中文)1086 就不告诉你
21
PAT (Basic Level) Practice (中文)1016 部分A+B
22
PAT (Basic Level) Practice (中文)1012 数字分类
23
PAT (Basic Level) Practice (中文)1013 数素数
24
PAT (Basic Level) Practice (中文)1011 A+B 和 C
25
PAT (Basic Level) Practice (中文)1009 说反话
26
PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题
27
PAT (Basic Level) Practice (中文)1006 换个格式输出整数
28
PAT (Basic Level) Practice (中文)1004 成绩排名
29
PAT (Basic Level) Practice (中文)1002 写出这个数

PAT (Basic Level) Practice (中文)1023 组个最小数

1023 组个最小数

给定数字 0-9 各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意 0 不能做首位)。例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558。

现给定数字,请编写程序输出能够组成的最小的数。

输入格式:

输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0、数字 1、……数字 9 的个数。整数间用一个空格分隔。10 个数字的总个数不超过 50,且至少拥有 1 个非 0 的数字。

输出格式:

在一行中输出能够组成的最小的数。

输入样例:

2 2 0 0 0 3 0 0 1 0

输出样例:

10015558

分析:

首先列出0~9所有数字,如 输入 2 2 0 0 0 3 0 0 1 0 时,应列出 0 0 1 1 5 5 5 8 ,对列出的数字从小到大排序,从左到右,找出第一个不为0的数,作为最小数的首位,将剩下的数依次输出。

代码:

代码语言:javascript
复制
#include<stdio.h>

void fun(int *arr2,int t)
{
    int i,j;
    int index=0;
    int temp;
    for(i=0;i<t-1;i++)
    {
        index=i;
        temp=arr2[i];
        for(j=i+1;j<t;j++)
        {
            if(arr2[j]<=arr2[index]) index=j;
        }
        arr2[i]=arr2[index];
        arr2[index]=temp;
    }
}

void printfun(int *arr2,int n)
{
    int min=0;
    int i;
    for(i=0;i<n;i++)
    {
        if(arr2[i]!=0)
        {
          //  min=arr2[i];
          printf("%d",arr2[i]);
          arr2[i]=-1;
            break;
        }
    }
    for(i=0;i<n;i++)
    {
        if(arr2[i]>=0&&arr2[i]<=9)
        {
           // min=min*10+arr2[i];
           printf("%d",arr2[i]);
        }
    }
   // printf("%d\n",min);
   printf("\n");
}
int main()
{
    int arr1[10];
    int arr2[1000];
    int t=0;
    int i;
    int sum=0;
    int l=0;
    for(i=0;i<10;i++)
    {
        scanf("%d",&arr1[i]);
        sum+=arr1[i];
    }
    if(sum>50&&(sum-arr1[0]<=0))return 0;
    else{
    int j;
    for(i=0;i<10;i++)
    {
        for(j=1;j<=arr1[i];j++)
        {
            arr2[t++]=i;
        }
    }
    fun(&arr2,t);  //排序
    printfun(&arr2,t); //组最小数
    return 0;
    }


}
下一篇
举报
领券