Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include "iostream"
- #include "conio.h"
- #include "fstream"
- using namespace std;
- int N;
- fstream file;
- void NhapHau( int &N)
- {
- cout << "Nhap So Quan Hau Can Dat: ";
- cin >> N;
- file << N << endl;
- }
- short* x = new short[N];
- void VietNghiem()
- {
- static short i = 1;
- short j;
- file << "Cach thu " << i++ << endl;
- int k;
- for( j = 0; j < N; j++)
- file<<"["<<j+1<<"]"<<"["<<x[j]+1<<"]"<<" "<< endl;
- }
- short KiemTra( short i )
- {
- short j;
- for( j = 0 ; j < i ; j++) if( x[i] == x[j] || i - x[i] == j - x[j] || i + x[i] == j + x[j]) return 0;
- return 1;
- }
- void XepHau(short i )
- {
- short j;
- if( i == N )
- VietNghiem();
- else
- for( j = 0 ; j < N ; j++)
- {
- x[i] = j;
- if( KiemTra( i))
- XepHau( i+1);
- }
- }
- void main()
- {
- file.open("DeQuy_DatHau.txt", ios::out);
- NhapHau(N);
- XepHau( 0);
- file.close();
- delete []x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement