General comments on the last two questions of the pen question

Source: Internet
Author: User

Question 1:

This is a dynamic programming problem, the state transition equation is
Dp[i] = dp[i-3] + dp[i-1], i>= 3 (i<3 when dp[i]=1, only 1 cases)
I'm just opening a DP array here. Resolves the problem. Initialized in the Init method.
Also, given the convenience of testing, I used a recursive function, DFS (M,N,STR), to output all cases. See Code in detail: The function of a DFS function is to output all possible scenarios.
For example: When I enter 5, the output:
4
All Programs:
11111
211
121
112
At the same time I set the range of N, when n<0 or n is greater than the range I set, it will prompt an error.
Here is the C + + code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < string>using namespace Std;const int maxn = 1010;int dp[maxn];void init () {    dp[0] = dp[1] = dp[2] = 1;    for (int i=3;i<maxn;i++) dp[i] = Dp[i-1] + dp[i-3];} void Dfs (int m,int n,string str) {    if (m = = 0) {        cout << str << endl;        return;    }    if (M >= 1)        dfs (m-1, N, "1" +str);    if (M >= 3)        dfs (m-3, N, "2" +str);    return;} int main () {    int n;    Init ();    while (scanf ("%d", &n)! = EOF) {        if (N < 0) {            puts ("The length of this road cannot be negative!") \ n ");            Continue;        }        if (n >= 1010) {            puts ("I set the maximum length here is 1009, if you need to change the length, please contact the relevant personnel!") ");            Continue;        }        int ans = dp[n];        printf ("%d\n", ans);        printf ("All Programs: \ n");        DFS (n, N, "");    }    return 0;}

Question 2:


First, the binary number is obtained by code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;void get (int n) {    if (n = = 0) return;    Get (N/2);p rintf ("%d", n%2);} int main () {    get (3593);    return 0;}

1) A minimum of 12 threads is required.
First I put 3593 as a binary number, 111000001001, a co-owned 12 bits, and then I open 12 processes, the first process includes all the binary representation of the user of the I-bit 0, assuming that the algorithm can detect the offending user, then the offending user's first bit binary number is 0, Otherwise 1. This opens 12 threads to get the number of all bits of the user in the first place, so that the offending user's numbers are obtained.

2) A minimum of 6 is required.
First 6 bits of 3593 are inferred;
The second second infers 3593 of the next 6 bits.
Method with 1).

Note: 2) The answer is not necessarily right.

General comments on the last two questions of the pen question

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.