You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value x
).
Determine how the array will look after described operations are performed.
For example, consider the given array looks like [3,4,1,2,2,1,1]
. It will be changed in the following way: [3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].
If the given array is look like [1,1,3,1,1]
it will be changed in the following way: [1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].
The first line contains a single integer n
) — the number of elements in the array.
The second line contains a sequence from n
elements a1,a2,…,an ( 1≤ai≤109) — the elements of the array.
In the first line print an integer k
integers — the elements of the array after all the performed operations.
7 3 4 1 2 2 1 1
4 3 8 2 1
5 1 1 3 1 1
2 3 4
5 10 40 20 50 30
5 10 40 20 50 30
The first two examples were considered in the statement.
In the third example all integers in the given array are distinct, so it will not change.
类似与查找字母的操作所以本题应把数字当成字符串来操作
代码如下
#include<stdio.h>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int strtoint(string str)//字符串转数字
{
int result = 0;
int t = str.length();
for (int i = 0; i < t; i++)
{
result*= 10;
result += str[i] - '0';
}
return result;
}
int check(int dd,string str)//查找是否可行
{
int len = str.length();
int t = len - 1;
int count1=0,count2 = 0;
while (dd)
{
int m = dd % 10;
count1++;
dd /= 10;
char c = m + '0';
while (t >= 0)
{
if (str[t] == c)
{
count2++;
t--;
break;
}
t--;
}
}
if (count2 == count1)
return len - count1;
else
return -1;
}
int main()
{
string str;
cin >> str;
int d = strtoint(str);
d = sqrt(d);//对初始数据开方,从最大的遍历,既能减小复杂度也能满足从改变最小位数找起
int ans = -1;
while (d)
{
ans = check(d*d, str);
if (ans!=-1)
{
cout << ans << endl;
return 0;
}
d--;
}
cout << ans << endl;
return 0;
}