Link:
https://www.nowcoder.com/acm/contest/116/B
Source: Niuke.com
Source: Niuke.com
Time limit: C/C++ 1 second, other languages 2 seconds
Space limit: C/C++ 32768K, other languages 65536K
64bit IO Format: %lld
Space limit: C/C++ 32768K, other languages 65536K
64bit IO Format: %lld
Topic description
Mr. Yang played a game for the students and asked to use multiplication and subtraction to represent a number. He gave everyone 9 cards, and then reported a number, and asked everyone to express the number in the form of an expression.
100 can be expressed as this Form: 100 = 129*67-8543, can also be expressed as: 100 = 13*489-6257
Note: In the expression, the numbers 1~9 appear respectively and only once (excluding 0).
For expressions like this, 100 has 20 representations.
Question requirements:
Read a positive integer N (N<1000 * 1000) from the standard input, and the
program outputs all the numbers that the number is composed of numbers 1~9 without repetition or omission.
Note: It is not required to output every representation, only count how many representations there are!
100 can be expressed as this Form: 100 = 129*67-8543, can also be expressed as: 100 = 13*489-6257
Note: In the expression, the numbers 1~9 appear respectively and only once (excluding 0).
For expressions like this, 100 has 20 representations.
Question requirements:
Read a positive integer N (N<1000 * 1000) from the standard input, and the
program outputs all the numbers that the number is composed of numbers 1~9 without repetition or omission.
Note: It is not required to output every representation, only count how many representations there are!
Enter description:
a positive integer N
Output description:
How many representations are there for the output
Example 1
enter
100
output
20
Remark:
Note that there is only one multiplication and one subtraction, and the * sign is guaranteed to be in front of the -
#include<iostream> using namespace std; int n,ans=0; bool fish [10]; int num[10]; void cal(int x,int y){ int a=0,b=0,c=0; for(int i=0;i<=x;i++){ a=a*10+num[i]; } for(int i=x+1;i<=y;i++){ b=b*10+num[i]; } for(int i=y+1;i<9;i++){ c=c*10+num[i]; } if((a*b-c)==n) years++; } void solve(){ for(int i=0;i<9;i++){ for(int j=i+1;j<9;j++){ cal(i,j); } } } void dfs(int d){ if(d==9){ solve(); return; } for(int i=1;i<10;i++){ if(!vis[i]){ vis[i]=true; num[d]=i; dfs(d+1); show [i] = false; } } } int main(){ cin>>n; dfs(0); cout<<ans<<endl; return 0; }
#include<iostream> #include<algorithm> using namespace std; int n,ans=0; bool fish [10]; int num[10]; void cal(int x,int y){ int a=0,b=0,c=0; for(int i=0;i<=x;i++){ a=a*10+num[i]; } for(int i=x+1;i<=y;i++){ b=b*10+num[i]; } for(int i=y+1;i<9;i++){ c=c*10+num[i]; } if((a*b-c)==n) years++; } void solve(){ for(int i=0;i<9;i++){ for(int j=i+1;j<9;j++){ cal(i,j); } } } int main(){ cin>>n; for(int i=0;i<9;i++){ num[i]=i+1; } int t=9*8*7*6*5*4*3*2*1-1; while(t--){ next_permutation(num,num+9); solve(); } cout<<ans<<endl; return 0; }