Răspuns :
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("forus.in");
ofstream g("forus.out");
int v[31], i, c, n, num, copie, nrdiv, nrdivmin, nuzero, z1, z2,m;
int numdiv(int x)
{
int d, nr=0;
for (d=1; d*d<x; ++d)
{
if (x%d==0) nr+=2;
}
if (d*d==x) ++nr;
return nr;
}
int main()
{
f >> c >> n;
if (c==1)
{
for (i=1; i<=n; ++i)
{
f >> num;
nuzero=1;
while (num && nuzero)
{
if (num%10==0) nuzero=0;
num/=10;
}
if (nuzero) ++m;
}
g << m;
}
else
{
int a, b, c, gasit;
for (i=1; i<=n; ++i)
{
f >> num;
copie=num;
z1=1; z2=1;
while (copie>9) {z1*=10; copie/=10;}
copie=num;
nrdivmin=numdiv(num);
gasit=num;
while (z1!=1)
{
a=num/z1; b=num%z1;
z1/=10; z2*=10;
if ((b/z1)!=0)
{
c=b*z2+a;
nrdiv=numdiv(c);
if (nrdiv<nrdivmin) { nrdivmin=nrdiv; gasit=c; }
else if (nrdiv==nrdivmin) gasit=min(c,gasit);
}
}
g << gasit << " ";
}
}
return 0;
}