[CF从零单排#23]514A - Chewbaсca and Number

题目来源:http://codeforces.com/problemset/problem/514/A

Luke 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 ≤ 10^18) — 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.

Examples
input
27
output
22
input
4545
output
4444

题目大意:

我们可以对数字进行“反转”,所谓反转,就是一个数字t,反转后变成9-t。如3,反转后变成6。现在给出一个正整数x(x<10^18),我们可以选择对某些位数上的数字进行反转,希望得到一个最小数。求反转后的最小的数,显然最高位不能是0。

题目分析:

显然,如果用整型来做的话,必须用long long ,但是其实这题可以用char数组来完成。很明显当t>=5时需要反转,否则不反转,最高位要特殊处理,最高位为9时不能反转,简单的字符数组模拟题。

参考代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
	char s[100];
	cin >> s;
	int len = strlen(s);
	if(s[0]>='5' && s[0]<'9')
		cout << '9'-s[0];
	else
		cout << s[0];
	for(int i=1; i<len; i++){
		if(s[i]>='5' && s[i]<='9')
			cout << '9'-s[i];
		else
			cout << s[i];
	}
	return 0;
} 
posted @ 2020-08-11 15:53  gdgzliu  阅读(134)  评论(0编辑  收藏  举报