Combo Problem Solving Report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
A three-bit passcode lock, each of which may be valued 1~n. There are two unlocking codes that are given.
The lock can be unlocked if the attempted password is equal or similar to an unlocked password (up to 2 per bit value).
For example: (4,5,6) is a two unlock password, then (1,n,5) (2,4,8) can be unlocked, and (1,5,6) is not.
"Data Range"
1<=n<=100
"Input Sample"
50
1 2 3
5 6 7
"Output Example"
249
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Direct enumeration.
It is important to note that two password-equal judgments are error prone (in cases where%n is required, the subscript does not start at 0, the difference is 2 and within the equivalent, and the difference 2 may occur simultaneously with%n).
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
Once again AC.
Incidentally, with the syntax of the next overloaded operator and the built-in function of the class, there is a small question: if I put add and inrange two functions into the key class inside the code, then operator== cannot invoke the two functions. If anyone understands, hope to enlighten ~
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:combo4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One intN; A - intAddintXintY) {return(x+y-1+n)%n+1; } - BOOLInRange (intXintY) {returnx==y | | Add (x,1) ==y | | Add (x,2) ==y | | Add (x,-1) ==y | | Add (x,-2)==y;} the - structKey - { - intx, y, z +FriendBOOL operator= = (Key A,key b) {returnInRange (a.x,b.x) && inrange (A.Y,B.Y) &&InRange (A.Z,B.Z);} - }; + A Key A, b; at - intMain () - { -Ifstreaminch("combo.in"); -Ofstream out("Combo.out"); - in inch>>N; - inch>>A.x>>A.y>>a.z; to inch>>B.x>>B.y>>b.z; + - ints=0; the Key t; * for(t.x=1; t.x<=n;++t.x) $ for(t.y=1; t.y<=n;++t.y)Panax Notoginseng for(t.z=1; t.z<=n;++t.z) s+= (t==a| | t==B); - out<<s<<Endl; the + inch. Close (); A out. Close (); the return 0; +}
Usaco Section1.3 Combination Lock Problem Solving report