第六屆藍橋杯-手鍊樣式
阿新 • • 發佈:2019-02-04
練習的時候想過用雜湊,想寫覺得太麻煩了,最後想了好久想了種簡單的思路,因為有翻轉和旋轉,
所以每次得到的樣式要旋轉12次,翻轉後再旋轉12次,在這過程中得到最小的數,然後跟已有的數進行檢查,如果沒有的話就是新的樣式了。
#include <iostream> #include <algorithm> using namespace std; long long arr[10000]; const int k = 12; int num; bool p(int *A) { long long sum = 0,tmp = 0,sum2 = 0; for(int m = 0;m < k;++m) sum = sum *10 + A[m]; for(int m = k-1;m >= 0;--m) sum2 = sum2 *10 + A[m]; tmp = sum; for(int m = 0;m < k;++m) { if(tmp%10 * 100000000000 + tmp/10 < sum) sum = tmp%10 * 100000000000 + tmp/10; tmp = tmp%10 * 100000000000 + tmp/10; } tmp = sum2; for(int m = 0;m < k;++m) { if(tmp%10 * 100000000000 + tmp/10 < sum2) sum2 = tmp%10 * 100000000000 + tmp/10; tmp = tmp%10 * 100000000000 + tmp/10; } sum = sum < sum2 ? sum : sum2; for(int m = 0;m < num;++m) if(sum == arr[m]) return false; arr[num++] = sum; return true; } int main() { int A[12] = {1,1,1,2,2,2,2,3,3,3,3,3}; num = 0; do { p(A); }while(next_permutation(A,A+12)); cout << num << endl; return 0; }