POJ 1950 Dessert

Source: Internet
Author: User

Topic link ~ ~

Test Instructions: give you the number of n (1 <= n <= 15) (1. 2. 3. 4 ..... Add +,-, in the middle of these numbers. , so that the final result of the calculation is 0, if the situation is not greater than 20, then output all, otherwise up to the output of the top 20, according to the dictionary sequence output.

problem-solving ideas: This problem and in the Guangdong Regional competition in the warm-up of the same topic, deep search enumeration all the situation, is to deal with the point when you need to pay attention to, at the same time processing the situation is greater than or equal to 10 of the number should be multiplied by 100.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cstdlib> #include <stack> #include <queue> #include <map> #include <string> #include < Vector> #include <cmath>using namespace std; const int MX = + 5; const int INF = 0x3f3f3f3f; int n, ans; char S[MX]; void dfs (int num, int sum, int temp, int pre, int di)//sum already computed sum {if (n = = num) {if (s[num-1]! = ').        ') sum + = Temp*pre;            else//greater than or equal to 10 when special {if (num <) sum + = (temp*10 + num) *pre;        else sum + = (temp*100 + num) *pre;        } if (sum) return;        ans++;        if (ans >) return;        cout<<1;           for (int i = 2; I <= n; ++i) cout<< "<<s[i-1]<<" "<<i;        Cout<<endl;    return;    } int temp = temp;    if (s[num-1] = = '. ') {if (num <) TEMP = temp*10 + num;    else Temp = temp*100 + num;  } S[num] = ' + ';    + DFS (num+1, sum + temp*pre, num+1, 1, 0);   S[num] = '-';    -DFS (num+1, sum + temp*pre, num+1,-1, 0);  S[num] = '. ';    // .    if (!DI)//Put point DFS for the first time (num+1, SUM, num, pre, di+1);        else {if (num < 10)//Note >= 10 is required by DFS (num+1, SUM, temp*10 + num, pre, di+1);    else Dfs (num+1, SUM, temp*100 + num, pre, di+1);        }}int Main () {while (~SCANF ("%d", &n) {ans = 0;  S[0] = ' + '; DFS (1, 0, 1, 1, 0);    The main point is the situation of the special Cout<<ans<<endl; } return 0;}




POJ 1950 Dessert

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.