zoukankan      html  css  js  c++  java
  • K

    题目:

    ay又对数字的列产生了兴趣: 
    现有四张卡片,用这四张卡片能排列出很多不同的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 Output1234 1243 1324 1342 1423 14322134 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

    解法一:
    这里使用了一个函数:next_permutation()
    使得我很快得到了全部的全排列,但是在格式话输出上花了很长的时间
    1 #include <stdlib.h>
     2 #include <algorithm>
     3 #include <string>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 int t[4];
     9 
    10 int pand (int a, int b, int c, int d)      //这个函数可以得到有几个非0且不相同的数
    11 {
    12     int i = 0;
    13     if(a != 0)
    14         t[i++] = a;                
    15     if(b != 0&& b!= a)
    16         t[i++] = b;
    17     if(c != 0&& c!= b)
    18         t[i++] = c;
    19     if(d != 0&& d!= c)
    20         t[i++] = d;
    21     return i;
    22 }
    23 
    24 int main()
    25 {
    26     int a[4],i,temp=1,s=1;
    27     while(1)
    28     {
    29         cin>>a[0]>>a[1]>>a[2]>>a[3];
    30         temp=1;
    31 
    32          
    33         if(a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0)
    34             break;
    35        if(s)    //这是我在提交多次都格式出错后,同学教我的,
    36         {
    37           s=0;
    38         }
    39         else
    40           cout<<endl;
    41         sort(a, a+4);
    42 
    43         i =  pand(a[0],a[1],a[2],a[3]);
    44         int i0 = 0;
    45         do{
    46 
    47             if(a[0] != 0&&a[0]==t[i0])
    48             {
    49                 if(temp == 1)
    50                     {   cout << a[0]<< a[1]<< a[2] <<a[3];  temp++; }
    51                 else
    52                     cout <<' '<< a[0]<< a[1]<< a[2] <<a[3];
    53             }
    54             else if(a[0] != 0&&a[0]==t[++i0]&&i0<i)
    55             {
    56                 cout<<endl;
    57                 cout << a[0]<< a[1]<< a[2] <<a[3];
    58             }
    59 
    60         }while (next_permutation(a, a+4));
    61         cout<<endl;
    62     }
    63 
    64     return 0;
    65 }

     
  • 相关阅读:
    Linux 部署 nginx
    Linux 部署vue项目(使用nginx)
    git 操作规范
    mysql grant权限分配(转)。
    前端优化,包括css,jss,img,cookie
    关于js里的那一堆事件
    个人作业——软件工程实践总结作业
    Unity3D 快捷键
    Beta冲刺第二天
    Beta冲刺第一天
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7196931.html
Copyright © 2011-2022 走看看