Ganav Posted June 7, 2014 Report Share Posted June 7, 2014 Realizeaza un program(C/C++ fara alte biblioteci) care citeste doua numere si il scrie pe primul ca si suma de factori din cel de al doilea:Ex: 26 5 => 26 = 5^2 + 5^0 deci se vor afisa 2, 019 4 => 19 = 4^2 + 4^0 + 4^0 + 4^0 se vor afisa 2, 0, 0, 0 Quote Link to comment Share on other sites More sharing options...
Robert1995 Posted June 7, 2014 Report Share Posted June 7, 2014 -- Removed somethingP.S nu e testat#include <iostream>int main() { int number,divider; std::cin>>number>>divider; std::cout<<number<<" ^ "<<(number / divider); for(int i = 1; i <= number % divider; std::cout<<number<<" ^ 0", i++); return 0;} Quote Link to comment Share on other sites More sharing options...
Ganav Posted June 7, 2014 Author Report Share Posted June 7, 2014 Incearca in continuare Quote Link to comment Share on other sites More sharing options...
Robert1995 Posted June 7, 2014 Report Share Posted June 7, 2014 Incearca in continuare N-am fost atent, merge bine, am testat si cu numere gen 156, am facut cu math.h, sqrt si pow.#include <iostream>#include <math.h>int main() { int number,divider, mySum, displayed = 0; std::cin>>number>>divider; mySum = number; do { int currentCut = 0; while(pow(divider, currentCut) < mySum) { currentCut++; } if(currentCut != 0) currentCut--; if(displayed != 0) std::cout<<" + "; else displayed = 1; std::cout<<divider<<" ^ "<<currentCut; mySum-= pow(divider, currentCut); } while(mySum != 0); return 0;}Online Demo : https://ideone.com/K2DZUl Quote Link to comment Share on other sites More sharing options...
Ganav Posted June 7, 2014 Author Report Share Posted June 7, 2014 Aproape bine. Quote Link to comment Share on other sites More sharing options...
Silviu Posted June 8, 2014 Report Share Posted June 8, 2014 Aproape bine.B?, tu î?i pui temele de cas? ca ?i challenge-uri pe forum? Dac? nu, arat? ce ai f?cut tu, cum ai rezolvat problema ?i nu mai umple categoria asta a forumului de probleme de clasa a 10-a. Quote Link to comment Share on other sites More sharing options...
FreddieTux Posted June 8, 2014 Report Share Posted June 8, 2014 Mi se pare ca avea si o solutie mai eleganta, dar si asta merge.#include <iostream>#include <math.h>using namespace std;int main() { int number, divider; cin >> number>>divider; cout << number << " = "; int maxPower = 0; int tmp = number; while (tmp >= divider) { tmp /= divider; maxPower++; } while (maxPower >= 0) { while (number - pow(divider, maxPower) >= 0) { number -= pow(divider, maxPower); cout << divider << "^" << maxPower << (number ? " + " : ""); } maxPower--; } return 0;} Quote Link to comment Share on other sites More sharing options...
Ganav Posted June 8, 2014 Author Report Share Posted June 8, 2014 (edited) M-am gandit cam care ar fi o problema cat de cat acceptabila, care sa fie legata de programare si sa aiba ca enunt o singura propozitie/fraza. Varianta de sus nu ia in calcul numerele 0 1, 0 0 si 1 0 de aceea este aproape corecta. Era o problema la un extemporal in clasa a IX-a, semestrul II, parca. Edited June 9, 2014 by Ganav Quote Link to comment Share on other sites More sharing options...
totti93 Posted June 8, 2014 Report Share Posted June 8, 2014 (edited) Banuiesc ca cele doua numere sunt naturale nenule... Uite solutia mea:#include <iostream>using namespace std;void descompunere(int a, int { // Pana cand nu stricam numarul while (a) { int t = 1; // Contor pentru rezultatul puterilor int nr = 0; // Contor pentru puteri // Ridicam la putere pana nu depasim numarul `a` while (t * b < a) { t *= b; // Aici e ridicarea la putere nr++; // Incrementam puterea } // Obtinem diferenta dintre rezultatul puterilor si `a` (restul) a -= t; // Afisam puterea cout << nr << ','; }}int main() { int a, b; cout << "a="; cin >> a; cout << "b="; cin >> b; descompunere(a, ; fflush(stdin); getchar(); return 0;} Edited June 8, 2014 by totti93 Quote Link to comment Share on other sites More sharing options...
FreddieTux Posted June 8, 2014 Report Share Posted June 8, 2014 (edited) Da, ai dreptate, nu functioneaza pentru acele cazuri particulare. Problema se aseamana cu scrierea unui numar in baza n, doar ca in loc sa scriem 203(8), vom scrie 8^2 + 8^0 + 8^0 + 8^0, respectiv 2,0,0,0. Apropo, "0 1" nu e un caz valid.#include <iostream>#include <stdio.h>#include <list>using namespace std;list<int> out;void pushPower(int power, int num){ for(int i=0; i<num;i++){ out.push_front(power); }}void printPower(){ for(list<int>::iterator iter = out.begin(); iter != out.end(); iter++){ cout<<*iter<<((next(iter) != out.end())?", ":""); }}int main(){ int number, divider; cout<<"Number: "; cin>>number; cout<<"Divider: "; cin>>divider; if(number == 0 && divider == 0){ cout<<0; getchar(); return 0; } else if(divider == 0 || divider == 1){ for(int i=0;i<number;i++){ cout<<"0"<<(i<number-1?", ":""); } getchar(); return 0; } int power; for(power=0; number>=divider; number /= divider,power++){ pushPower(power,(number % divider)); } pushPower(power,number); printPower(); getchar(); return 0;} Edited June 8, 2014 by FreddieTux Quote Link to comment Share on other sites More sharing options...