random generator: take a random 4-digit number, then square it, then divide it by 100, then take (minus) the remainder of division after divided by 10,000. Count number of "random generate" until the process loops. Example: 4100 -> 8100 -> 6100 -> 2100 -> 4100.
#include <iostream>
using namespace std;
int main ()
{
int a, b;
cin>>a;
for (int i=0;i<a;i++)
{ b=0;
cin>> b;
int c=b;
int loops =0;
do{b*=b; //Multiply it by itself (i.e. raise to power 2) to get value consisting of 8 digits
b/=100; //To truncate the 8-digit value, divide it by 100 and then take remainder of division by 10000.
b=b%10000;
loops++;
}while (b!=c); //To get more values, repeat from step 2.
cout<<loops<<" ";
}
}
Sample input: 13 9762 2057 6808 779 6382 5640 5546 6810 2665 5959 9511 1687 8090 Correct solution: 107 103 108 108 100 99 105 104 101 106 109 107 98
Aucun commentaire:
Enregistrer un commentaire