参考链接:https://zhuanlan.zhihu.com/p/99381263
思路:用vector配合数位进行每一位拆解,然后sort一遍组合得到最小值 reverse组合得到最大值
#include
#include
#include
using namespace std;
int x,y;
vector vec;
int main(){
cin >> x >> y;
while(x != y){
int t = x;
while(t > 0){
vec.push_back(t % 10);
t /= 10;
}
sort(vec.begin(), vec.end());
int min_v = 0;
for(int v : vec){
min_v = min_v * 10 + v;
}
reverse(vec.begin(), vec.end());
int max_v = 0;
for(int v : vec){
max_v = max_v * 10 + v;
}
x = max_v - min_v;
vec.clear();
cout << x << endl;
}
return 0;
}
这题要注意两种进位方式
0-9对应a-z
但是首位0不显示 a要显示
字符转数字:s[i] - '0' 数字转字符:(char)(t % 10 + '0')
#include
#include
#include
#include
using namespace std;
string s;
int n;
vector vec;
int main()
{
cin >> s >> n;
int t = n; //进位
for(int i = s.size() - 1; i >= 0; i--){
if(isdigit(s[i])){
t += s[i] - '0';
vec.push_back((char)(t % 10 + '0'));
}else{
t += s[i] - 'a';
vec.push_back((char)(t % 26 + 'a'));
t /= 26;
}
}
if(t > 0){
if(isdigit(s[0])) vec.push_back((char)(t % 10 + '0'));
else vec.push_back((char)(t % 26 + 'a' - 1));
}
reverse(vec.begin(),vec.end());
for(char a : vec){
cout << a;
}
return 0;
}
这题数据量不大,直接三层循环暴力
#include
using namespace std;
int n;
int main()
{
cin >> n;
for(int x1 = 1; x1 <= 1000; x1++){
if((n+1) / n < 1 / x1) continue;
for(int x2 = 1; x2 <= 1000; x2++){
if((n+1) / n < 1 / x1 + 1 / x2) continue;
for(int x3 = 1; x3 <= 1000; x3++){
if((n+1) * x1 * x2 * x3 == n * (x1*x2 + x1*x3 + x2*x3) && x1 <= x2 && x2 <= x3){
cout << x1<<' '<< x2<<' '<< x3 << ' '<