Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.
Attached is a summary post: Analysis of past CSP-J preliminary test questions | Summary_csp past past questions_Blog of a communicator who loves programming-CSDN blog
#include <iostream>
using namespace std;
long long n, ans;
int k, len;
long long d[1000000];
int main() {
cin >> n >> k;
d[0] = 0;
len = 1;
ans = 0;
for (long long i = 0; i < n; ++i) {
++d[0];
for (int j = 0; j + 1 < len; ++j) {
if (d[j] == k) { //进位
d[j] = 0;
d[j + 1] += 1;
++ans; //只在进位时发生改变,记录进位次数
}
}
if (d[len - 1] == k) { //高精度加法,进位操作
d[len - 1] = 0;
d[len] = 1;
++len; //只在高位进位时发生改变,即记录数字长度
++ans;
}
}
cout << ans << endl;
return 0;
}
Assume that the input n is a positive integer not exceeding 2^62, and k is a positive integer not exceeding 10000. Complete the following true-false and multiple-choice questions:
22. If k=1, then when outputting ans, len=n. ( )
[Answer]: Wrong
【Analysis】
When k=1, len is always 2. so error
23. If k>1, when outputting ans, len must be less than n. ( )
[Answer]: Wrong
【Analysis】
When n=1, len is equal to 1, so it is wrong
24. If k>1, when outputting ans, k^len must be greater than n. ( )
[Answer]: Yes
【Analysis】
The 2-digit n (len=2) in base k must be less than k². For example, two digits in decimal system are all less than 100. The 2-digit number in binary system must be less than 4. so correct
25. If the input n is equal to 10^15 and the input k is 1, the output is equal to ( )
A.1
B.(10^30-10^15)/2
C.(10^30+10^15)/2
D.10^15
[Answer]: D
【Analysis】
When k=1, len=2, it will only carry in the low bit (i.e. d[1]), so the number of carries is the same as the number size, choose D
26. If the input n is equal to 205,891,132,094,649 (i.e. 3^30) and the input k is 3, the output is equal to (B)
A.3^30
B.(3^30-1)/2
C.3^30-1
D.(3^30+1)/2
【Answer】:
【Analysis】
d[0] carries a carry to d[1], one bit is carried every 3 times, and the number of carries (ans) is 3^29
d[1] carries a carry to d[2], one bit is carried every 9 times, and the number of carries (ans) is 3^28
d[2] carries a carry to d[3], one bit is carried every 3^3 times, and the number of carries (ans) is 3^27
...
d[29] carries to d[30], the number of carries (ans) is 3^0
The sum is 3^29+3^28+3^27+...+3^0. According to the geometric sequence summation formula, it is equal to (3^30-1)/2
27. If the input n is equal to 100,010,002,000,090 and the input k is 10, the output is equal to ( )
A.11,112,222,444,543
B.11,122,222,444,453
C.11,122,222,444,543
D.11,112,222,444,453
[Answer]: D
【Analysis】
d[0] carries a carry to d[1], and the number of carries is 10001000200009
d[1] carries a carry to d[2], and the number of carries is 1000100020000
d[2] carries a carry to d[3], and the number of carries is 100010002000
d[3] carries to d[4], the number of carries is 10001000200
d[4] carries a carry to d[5], and the number of carries is 1000100020
d[5] carries a carry to d[6], and the number of carries is 100010002
d[6] carries a carry to d[7], and the number of carries is 10001000
d[7] carries to d[8], the number of carries is 1000100
d[8] carries a carry to d[9], and the number of carries is 100010
d[9] carries to d[10], the number of carries is 10001
d[10] carries to d[11], the number of carries is 1000
d[11] carries to d[12], the number of carries is 100
d[12] carries to d[13], the number of carries is 10
d[13] carries to d[14], the number of carries is 1
The final calculation is 11112222444453