Poate cineva sa ma ajute cu problema 7, va rog? (C++)
Răspunsuri la întrebare
Răspuns:
#include <iostream>
using namespace std;
int num1, num2, cif, z,u,d,t,p,c,s,st,o,n;
int z2,u2,d2,t2,p2,c2,s2,st2,o2,n2;
int main()
{
cout << "dati primul numar: "; cin >> num1;
if (num1==0) z=1;
else
{
while (num1!=0)
{
cif=num1%10; num1=num1/10;
if (cif==0) z=1;
if (cif==1) u=1;
if (cif==2) d=1;
if (cif==3) t=1;
if (cif==4) p=1;
if (cif==5) c=1;
if (cif==6) s=1;
if (cif==7) st=1;
if (cif==8) o=1;
if (cif==9) n=1;
}
}
cout << "dati al doilea numar: "; cin >> num2;
if (num2==0)
{if (z==1) z2=1;}
else
{
while (num2!=0)
{
cif=num2%10; num2=num2/10;
if (cif==0) z2=1;
if (cif==1) u2=1;
if (cif==2) d2=1;
if (cif==3) t2=1;
if (cif==4) p2=1;
if (cif==5) c2=1;
if (cif==6) s2=1;
if (cif==7) st2=1;
if (cif==8) o2=1;
if (cif==9) n2=1;
}
}
cout << "Cifrele comune: ";
if (n==1 && n2==1) cout << 9 << " ";
if (o==1 && o2==1) cout << 8 << " ";
if (st==1 && st2==1) cout << 7 << " ";
if (s==1 && s2==1) cout << 6 << " ";
if (c==1 && c2==1) cout << 5 << " ";
if (p==1 && p2==1) cout << 4 << " ";
if (t==1 && t2==1) cout << 3 << " ";
if (d==1 && d2==1) cout << 2 << " ";
if (u==1 && u2==1) cout << 1 << " ";
if (z==1 && z2==1) cout << 0 << " ";
}
Explicație:
#include <iostream>
using namespace std;
int num1, num2, cif, v1[10], v2[10];
int main()
{
cout << "dati primul numar: "; cin >> num1;
if (num1==0) v1[0]=1;
else {
while (num1!=0)
{
cif=num1%10;
v1[cif]=1;
num1/=10;
}}
cout << "dati al doilea numar: "; cin >> num2;
if (num2==0) v2[0]=1;
else{
while (num2!=0)
{
cif=num2%10;
v2[cif]=1;
num2/=10;
}}
cout << "cifre comune: ";
for (cif=9; cif>=0; --cif)
if (v1[cif]==1 && v2[cif]==1) cout << cif << " ";
return 0;
}