problem with task 24

Back to Problem Solutions forum

Igor Bushminsky     2016-04-25 16:12:54

My program works right for numbers 0001 and 4100, but it counts 86 iterations insted of 88 for number 5761

include <iostream>

include <string>

include <fstream>

using namespace std;

void itoMass(int iMass[], int iZnach) { for (int j = 0; j < 8; j++) { int iProv = 0; while (true) { iMass[7-j] = iZnach % 10; iProv = (iZnach % 10)*pow(2, j); if (iProv == iZnach || j==7) { break; } iZnach = iZnach / 10; j++; } iMass[7-j] = 0; } }

int main() { setlocale(0, ""); ofstream out; ifstream in; out.open("output.txt"); in.open("input.txt"); int iSize = 0; in >> iSize; for (int i = 0; i < iSize; i++) { int iNum = 0; in >> iNum; int iMass[1000]; int iCount = 0; iMass[0] = iNum; while (true) { iCount++; iNum = iNum*iNum; int iProm[8]; itoMass(iProm, iNum); iNum = iProm[2] * 1000 + iProm[3] * 100 + iProm[4] * 10 + iProm[5]; int i1 = 0;

        for (int j = 0; j < iCount; j++)
        {

            if (iNum == iMass[j])
            { 
                i1 = 1;
                cout << iNum << " " << iMass[j] <<  endl;

                break;
            }
        }
        if (i1 == 1) 
        {
            cout << "Нащел " << iCount << endl;
            break;
        }

        iMass[iCount] = iNum;
    }
}

out.close();
in.close();
system("pause");
return 0;

}

Quandray     2016-04-25 19:00:36
User avatar

Hi Igor,

My c++ compiler didn't like the pow(2,j) in your code, so I gave up looking at it.

However, when starting with 5761, what sequence does your code generate? My code gives

 0 5761
 1 1891
 2 5758
 3 1545
 4 3870
 5 9769
 6 4333
 7 7748
 8 0315
 9 0992
10 9840
11 8256
12 1615
13 6082
14 9907
15 1486
16 2081
17 3305
18 9230
19 1929
20 7210
21 9841
22 8452
23 4363
24 0357
25 1274
26 6230
27 8129
28 0806
29 6496
30 1980
31 9204
32 7136
33 9224
34 0821
35 6740
36 4276
37 2841
38 0712
39 5069
40 6947
41 2608
42 8016
43 2562
44 5638
45 7870
46 9369
47 7781
48 5439
49 5827
50 9539
51 9925
52 5056
53 5631
54 7081
55 1405
56 9740
57 8676
58 2729
59 4474
60 0166
61 0275
62 0756
63 5715
64 6612
65 7185
66 6242
67 9625
68 6406
69 0368
70 1354
71 8333
72 4388
73 2545
74 4770
75 7529
76 6858
77 0321
78 1030
79 0609
80 3708
81 7492
82 1300
83 6900
84 6100
85 2100
86 4100
87 8100
88 6100
Please login and solve 5 problems to be able to post at forum