最小位数和
#include <iostream>
#include <string>
using namespace std;
string fun(int number) {
string str = "";
if(number < 10) {
return (char)(number + '0') + str;
}
while (number) {
if(number > 9) {
str = '9' + str;
number -= 9;
} else {
str = (char)(number + '0') + str;
number = 0;
}
}
return str;
}
int main() {
int n;
cin >> n;
while(n--) {
int temp;
cin >> temp;
cout << fun(temp) << endl;
}
return 0;
}
翻倍
#include <iostream>
using namespace std;
int main() {
int T;
cin >> T;
while(T--) {
long long A, B, p, q;
cin >> A >> B >> p >> q;
if(A + p > B) {
cout << 1 << endl;
} else {
int num = 1;
while(A + p < B) {
p *= q;
num ++;
}
cout << num << endl;
}
}
return 0;
}
跳柱子
#include <iostream>
using namespace std;
int main() {
int T;
cin >> T;
while(T--) {
int n, k;
cin >> n >> k;
int sign = 1;
int success = 1;
int arr[1005];
for(int i = 0; i < n; i ++) {
cin >> arr[i];
}
for(int i = 0; i < n - 1;) {
int maxIndex = i, max = 0;
for(int j = 1; j <= k; j++) {
if(i + j >= n) {
break;
}
if(arr[i + j] <= arr[i] && arr[i + j] >= max) {
max = arr[i + j];
maxIndex = i + j;
}
}
if(max == 0) {
if(sign == 1) {
sign = 0;
for(int j = 1; j <= k; j++) {
if(i + j >= n) {
break;
}
if(arr[i + j] > max) {
max = arr[i + j];
maxIndex = i + j;
}
}
i = maxIndex;
} else {
success = 0;
break;
}
} else {
i = maxIndex;
}
}
if (success == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
积木
#include <iostream>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long n, m;
long long arr[100005];
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int sign = 1;
for (int i = 0; i < n; i++) {
if(i == 0) {
m += arr[i];
arr[i] = 0;
continue;
}
int temp = arr[i] - arr[i-1];
if(temp > 1) {
m += (temp - 1);
arr[i] -= (temp - 1);
} else if(temp < 1) {
if(m >= -temp + 1) {
m -= (-temp + 1);
arr[i] += (-temp + 1);
} else {
sign = 0;
break;
}
}
}
if(sign == 1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}