Uva 725 ----------- Division
- // Uva 725 -------- Division
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- int main()
- {
- int iInput;
- int iFlag, iFlag2, iFlag3;
- bool second;
- int iBucket[10] = {0};
- int iRem[5] = {0}; // store number of abcde
- second = false;
- while ( cin >> iInput )
- {
- if ( iInput == 0 )
- break;
- if ( second )
- cout << endl;
- else
- second = true;
- int i,j,k,l,m;
- int iNum, iTemp, iTemp2,count;
- iFlag3 = 0;
- for ( i=12345 ; i<=98765 ; i++ )
- {
- memset (iBucket,0,sizeof(iBucket));
- iTemp = i;
- iFlag = j = 0;
- while ( iTemp > 0 )
- {
- iRem[j] = iTemp % 10;
- l = iTemp % 10;
- iTemp = iTemp / 10;
- for ( k=0 ; k<j ; k++ )
- {
- if (l != iRem[k])
- iFlag ++;
- else
- {
- iFlag = 0;
- break;
- }
- }
- if ( iFlag == 0 && j>0 )
- break;
- j++;
- }
- iFlag2 = 0;
- if ( iFlag == 10 )
- {
- for ( m=0 ; m<5 ; m++ )
- iBucket[iRem[m]]++;
- if ( i % iInput == 0 )
- {
- count++;
- iTemp2 = i/iInput;
- while ( iTemp2 > 0 )
- {
- if ( iBucket[iTemp2 % 10] == 0 )
- {
- iBucket[iTemp2 % 10]++;
- iFlag2 = 1;
- }
- else
- {
- iFlag2 = 0;
- break;
- }
- iTemp2 = iTemp2 / 10;
- }
- for ( m=1 ; m<=9 ; m++ )
- if ( iBucket[m] == 0 )
- iFlag2 = 0;
- if ( iFlag2 == 1 )
- {
- cout << i << " / ";
- if ( iBucket[0] == 0 )
- cout << "0";
- cout << i / iInput << " = " << iInput << endl;
- iFlag3 = 1;
- }
- }
- }
- }
- if ( iFlag3 == 0 )
- cout << "There are no solutions for " << iInput << "." <<endl;
- }
- }
hint: 固定 abcde 檢查無重複的數 然後除他給的數也就是N 可整除之後判斷商和被除數 有沒有把1~9全用掉
留言
張貼留言