codejam题目
This problem was asked in Google CodeJam 2019 online qualifier round.
在Google CodeJam 2019在线预选赛中询问了此问题。
Problem Statement:
问题陈述:
Someone just won the Code Jam lottery, and we owe them N jamcoins! However, when we tried to print out an oversized check, we encountered a problem. The value of N, which is an integer, includes at least one digit that is a 4... and the 4 key on the keyboard of our oversized check printer is broken.
有人刚刚赢得了Code Jam彩票,我们欠他们N个 jamcoins! 但是,当我们尝试打印超大支票时,我们遇到了问题。 N的值是一个整数,包括至少一个为4 ...的数字,并且超大尺寸支票打印机键盘上的4键已损坏。
Fortunately, we have a workaround: we will send our winner two checks for positive integer amounts A and B, such that neither A nor B contains any digit that is a 4, and A + B = N. Please help us find any pair of values A and B that satisfy these conditions.
幸运的是,我们有一个解决方法:我们将派我们的冠军两次检查为正整数相当于A和B,使得A和B都不包含任何数字是4,和A + B = N。 请帮助我们找到满足这些条件的任意一对值A和B。
Input
输入项
The first line of the input gives the number of test cases, T. T test cases follow; each consists of one line with an integer N.
输入的第一行给出测试用例的数量T。 随后是T测试用例; 每一行包含一行,整数为N。
Output
输出量
For each test case, output one line containing Case #x: A B, where x is the test case number (starting from 1), and A and B are positive integers as described above.
对于每个测试用例,输出一行包含Case #x:AB ,其中x是测试用例编号(从1开始),并且A和B是如上所述的正整数。
It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any one of them.
保证至少存在一种解决方案。 如果有多个解决方案,则可以输出其中任何一个。
Sample
样品
Input | Output |
3 4 940 4444 | Case #1: 2 2 Case #2: 852 88 Case #3: 667 3777 |
输入项 | 输出量 |
3 4 940 4444 | 案例1:2 2 案例2:852 88 案例3:667 3777 |
Reference: Foregone Solution
参考:已 放弃的解决方案
Explanation:
说明:
In Sample Case #1, notice that A and B can be the same. The only other possible answers are 1 3 and 3 1.
在示例案例1中 ,请注意, A和B可以相同。 唯一其他可能的答案是1 3和3 1 。
Note: Before going to solution, please try it by yourself.
注意:在寻求解决方案之前,请自己尝试一下。
Short description of solution approach
解决方法的简短描述
For any number N, let say, 45234, we can write it as 45234 + 00000 that is N = A+B, where A = 45234 and B = 00000. But, according to given condition there should not be any '4' in A or B. So, whenever we encounter '4' in A we can write it as 2 and at the same position at B we can put 2.
对于任何数字N (假设45234) ,我们可以将其写为45234 + 00000 ,即N = A + B ,其中A = 45234和B = 00000 。 但是,根据给定条件, A或B中不应有任何“ 4” 。 因此,只要我们在A中遇到“ 4” ,就可以将其写为2,而在B的相同位置,则可以放2 。
Example:
例:
Input: 45234
Step 1:
45234 ← A
+00000 ← B
--------------------------------
45234 ← N
---------------------------------
Step 2:
25232 ← A
+20002 ← B
--------------------------
45234 ← N
---------------------------
So, after step 2, we can see that there is no 4 in A or B.
因此,在步骤2之后,我们可以看到A或B中没有4。
Note: You can replace it as 3 and 1 also. There may be multiple solutions but you must have to fulfill the condition.
注意:您也可以将其替换为3和1 。 可能有多种解决方案,但是您必须满足条件。
Algorithm
算法
Step1: Take input N (as a string)
Step2: Take an array B of size N and initialize all values to 0
Step3: for(i=0;i<N.length();i++)
Step3.1: if(N[i]=='4')
B[i]='2';
N[i]=2;
Step4: print N (that is A) and B
Explanation
说明
Please read "Short description of sloution approach" section given above.
请阅读上面给出的“拖拉方法的简短描述”部分。
C++ implementation:
C ++实现:
#include <bits/stdc++.h>
#define ll long long int;
using namespace std;
int main()
{
int T,k=1;
cin>>T;
while(T--)
{
string N;
cin>>N;
int i,len=0;
len=N.length(); //Length of N
//Taking vector B of size len (Size of N)
//and initialize all values to 0
vector<int>B(len,0);
for(i=0;i<len;i++)
{
//Checking if N[i] is 4 or not
if(N[i]=='4')
{
N[i]='2'; //If 4 replace it by 2
B[i]=2; //Also replace B[i] by 2
}
}
int ind=-1;
/*If there is any leading 0 in B then we should not print that.
So, moving the index to very first non zero value of B*/
for(i=0;i<len;i++)
{
//checking if there is any more leading 0 or not
if(B[i]!=0)
{
ind=i;
break;
}
}
//printing the value of A
cout<<"Case #"<<k<<": "<<N<<" ";
//printing the value of B without leading 0
for(i=ind;i<len;i++)
cout<<B[i];
cout<<"\n";
//k is for printing the Case Number
k++;
}
return 0;
}
Output
输出量
翻译自: https://www.includehelp.com/icp/google-codejam-2019-foregone-solution.aspx
codejam题目