注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
2. 字符串处理
You are given a non-negative integer number represented as a string of digits, and a positive integer k. Your task is to perform the following algorithm:
If the length of the number is less than or equal to k, end the algorithm. Otherwise proceed to the next steps;
Split the number into groups of k digits in each. The last group may contain less than k digits;
Add up the digits in each group, and concatenate these results in the same order;
Replace the number with the result of step 3 and go back to step 1.
Return the resulting string number after the algorithm ends.
It is guaranteed that the algorithm will end at step 1 at some point.
Example
For number = "1111122222" and k = 3, the output should be solution(number, k) = "132".
Expand to see the example video.
The number has length 10 > k, so we proceed to the next steps;
We split number into groups of size k = 3: [111, 112, 222, 2]. Note that the last group is not full and has only one digit;
We add up the digits in each group, and concatenate the results. Thus, the new value of number becomes "3462";
We start again with step 1: number has length 4 > k, so we proceed to the next steps;
We split number into groups of size k = 3: [346, 2];
We add up the digits in each group, and concatenate the results, the new number is "132";
The length of the new number is 3 = k, so the algorithm ends with "132" as a result.
For number = "1111122222" and k = 5, the output should be solution(number, k) = "510".
Expand to see the example video.
We successfully proceed step 1 and split the number into groups of k = 5 digits on step 2: [11111, 22222];
We add up the digits in each group, and concatenate the results, and the number becomes "510";
The new number has length 3 < k, so we finish the algorithm and return the string "510".
Input/Output
[execution time limit] 0.5 seconds (cpp)
[input] string number
A string of digits representing the initial number. It is guaranteed that the given string contains only digits and doesn't contain leading zeros (unless the number is 0).
Guaranteed constraints:
1 ≤ number.length ≤ 1000.
[input] integer k
The size of the groups number should be split into.
Guaranteed constraints:
3 ≤ k ≤ 1000.
[output] string
The string on which the algorithm ends.
Test 5
Input:
number: "111000222"
ɨut and console logs.
Test 6
Input:
chunks:
[[1000000000000,1000000000000],
[1000000000000,1000000000000],
[10000000000,100000000000]]
Expected Output:
[1, 1, 90000000002]
Click the "Run Tests" button to see output and console logs.
Test 7
Input:
chunks:
[[1,10],
[10,100],
[100,1000],
[1000,10000]]
Expected Output:
[10, 100, 1000, 10000]
Click the "Run Tests" button to see output and console logs.
Test 8
Input:
chunks:
[[1,2],
[1,16],
[1,9],
[1,44],
[1,38]]
Expected Output:
[2, 16, 16, 44, 44]
Click the "Run Tests" button to see output and console logs.
Test 9
Input:
chunks:
[[20,24],
[20,31],
[20,49],
[20,91],
[20,89]]
Expected Output:
[5, 12, 30, 72, 72]
Click the "Run Tests" button to see output and console logs.
Test 10
Input:
chunks:
[[242319992485,888494425039],
[890253050257,893986666716],
[888022315788,983197327824],
[375477269778,544174442651],
[372646369775,471869607388],
[104710990462,767613232623],
[885899371431,984870237450],
[590383734375,800503393032],
[605928833001,947931236759],
[948695791408,988903707680],
[773919986477,782808658175],
[251948207514,573096090660],
[90130840141,347207472243],
[111095701427,417671009162],
[697732084372,898883057010]]
Expected Output:
[646174432555, 649908049015, 740877335340, 740877335340, 740877335340, 878486337363, 880159246989, 880159246989, 880159246989, 884192717219, 884192717219, 884192717219, 898772867540, 898772867540, 898772867540]
Click the "Run Tests" button to see output and console logs. |