Constructing a Number
Manipulating numbers is at the core of a programmer's job. To test how well you know their properties, you are asked to solve the following problem.
You are given non-negative integers , , ..., . You want to know whether it's possible to construct a new integer using all the digits of these numbers such that it would be divisible by . You can reorder the digits as you want. The resulting number can contain leading zeros.
For example, consider the numbers from which you have to construct a new integer as described above. Numerous arrangements of digits are possible; but we have illustrated one below.
注意:数据类型不能用int储存,否则tle(wa)
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
* Complete the 'canConstruct' function below.
*
* The function is expected to return a STRING.
* The function accepts INTEGER_ARRAY a as parameter.
*/
string canConstruct(vector<int> a) {
// Return "Yes" or "No" denoting whether you can construct the required number.
long long sum=0;
for(int i=0;i<a.size();i++){
sum+=a[i];
}
if(sum%3==0) return "Yes";
else return "No";
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string t_temp;
getline(cin, t_temp);
int t = stoi(ltrim(rtrim(t_temp)));
for (int t_itr = 0; t_itr < t; t_itr++) {
string n_temp;
getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp)));
string a_temp_temp;
getline(cin, a_temp_temp);
vector<string> a_temp = split(rtrim(a_temp_temp));
vector<int> a(n);
for (int i = 0; i < n; i++) {
int a_item = stoi(a_temp[i]);
a[i] = a_item;
}
string result = canConstruct(a);
fout << result << "\n";
}
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
Closest Number
You are given 3 numbers a, b and x. You need to output the multiple of x which is closest to ab. If more than one answer exists , display the smallest one.
Input Format
The first line contains T, the number of testcases.
T lines follow, each line contains 3 space separated integers (a, b and x respectively)
Constraints
1 ≤ T ≤ 105
1 ≤ x ≤ 109
0 < ab ≤ 109
1 ≤ a ≤ 109
-109 ≤ b ≤ 109
Output Format
For each test case , output the multiple of x which is closest to ab
Sample Input 0
3 349 1 4 395 1 7 4 -2 2
Sample Output 0
348 392 0
Explanation 0
The closest multiple of 4 to 349 is 348.
The closest multiple of 7 to 395 is 392.
The closest multiple of 2 to 1/16 is 0.
注意:b=0的情况需要单独判断x的值,否则只能输出0.并且判断多个multiple的情况,接近a^b才是目标
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
* Complete the 'closestNumber' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER a
* 2. INTEGER b
* 3. INTEGER x
*/
//a=8 b=0 x=1
int closestNumber(int a, int b, int x) {
if(b==0) {
if(x==1) return x;
else return 0;
}
long long power = pow(a,b);
long long lower = (power/x)*x; //first multiple
long long next = lower+(power>0?x:-x); //second multiple
if(abs(lower-power)<=abs(next-power)){
return lower;
}
else {
return next;
}
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string t_temp;
getline(cin, t_temp);
int t = stoi(ltrim(rtrim(t_temp)));
for (int t_itr = 0; t_itr < t; t_itr++) {
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);
vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
int a = stoi(first_multiple_input[0]);
int b = stoi(first_multiple_input[1]);
int x = stoi(first_multiple_input[2]);
int result = closestNumber(a, b, x);
fout << result << "\n";
}
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}