• 杭电1716


    排列2

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2076    Accepted Submission(s): 816


    Problem Description
    Ray又对数字的列产生了兴趣:
    现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
     
    Input
    每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
     
    Output
    对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
    每组输出数据间空一行,最后一组数据后面没有空行。
     
    Sample Input
    1 2 3 4
    1 1 2 3
    0 1 2 3
    0 0 0 0
     
    Sample Output
    1234 1243 1324 1342 1423 1432
    2134 2143 2314 2341 2413 2431
    3124 3142 3214 3241 3412 3421
    4123 4132 4213 4231 4312 4321
     
    1123 1132 1213 1231 1312 1321
    2113 2131 2311 3112 3121 3211
     
    1023 1032 1203 1230 1302 1320
    2013 2031 2103 2130 2301 2310
    3012 3021 3102 3120 3201 3210
     
    Source
     
    Recommend
    lcy
     
    Analyse:

     感觉这题和1027有点雷同,都是将一个数字的几个位调来调去。但是1027比较简单,每个输入只要求输出一个数字串,这个要的是全部数字串,但用的基本思路和1027一样(用函数的递归调用,以及对各个数字标记的方法)。首先升序排好各个数字,当然是由小到大地放好每个数字得出的数是最小的。每输出一个位的数字就标记该数字为不可用。

    View Code
     1 #include<stdio.h>
    2 #include<string.h>
    3 char num[30][6];//用来放生成的数字串
    4 int count;//作为生成的数字的索引
    5 int n[4]={6,2,2,1};
    6 void swap(int *x,int *y)
    7 {
    8 int temp;
    9 temp=*x;
    10 *x=*y;
    11 *y=temp;
    12 }
    13 void getnum(int a[4][2],int pos)//pos表示这是第几位的数字(万位为第1位)
    14 {
    15 int i,j;
    16 for(i=0;i<4;i++)
    17 {
    18 //遇到第一位是0则跳过
    19 if(pos==1 && a[i][0]==0)
    20 continue;
    21 if(a[i][1]==1)
    22 {
    23 for(j=0;j<n[pos-1];j++)
    24 num[count+j][pos-1]=a[i][0]+'0';
    25 //每次把最后一位的数字确定,count自增,指向下一个数字串
    26 if(pos==4)
    27 count++;
    28 //若未到最后一位则继续自我调用
    29 else
    30 {
    31 //标记为不可用
    32 a[i][1]=0;
    33 getnum(a,pos+1);
    34 //重新标记为可用
    35 a[i][1]=1;
    36 }
    37 }
    38 }
    39 }
    40 //此函数将重复的(后面的)数字串去掉,方法是在该数字串的头位放空字符做标记
    41 void delrepeat()
    42 {
    43 int i,j;
    44 for(i=1;i<24;i++)
    45 {
    46 for(j=0;j<i && strcmp(num[i],num[j])!=0;j++);
    47 if(j<i)
    48 num[i][0]='\0';
    49 }
    50 }
    51 main()
    52 {
    53 int a[4][2];
    54 int i,j,min;
    55 char lasthead;
    56 int FLAG=0;//FLAG作为是否第一个输入案例的记号
    57 while(scanf("%d%d%d%d",&a[0][0],&a[1][0],&a[2][0],&a[3][0])&&(a[0][0]||a[1][0]||a[2][0]||a[3][0]))
    58 {
    59 count=0;
    60 //每个数字串先初始化
    61 for(i=0;i<24;i++)
    62 num[i][0]=num[i][4]='\0';
    63 //先对所得的卡片升序排序
    64 for(i=0;i<3;i++)
    65 {
    66 min=i;
    67 for(j=i+1;j<4;j++)
    68 {
    69 if(a[min][0]>a[j][0])
    70 min=j;
    71 }
    72 swap(&a[min][0],&a[i][0]);
    73 }
    74 //全部标记为可用
    75 for(i=0;i<4;i++)
    76 a[i][1]=1;
    77 //逐个输出
    78 getnum(a,1);
    79 //去重
    80 delrepeat();
    81 lasthead='a';
    82 if(FLAG!=0)
    83 putchar('\n');
    84 FLAG=1;
    85 for(i=0;i<24;i++)
    86 {
    87 if(strlen(num[i])==0 )
    88 continue;
    89 if(lasthead!=num[i][0] && lasthead!='a')
    90 putchar('\n');
    91 printf(lasthead==num[i][0]?" %s":"%s",num[i]);
    92 lasthead=num[i][0];
    93 }
    94 printf("\n");
    95 }
    96 }




     

  • 相关阅读:
    Awesome Adb——一份超全超详细的 ADB 用法大全
    adb devices unauthorized的解决办法
    Vim用法AAAAA
    Security arrangements for extended USB protocol stack of a USB host system
    六 Python基础 字符串和编码
    四 Python基础
    三 Python解释器
    二安装Python
    Python教程(一)Python简介
    Python基本语法[二]
  • 原文地址:https://www.cnblogs.com/ZShogg/p/2432227.html
Copyright © 2020-2023  润新知