8.22字节 大佬请检查 两道题,在VS上可以实现,但是提交的时候都不是100% ,题目四成功率0%,但是在自己编辑器上很多例子都可通过, 不知道哪出问题了
题目二:求【x,y】内部的完美数字的个数,比如1,11,222,333
#include<iostream>
#include<stdio.h>
#include<string>
#include <vector> // std::vector
using namespace std;
int getnum(int x) {
string str = to_string(x);
int size = str.size();
char minest = str[0];
for (int i = 1; i < size; i++) {
minest = minest < str[i] ? minest : str[i];
}
int min_num = minest - '0';
int res = (size - 1) * 9 + min_num;
return res;
}
void fun(vector<pair<int, int>>& v, vector<int>& res) {
for (auto qj : v) {
int x = getnum(qj.first);
int y = getnum(qj.second);
int get = y - x + 1;
res.push_back(get);
}
}
int main() {
int a;
cin >> a;
vector<pair<int, int>> v;
vector<int> res;
for (int i = 0; i < a; i++) {
int begin;
int end;
cin >> begin;
cin >> end;
v.push_back({ begin, end });
}
fun(v, res);
for (auto b : res) {
cout << b << endl;
}
system("pause");
return 0;
}
题目四:发糖题目,一串数组,要是比他右边的少,就+1,看第x个糖发到谁手里;要是没有,就返回数组长度+1
#include<stdio.h>
#include<string>
#include <vector> // std::vector
using namespace std;
int getnum(int x) {
string str = to_string(x);
int size = str.size();
char minest = str[0];
for (int i = 1; i < size; i++) {
minest = minest < str[i] ? minest : str[i];
}
int min_num = minest - '0';
int res = (size - 1) * 9 + min_num;
return res;
}
void fun(vector<pair<int, int>>& v, vector<int>& res) {
for (auto qj : v) {
int x = getnum(qj.first);
int y = getnum(qj.second);
int get = y - x + 1;
res.push_back(get);
}
}
int main() {
int a;
cin >> a;
vector<pair<int, int>> v;
vector<int> res;
for (int i = 0; i < a; i++) {
int begin;
int end;
cin >> begin;
cin >> end;
v.push_back({ begin, end });
}
fun(v, res);
for (auto b : res) {
cout << b << endl;
}
system("pause");
return 0;
}
题目四:发糖题目,一串数组,要是比他右边的少,就+1,看第x个糖发到谁手里;要是没有,就返回数组长度+1
eg:
4 3
1 2 3 4
长度为4,发3个糖,则:
第一个糖:2 2 3 4 发到1号
第二个糖:2 3 3 4 发到2号
第三个糖:3 3 3 4 发到1号
返回1
4 3
4 3 2 3
返回 5
#include <iostream>
#include <vector>
using namespace std;
void fun(vector<int>& v, int value, vector<int>& res) {
int n = v.size();
int val = v[n - 1];
int count = 0;
for (int i = 0; i < n - 1; i++) {
if (v[i] < val) {
count = count + (val - v[i]);
}
}
if (value > count) {
res.push_back(n + 1);
}
else {
int ind;
for (int i = 0; i < value; i++) {
for (int j = 0; j < n - 1; j++) {
if (v[j] < v[j + 1]) {
v[j]++;
ind = j + 1;
break;
}
}
}
res.push_back(ind);
}
}
int main() {
int a;
cin >> a;
vector<int> res;
for (int i = 0; i < a; i++) {
int nums;
int val;
vector<int> v;
cin >> nums;
cin >> val;
for (int j = 0; j < nums; j++) {
int a;
cin >> a;
v.push_back(a);
}
fun(v, val, res);
}
for (auto b : res) {
cout << b << endl;
}
return 0;
}
#include <vector>
using namespace std;
void fun(vector<int>& v, int value, vector<int>& res) {
int n = v.size();
int val = v[n - 1];
int count = 0;
for (int i = 0; i < n - 1; i++) {
if (v[i] < val) {
count = count + (val - v[i]);
}
}
if (value > count) {
res.push_back(n + 1);
}
else {
int ind;
for (int i = 0; i < value; i++) {
for (int j = 0; j < n - 1; j++) {
if (v[j] < v[j + 1]) {
v[j]++;
ind = j + 1;
break;
}
}
}
res.push_back(ind);
}
}
int main() {
int a;
cin >> a;
vector<int> res;
for (int i = 0; i < a; i++) {
int nums;
int val;
vector<int> v;
cin >> nums;
cin >> val;
for (int j = 0; j < nums; j++) {
int a;
cin >> a;
v.push_back(a);
}
fun(v, val, res);
}
for (auto b : res) {
cout << b << endl;
}
return 0;
}
全部评论
(3) 回帖