题目0006基于身高差排序
题目描述:
小明今年升学到了小学一年级,
来到新班级后,发现其他小朋友身高参差不齐,
然后就想基于各小朋友和自己的身高差,对他们进行排序,
请帮他实现排序
输入描述
第一行为正整数 H
和N
0 < H < 200
为小明的身高
0 < H < 50
为新班级其他小朋友个数
第二行为N
个正整数
H1 ~ Hn
分别是其他小朋友的身高
取值范围0 < Hi < 200
且N
个正整数各不相同
输出描述
输出排序结果,各正整数以空格分割
和小明身高差绝对值最小的小朋友排在前面
和小明身高差绝对值最大的小朋友排在后面
如果两个小朋友和小明身高差一样
则个子较小的小朋友排在前面
示例一
-
输入
100 10 95 96 97 98 99 101 102 103 104 105
-
输出
99 101 98 102 97 103 96 104 95 105
C++代码
//
// Created by HANWENKE on 2022/8/28.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> quicsort(vector<int>&arr,int target){
//计算每一个学生与小明身高差的绝对值
int diff[arr.size()];
for(int i=0;i<arr.size();i++){
diff[i]=abs(arr[i]-target);
}
vector<int>temp(arr.size());
int local;//记录位置
int x=0;
for(int j=0;j<arr.size();j++){
int ss=5000;//记录最小值
for(int i=0;i<arr.size();i++){
if(ss>diff[i]){
ss=diff[i];
local=i;
}else if(ss==diff[i]){
//如果值相等的时候--返回身高小的那个数组下标
if(arr[i]<arr[local]){
local=i;
}
}
}
temp[x++]=arr[local];
diff[local]=INT32_MAX;
}
return temp;
}
int main(){
int H,N;
//身高--学生个数
cin>>H>>N;
vector<int>arr(N);
int temp;
for(int i=0;i<N;i++){
cin>>temp;
arr[i]=temp;
}
vector<int>arr1= quicsort(arr,H);
for(int i=0;i<arr1.size();i++){
cout<<arr1[i]<<" ";
}
return 0;
}
题目0007指定区域单词翻转
题目描述:
输入一个英文文章片段,
翻转指定区域的单词顺序,
标点符号和普通字母一样处理,
例如输入字符串
I am a developer.
[0,3]
则输出
developer. a am I
输入描述
使用换行隔开3个参数
第一个参数为文章内容 即英文字符串
第二个参数为翻转起始单词下标,下标从0开始
第三个参数为结束单词下标
输出描述
翻转后英文文章片段每个单词之间以一个半角空格分割输出
示例一
-
输入
I am a developer. 0 3
-
输出
developer. a am I
示例二
-
输入
hello world! 0 3
-
输出
world! hello
C++代码
//
// Created by HANWENKE on 2022/8/28.
//
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
void Reverse(vector<string>&nums,int l,int r){
//双指针进行翻转
while(l<r){
string temp=nums[l];
nums[l]=nums[r];
nums[r]=temp;
l++;
r--;
}
}
int main(){
/*
* 首先要获取到有几个单词--单词用空格分开--所以可以记录空格的个数
* 其次在空格的个数上加1就是单词的个数
* 如果是从0-空格个数就是翻转整个
*/
vector<string>arr1;
string ss;
getline(cin,ss);
//将字符串用空格分开
istringstream s1(ss);
while(s1>>ss){
arr1.push_back(ss);
}
/* for(int i=0;i<arr1.size();i++){
cout<<arr1[i]<<" ";
}*/
int start,end;
cin>>start>>end;
if(end>=arr1.size()){
end=arr1.size()-1;
}
Reverse(arr1,start,end);
for(int i=0;i<arr1.size();i++)
cout<<arr1[i]<<" ";
return 0;
}
题目0008 最大花费
题目描述:
双11众多商品进行打折销售,小明想购买一些自己心仪的商品,
但由于受购买资金限制,所以他决定从众多心意商品中购买3件,
而且想尽可能的花完资金,
现在请你设计一个程序帮助小明计算尽可能花费的最大资金额。
输入描述
第一行为整型数组M
,数组长度小于100,数组元素记录单个商品的价格;
单个商品价格小于1000;
第二行输入为购买资金的额度R
;
R < 100000
。
输出描述
输出为满足上述条件的最大花费额度
如果不存在满足上述条件的商品请返回-1
示例一
-
输入
23,26,36,27 78
-
输出
76
示例二
-
输入
23,30,40 26
-
输出
-1
C++代码
//
// Created by HANWENKE on 2022/8/28.
//
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
void backtracking(vector<int>&nums,vector<int>&path,int &target,int startindex,int sum,vector<int>&res){
if(path.size()>3){
return;
}
if(path.size()==3&&sum<=target){
//不为1的时候只添加最大的
if(res.size()==1&&sum>res.back()){
res[0]=sum;
return;
}else if(res.size()==0){
res.push_back(sum);
return;
}
return;
}
for(int i=startindex;i<nums.size();i++){
path.push_back(nums[i]);
sum+=nums[i];
backtracking(nums,path,target,i+1,sum,res);
path.pop_back();
sum-=nums[i];
}
}
void solution(vector<int>&nums,int &target){
sort(nums.begin(),nums.end());
if(nums.size()<3||
(nums[0]+nums[1]+nums[2]>target)){
cout<<-1;
}
vector<int>path;
vector<int>res;
backtracking(nums,path,target,0,0,res);
if(res.size()!=0){
cout<<res[0];
}else{
cout<<-1;
}
}
int main(){
vector<int>arr1;
arr1.reserve(100);
int num;
while(cin>>num){
arr1.push_back(num);
if(getchar()=='\n')break;
}
//小明的资金
cin>>num;
solution(arr1,num);
return 0;
}
题目0009 从字符串2中找字符串1的所有字符
题目描述:
给定两个字符串,
从字符串2中找出字符串1中的所有字符,
去重并按照ASCII码值从小到大排列,
输入描述
字符范围满足ASCII编码要求,
输入字符串1长度不超过1024,
字符串2长度不超过100
输出描述
按照ASCII由小到大排序
示例一
-
输入
bach bbaaccddfg
-
输出
abc
示例二
-
输入
fach bbaaccedfg
-
输出
acf
C++代码
//
// Created by HANWENKE on 2022/8/28.
//
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
int main(){
string s1,s2;
cin>>s1;
cin>>s2;
//在这里可以使用set集合--将s2放到set当中--遍历s1如果字符存在那么就加入到字符串中
set<char>use;
for(int i=0;i<s2.length();i++){
if(use.end() ==use.find(s2[i])){
use.insert(s2[i]);
}
}
//ss有可能是重复的--所以要对其去重
string ss;
//再用一个set对其去重
set<char>ss1;
for(int i=0;i<s1.length();i++){
if(use.find(s1[i])!=use.end()&&ss1.find(s1[i])==ss1.end()){
ss+=s1[i];
ss1.insert(s1[i]);
}
}
//字符串排序
vector<char>res(ss.length());
for(int i=0;i<ss.length();i++){
res[i]=ss[i];
}
sort(res.begin(),res.end());
for(int i=0;i<res.size();i++){
cout<<res[i];
}
return 0;
}
题目0010 数据分类
题目描述:
对一个数据a进行分类,
分类方法是,此数据a(4个字节大小)的4个字节相加对一个给定值b取模,
如果得到的结果小于一个给定的值c则数据a为有效类型,其类型为取模的值。
如果得到的结果大于或者等于c则数据a为无效类型。
比如一个数据a=0x01010101
,b=3
按照分类方法计算:(0x01+0x01+0x01+0x01)%3=1
所以如果c等于2,则此a就是有效类型,其类型为1
如果c等于1,则此a是无效类型
又比如一个数据a=0x01010103
,b=3
按分类方法计算:(0x01+0x01+0x01+0x03)%3=0
所以如果c=2则此a就是有效类型 其类型为0
如果c等于0 则此a是无效类型
输入12个数据,
第一个数据为c,第二个数据为b,
剩余10个数据为需要分类的数据
请找到有效类型中包含数据最多的类型,
并输出该类型含有多少个数据
输入描述
输入12个数据用空格分割,
第一个数据为c,第二个数据为b,
剩余10个数据为需要分类的数据。
输出描述
请找到有效类型中包含数据最多的类型,
并输出该类型含有多少个数据。
示例一
-
输入
3 4 256 257 258 259 260 261 262 263 264 265
-
输出
3
-
说明
这10个数据4个字节相加后的结果分别是 1 2 3 4 5 6 7 8 9 10 故对4取模的结果为 1 2 3 0 1 2 3 0 1 2 c是3所以012都是有效类型 类型为1和2的有3个数据 类型为0和3的只有两个
示例二
-
输入
1 4 256 257 258 259 260 261 262 263 264 265
-
输出
2
C++代码
//
// Created by HANWENKE on 2022/8/28.
//
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#define byte unsigned char
using namespace std;
bool cmp(const pair<int,int>&a,const pair<int,int>&b){
return a.second>b.second;
}
class solution{
public:
void Input(vector<int>&res){
int x;
while(cin>>x){
res.push_back(x);
if(getchar()=='\n'){ break;}
}
}
//将int转化为byte并且求和输出为int
int intToBytSum(int x){
byte bytes[4]={0,0,0,0};
bytes[0]=(byte)((0xff000000 &x)>>24);
bytes[1]=(byte)((0xff0000 &x)>>16);
bytes[2]=(byte)((0xff00 &x)>>8);
bytes[3]=(byte)((0xff &x));
int sum=bytes[0]+bytes[1]+bytes[2]+bytes[3];
return sum;
}
//计算4字节相加的结果取模并且将其放到vector中
vector<int> mod(vector<int>&nums,int c,int mod){
vector<int>temp;
for(int i=2;i<nums.size();i++){
int x=intToBytSum(nums[i])%mod;
if(x<c){
temp.push_back(x);
}
}
return temp;
}
//在temp--有效数据当中--找数据最多的--并返回其个数
void Max(vector<int>&temp){
map<int,int>ismap;
for(auto &x:temp){
ismap[x]++;
}
vector<pair<int,int>>arr;
for(auto &x:ismap){
arr.push_back(x);
}
sort(arr.begin(),arr.end(),cmp);
if(arr.size()!=0){
cout<< arr[0].second;
return;
}
cout<<0;
return ;
}
};
int main(){
vector<int>res;
solution l;
l.Input(res);
vector<int>path=l.mod(res,res[0],res[1]);
l.Max(path);
return 0;
}