Jump to content
Ganav

Programare

Recommended Posts

Posted

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, 0

19 4 => 19 = 4^2 + 4^0 + 4^0 + 4^0 se vor afisa 2, 0, 0, 0

Posted

-- Removed something

P.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;
}

Posted
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

Posted
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.

Posted

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;
}

Posted (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 by Ganav
Posted (edited)

Banuiesc ca cele doua numere sunt naturale nenule... Uite solutia mea:

#include <iostream>

using namespace std;

void descompunere(int a, int B) {

// 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, B);

fflush(stdin);

getchar();

return 0;

}

Edited by totti93
Posted (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 by FreddieTux

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...