Google kickStart-2018-RoundA-Problem A. Even Digits

2023-12-24 18:48

本文主要是介绍Google kickStart-2018-RoundA-Problem A. Even Digits,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接

题目大意:

给定一个数N,N中的每一位数都不能是奇数,如果有奇数则改成与它最近的数M(M中的每一个数都是偶数)
比如:N=2018,1是奇数,改成M=2020,11改成8

直接上官方题解(可以直接看Large dataset):

Even Digits: Analysis.
To make our discussion easier, let us define a beautiful integer as an integer which has only even digits in its decimal representation.

Small dataset

One useful observation for solving this problem is that we either only want to press the minus button or only want to press the plus button. It is useless to have both types in the same sequence, since they cancel each other out.

Therefore, we want to either keep pressing the minus button, or keep pressing the plus button, until we have a beautiful integer. Let M be the minimum number of minus button presses before reaching a beautiful integer, and let P be the minimum number of plus button presses before reaching a beautiful integer. Then the answer is the smaller of M and P.

Note that for this problem, the answer for an input N is bounded above by N, since we can just press the minus button N times to reach the beautiful integer 0. Therefore, since N ≤ 105, we can solve the Small dataset using complete search.

We can loop over a value i in the range [0, N], and, for each i, check whether N+i or N-i is a beautiful integer. If that is the case, then we break the loop and return i as the answer.

Large dataset

Iterating over the range [0, N] would be too slow for this dataset. Therefore, we need another approach. To find the value of M, we must find the largest beautiful integer that is still no greater than N. Let us call this integer X. Similarly, to find the value of P, we can find the smallest beautiful integer that is still no smaller than N. Let us call this integer Y.

We can find X by decreasing the first odd digit in N by one and then replacing all digits to the right of that odd digit with the largest even digit (i.e. 8). For example, if N=4436271, then X=4428888. This can create a leading 0, which we can simply drop the leading 0 in that case. If there are no odd digits in N, then N is a beautiful integer, thus X=N.

By constructing X this way, it is guaranteed that there will be no beautiful integer between X and N, since the next beautiful integer after X would be larger than N at the first odd digit. For the example above, the next beautiful integer after X is 4440000, which is larger than N.

Similarly, we can find Y by increasing the first odd digit in N by one and replacing all digits to the right of that odd digit with the smallest even digit (i.e. 0). If there are no odd digits in N, then N is a beautiful integer, thus Y=N. However, when finding Y, we must watch out for some tricky cases. If the first odd digit is 9, then we must replace the digit directly to the left of that odd digit as well with the next even digit. For example, if N=86912, then Y=88000.

Another tricky case is when the digit directly to the left of the first odd digit is 8. In this case, we must replace the digit directly to the left of that 8 digit as well, and keep doing this until we have a non-8 digit. For example, if N=6488962, then Y=6600000. Finally, if all digits to the left continue to be 8 until we reach the leftmost digit, or if the first digit of N is a 9, then we must add the smallest non-zero even digit (i.e. 2) as a new digit on the left. For example, if N=88892 or N=91112, then Y=200000.

Once we know X and Y, we also know M and P, and we can take the smallest of those values, just as we did before.

代码实现:

#include<cstdio>
#include<sstream>
#include<iostream>
#include<algorithm>using namespace std;#define LL long longLL haveOdd(LL tmp){int left;LL  flag=0;LL  Odd=1;while(tmp!=0){left=tmp%10;tmp/=10;Odd=Odd*10;if(left%2!=0){flag=Odd;}}return flag;
}LL findMiner(LL n){/// 比n小的全部非奇数数:奇数左边的第一个数减一,后面的全是8:4436271 -> 4428888LL tmpFlag=haveOdd(n);if(tmpFlag!=0){tmpFlag/=10;n=n/tmpFlag;n=n*tmpFlag;n=n-tmpFlag;while(tmpFlag>0){tmpFlag/=10;n=n+8*tmpFlag;}}return n;
}LL findBigger(LL n){/** 比n大的全部非奇数数:最左边的第一个奇数加1,后面的全是0,4436271 -> 4440000若奇数左侧第一个数是8:一直加1直到不是8为止(题解中好像是直接替换8)若首字符是8、9的数字特殊处理,直接替换成最小的非0偶数作为新数字(这里没判断): N=88892 or N=91112 ->  200000*/LL tmpFlag=haveOdd(n);if(tmpFlag==0){return n;}if(tmpFlag!=0){tmpFlag=tmpFlag/10;n=n/tmpFlag;n=n*tmpFlag;n+=tmpFlag;n=findBigger(n);}return n;
}int calculate(LL n,int i){LL M,N,ans;M=findMiner(n);//找到比n小的最大 全偶数的数字N=findBigger(n);//找到比n大的最小 全偶数的数字//cout<<"N:"<<N<<endl;//cout<<"M:"<<M<<endl;ans=(n-M)>(N-n)?(N-n):(n-M);cout<<"Case #"<<i<<": "<<ans<<endl;return 0;
}
int main(){//freopen("A.in","r",stdin);//freopen("A.out","w",stdout);int N;LL n;cin>>N;for(int i=1; i<=N; i++){cin>>n;calculate(n,i);}return 0;
}

这篇关于Google kickStart-2018-RoundA-Problem A. Even Digits的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/532769

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException异常

用Gson解析json数据的时候,遇到一个异常,如下图: 这个异常很简单,就是你的封装json数据的javabean没有写对,你仔细查看一下javabean就可以了 比如:我的解析的代码是             Gson gson = new Gson();             ForgetJson rb = gson.fromJson(agResult.mstrJson, For

2018秋招C/C++面试题总结

博主从8月中旬开始大大小小面试了十几家公司,至今也许是告一段落吧,希望后面会有好结果,因此总结记录一些C/C++方向常见的问题。和大家一起学习! 参考了互联网的各种资源,自己尝试归类整理,谢谢~ 一、C和C++的区别是什么? C是面向过程的语言,C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛。 C中函数不能进行重载,C++函数可以重载 C++在C的基础上增添类,C是一个结构

Google Earth Engine——高程数据入门和山体阴影和坡度的使用

目录 山体阴影和坡度 对图像应用计算 应用空间减速器 高程数据 通过从“重置”按钮下拉菜单中选择“清除脚本”来清除脚本。搜索“elevation”并单击 SRTM Digital Elevation Data 30m 结果以显示数据集描述。单击导入,将变量移动到脚本顶部的导入部分。将默认变量名称“image”重命名为“srtm”。使用脚本将图像对象添加到地图: Map

The import com.google cannot be resolved

The import com.google cannot be resolved,报错: 第一感觉就是缺少jar包,因为项目用maven管理,所以在pom.xml中添加: <dependency>  <groupId>com.google.code.gson</groupId>  <artifactId>gson</artifactId>  <version>2.3.1</ver