A. Chewbaсca and Number
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputLuke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t.
Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.
Input
The first line contains a single integer x (1 ≤ x ≤ 1018) — the number that Luke Skywalker gave to Chewbacca.
Output
Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.
Sample test(s)
Input
27
Output
22
Input
4545
Output
4444
题意:给一个数字x,比较x的每一位m与9-m的大小,取较小的那一位替换原来的位,最高位不能为0,输出这个最小数;
分析:给出数据已超过int范围改用longlong,如果数据更大考虑用字符串,同时数组存储每一位,注意最高位不能为0;
1 #include<cstring>
2 #include<cstdio>
3 #include<iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 long long n;
10 int a[20],j=0;
11 cin>>n;
12 for(;n!=0;n/=10)
13 {
14 int m=n%10;
15 if(n/10!=0&&9-m<m) m=9-m;
16 else if(n/10==0&&m!=9&&9-m<m) m=9-m;
17 a[j++]=m;
18 }
19 for(j--;j>=0;j--)
20 cout<<a[j];
21 cout<<endl;
22 }