pku 2183 Bovine Math Geniuses(map)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
using namespace std;
map<int, int> numMap;
map<int, int>::iterator mIt;
int middle4ToInt(const char *str)
{
int a = 0;
for(int i = 1; i <= 4; i++)
{
a = a * 10 + str[i] - '0';
}
return a;
}
int last6(int a, char *str)
{
int n = a % 1000000, i;
for(i = 5; i >= 0; i--)
{
str[i] = n % 10 + '0';
n /= 10;
}
str[6] = 0;
/* for(a = 0, i = 0; i < 6; i++)
{
a = a * 10 + str[i] - '0';
}*/
return atoi(str);
}
void solve(char *str)
{
int a, step = 0;
while(true)
{
a = middle4ToInt(str);
// printf("a=%d\n", a); system("pause");
a *= a;
a = last6(a, str);
// printf("str=%s\n", str); system("pause");
++step;
mIt = numMap.find(a);
if(mIt == numMap.end())
{
numMap.insert(make_pair(a, step));
}
else
{
printf("%d %d %d\n", a, step - mIt->second, step);
return;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("indata.txt", "r", stdin);
#endif
char inStr[10];
while(scanf("%s", inStr) != EOF)
{
solve(inStr);
}
return 0;
}
浙公网安备 33010602011771号