Find a permutation of 1234, so that the number of components can be divisible by 7

Source: Internet
Author: User

Is Tencent 2011 years of Pen test, I did not read the answer, I think of the stupid way, is literally, that

All possible permutations are first calculated, and then the calculation of each permutation is divisible by 7.

The key is to generate all permutations, fortunately studied the scheme language, the recursive has the experience, the code is as follows

#include <iostream> #include <stdio.h> using namespace std;
    int Calc (char *p_base_addr, char base_len) {int res = 0, radix = 1;
    for (int i = base_len-1; I >=0; radix *=, i--) {res + = radix * P_base_addr[i];
} return res;  } void Print_res (char *p_base_addr, char Base_len) {for (int i = 0; i < Base_len; i++) {printf ("%d",
    P_base_addr[i]);
} printf ("\ n");
    } void Swap_f (char *p1, char *p2) {char temp = *P1;
    *P1 = *P2;
*P2 = temp; } void Find7 (char *p_base_addr, Char Base_len, Char *p_cur_addr, char cur_len) {if (Cur_len = = 1) {int
        res = Calc (p_base_addr, Base_len);
        if (res% 7 = = 0) {printf ("==>");
    } print_res (P_base_addr, Base_len);
            }else{for (int i = 0; i < Cur_len; i++) {Swap_f (&p_cur_addr[0], &p_cur_addr[i]);
         Find7 (P_base_addr, Base_len, p_cur_addr+1, cur_len-1);   Remember to swap back!
        Swap_f (&p_cur_addr[0], &p_cur_addr[i]);
    }}} int main () {char a[4] = {1, 2, 3, 4};
    Find7 (A, 4, a, 4);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.