原题链接:https://codeforces.com/problemset/problem/1455/Ahttps://codeforces.com/problemset/problem/1455/A
翻译:
让我们来定义两个函数
f(x)(x是个正数):把x翻转并去掉反转后的前导0.
例如:f(123) = 321,f(110) = 11,f(3000000) = 3,f(333) = 333.
g(x) = x / f(f(x)),x同样也是一个正数.
你现在的任务是给你一个正数 n ,计算出所有 g(x) 不同函数值的个数, 1 <= x <= n;Input
第一行为 t ,代表接下来会有 t 组数据
接下来 t 行每行一个数字 n ,1 ≤ n < 10^100 (10的一百次方,数据保证 n 没有前导零.
Output
输出:
对于每输入的一个数字 **n **,请输出所有 g(x) 不同函数值的个数, 1 <= x <= n;
Example
input:
5
3
12345
1000000007
996
12345678901337426966631415output:
1
5
10
3
26Note
1:对于 3 来说,可能的 g(x) = {1};
2: 对于996 来说,可能的 g(x) = {1,10,100}; g(x) == 10 仅当 x == 10;g(x) == 100 仅当 x == 100.
思路:我们可以发现规律:有奇数个负数翻转后一定会留下一个负数,偶数个可全部翻转为正数。而0可正可负,所以有0即可全部为正。
代码实现:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll st[100005];
int main()
{
int N,j = 0,flag = 0;
ll m = 0,num = 0;
cin >> N;
if(N == 2){ //特殊情况
ll s,b;
cin >> s >> b;
num = max(s + b, -s - b);
cout << num << endl;
}
else{
for(int i = 0; i < N; i++)
{
scanf("%lld",&st[i]);
if(st[i] < 0){
st[i] = -st[i];
}
num += st[i];
if(st[i] == 0){
flag = 1;
}
}
sort(st,st + N);
if(flag == 1 || j % 2 == 0){
cout << num << endl;
}
else{
cout << num - 2*st[0] << endl;
}
}
return 0;
}