#include <iostream>
#include<vector>
using namespace std;
int main(){
int t,a,b;
cin >> t;
int x;
for (int i = 0; i < t; i++){
cin >> x;
if (x < 10)
cout << x << endl;
else{
a = x % 9;
b = x / 9;
vector<int> result;
int j = 0;
for (j = 0; j < b; j++) {
result.push_back(9);
}
if (a != 0) {
result.push_back(a);
}
for (int k=result.size()-1; k >= 0; k--)
cout << result[k];
cout << endl;
}
}
}
自己的解法(通过率只有20%)
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int t;
cin >> t;
long a[3];
long long sum = 0;
vector<long long> res;
for (int i = 0; i < t; i++){
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
sum=a[0]+a[1]+a[2];
if(sum%3==0){
sum=sum/3;
if(a[0]+a[1]<sum)
sum=sum+1;
}
else{
int tmp=a[0]+(2*(a[1]-a[0]))/3+(a[2]-a[1])/2+1;
if(tmp-(sum-tmp)/2>1)
sum=tmp-1;
else
sum=tmp;
}
res.push_back(sum);
}
for (int i = 0; i < t;i++)
cout << res[i] << endl;
return 0;
}
(牛客网别人的解法)
#include <iostream>
#include <cmath>
using namespace std;
void sort(long list[3]) // 手动冒泡排序
{
if (list[0]<list[1]) swap(list[0],list[1]);
if (list[0]<list[2]) swap(list[0],list[2]);
if (list[1]<list[2]) swap(list[1],list[2]);
}
int main()
{
int n;
long l[3], sum;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> l[0] >> l[1] >> l[2];
sort(l);
sum = l[0] + l[1] + l[2];
cout << max((sum + 2) / 3, (l[0] + 1) / 2) << endl;//加2与加1是为上取整
}
}
牛客网别人的解法
链接:https://www.nowcoder.com/questionTerminal/546b22232fd14997a10a8d2a7a158cbc?f=discussion
来源:牛客网
#include<iostream>
#include<vector>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
if (n<=0) {
cout << 0 << endl;
continue;
}
vector<long long> a;
long long tmp;
while(n--) {
cin >> tmp;
a.push_back(tmp);
}
// 闭区间
int maxlen = 1;
long long nowsum = a[0];
int left=0, right=0;
while(right+1 < a.size()) {
if (a[right+1] >= a[right] && a[right+1] >= nowsum) {
nowsum += a[right+1];
right ++;
maxlen = max(maxlen, right-left+1);
continue;
}
nowsum -= a[left];
left ++;
if (left > right) {
right = left;
nowsum = a[left];
}
}
cout << maxlen << endl;
}
}
自己的方法:只能通过30%,但是数组溢出,嵌套太深。
#include <iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
int t,n,tmp;
cin >> t;
vector<string> res;
for (int i = 0; i < t; i++){
cin >> n;
long long sum = 0;
vector<long> a;
for (int j = 0; j < n; j++){
cin >> tmp;
a.push_back(tmp);
sum = sum + tmp;
}
if (sum % 2 != 0){
res.push_back("NO");
}
else{
sum = sum / 2;
int j = 0,k=n-1;
long long head = 0;
while (j!=k)
{
head = head + a[j];
if (head == sum && j!=k){
res.push_back("YES");
break;
}
else if (head < sum && j!=k && k==n-1){
j++;
}
else if (head < sum && j != k && k != n - 1){
j--;
}
else if (j == k && head != sum){
res.push_back("NO");
break;
}
else if(head>sum && k!=n-1){
head = head - a[k]-a[j];
k--;
}
else{
head = head - a[j];
j--;
int temp = j;
j = k;
k = temp;
}
}
}
}
for (int i = 0; i < res.size(); i++)
cout << res[i] << endl;
return 0;
}