zoukankan      html  css  js  c++  java
  • n个数中选取m个数,并全排列

    问题描述如下:

    从下列乘法竖式中,每一个星号代表一个数位。若出现的数字有且仅有2,3,5,7四种,你能将此竖式完全还原嘛?

        

    答案:775*33 = 25575(2325+23250)

    进一步,若将题目中的2,3,5,7改为其他互异的四个数字,还存在要求的乘法竖式吗?

    分析:

    该题,利用竖式乘法解决并不困难,主要需要处理的地方是“如何在4位数字中挑选3个数字生成第一个乘数,如何在4位数字中挑选2个数字生成第二个乘数”,并且按照题意,数字的挑选是可以重复的,也就是允许777这样的乘数存在。

    第一种解法:直接用数组模拟整个计算过程。

     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int main(){
     7     int number[4] = {2,3,5,7};
     8 
     9     for(int i=0;i<4;i++) {  //遍历first的第1位,遍历每一位的可能的取值
    10         for(int j=0;j<4;j++ ) {  //遍历first的第2位,遍历每一位的可能的取值
    11             for(int k=0;k<4;k++){//遍历first的第3位,遍历每一位的可能的取值
    12                 for(int m=0;m<4;m++){//遍历second的第1位,遍历每一位的可能的取值
    13                     for(int n=0;n<4;n++){ //遍历second的第2位,遍历每一位的可能的取值
    14                         int first[3]={0};
    15                         int second[2]={0};
    16                         int a1[4]={0},a2[4]={0};
    17                         int mul_res[5]={0};
    18 
    19                         int first_temp = number[i]*100+number[j]*10+number[k];
    20                         int a1_temp = first_temp * number[m];  //得到a1的数值
    21                         int a2_temp = first_temp * number[n];  //得到a2的数值
    22                         int mul_temp = a1_temp+a2_temp*10;     //得到mul结果的各位数值
    23 
    24                         int index1=0,index2=0,index3=0;
    25 
    26                         while(a1_temp>0){
    27                             a1[index1++] = a1_temp%10;
    28                             a1_temp = a1_temp/10;
    29                         }
    30 
    31                         while(a2_temp>0){
    32                             a2[index2++] = a2_temp%10;
    33                             a2_temp = a2_temp/10;
    34                         }
    35 
    36                         while(mul_temp>0){
    37                             mul_res[index3++] = mul_temp%10;
    38                             mul_temp = mul_temp/10;
    39                         }
    40 
    41                         bool outt = 1;
    42                         if(index1!=4||index2!=4||index3!=5) continue;
    43                         else{
    44                                 //检测a1中的数字是否都在number中
    45                             for(int x=0;x<4;x++){
    46                                 if(a1[x]!=number[0]&&a1[x]!=number[1]&&a1[x]!=number[2]&&a1[x]!=number[3] ){
    47                                     outt = 0;
    48                                     break;
    49                                 }
    50 
    51                             }
    52 
    53                             //检测a2中的数字是否都在number中
    54                             for(int x=0;x<4;x++){
    55                                 if(a2[x]!=number[0]&&a2[x]!=number[1]&&a2[x]!=number[2]&&a2[x]!=number[3] ){
    56                                     outt = 0;
    57                                     break;
    58                                 }
    59                             }
    60 
    61                             //检测mul_res中的数字是否都在number中
    62                             for(int x=0;x<5;x++){
    63                                 if(mul_res[x]!=number[0]&&mul_res[x]!=number[1]&&mul_res[x]!=number[2]&&mul_res[x]!=number[3] ){
    64                                     outt = 0;
    65                                     break;
    66                                 }
    67 
    68                             }
    69                         }
    70 
    71                         if(outt==1){
    72                             cout<<number[i]<<number[j]<<number[k];
    73                             cout<<"*";
    74                             cout<<number[n]<<number[m];
    75                             cout<<"=";
    76                             for(int w=4;w>=0;w--) cout<<mul_res[w];
    77                             cout<<endl;
    78 
    79                         }
    80 
    81                     }
    82                 }
    83             }
    84         }
    85     }
    86 
    87     system("pause");
    88     return 0;
    89 }
    数组模拟

    第二种解法:从7,7,7,5,5,5,3,3,3,2,2,2中选取3个数组成第一个乘数,选取2个数组成第二个数。这就用到了组合数全排列

  • 相关阅读:
    Contest20140710 eagleeggs
    bzoj 1059: [ZJOI2007]矩阵游戏 二分图匹配
    Quick-Cocos2d-x初学者游戏教程(二) -------------------- Quick内部的代码结构及相应的原理
    Quick-Cocos2d-x初学者游戏教程(一)--------------------Quick的一些基础知识
    使用Cocos Studio 1.6 编辑人物属性界面
    Cocos2d-x 3.0 开发(十四)使用UIScrollView 实现大小不同物品拖动展示
    使用Cocos Studio 1.6 编辑开始界面
    Cocos2d-x Json用法
    Cocos2d-x xml解析
    plist文件操作
  • 原文地址:https://www.cnblogs.com/liugl7/p/9093477.html
Copyright © 2011-2022 走看看