1.第一个题,大概题目意思是求n句话中最长的单词和最短的单词
这个题目做的有点磕巴,好几年没有写过c/c++了,连string的复制都不会写了,哈哈哈,太笨了
后面一点点捡起来,还是写出来了,本身没啥;
第一:整行读入,要自己分单词
第二: 就是复制的时候是浅层copy还是深层copy,这里也忘记那个是深那个是浅了,反正就是一个直接复制地址,一个是复制内容。
粗暴的代码:
#include<vector>
#include<cmath>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
vector<string> str;
vector<int> str_leng;
int main(){
int num;
cin >> num;
string line;
int min_num=10000;
int max_num=0;
char max_str[1000]="",min_str[1000]="";
getline(cin,line);
while(num){
getline(cin,line);
if (1){
// cout <<num<< line << endl;
int len=0;
char tmp[1000]="";
for (int i=0;i<line.length();i++){
if (line[i]!=' '){
tmp[len]=line[i];
len++;
}
if(line[i]==' ' | i==line.length()-1){
tmp [len]='\0';
// cout<<tmp<<" len: "<<len<<endl;
if (len>max_num){
// cout<<tmp<<endl;
max_num=len;
strcpy(max_str,tmp);
}
if(len<min_num){
// cout<<"min: "<<tmp<<endl;
min_num = len;
strcpy(min_str,tmp);
}
len=0;
strcpy(tmp,"");
}
}
}
num--;
}
cout<<max_str<<endl;
cout<<min_str<<endl;
return 0;
}
2、就是对n个学生进行按成绩从高到低进行排序,成绩重复的按名字字典序从小到大排序
这里就是自己设置一个学生结构体,然后自定义个结构体排序就好了
还可以的代码:
沉默:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct student{
char name[20];
int score;
};
bool com(const student x,const student y)
{
if(x.score==y.score)
return x.name<y.name;
return x.score>y.score;
}
int main(){
int n;
cin >>n;
student std[30];
for(int i=0;i<n;i++){
cin >> std[i].name >> std[i].score;
}
sort(std,std+n,com);
for(int i=0;i<n;i++)
{
cout<<std[i].name<<" "<<std[i].score<<endl;
}
return 0;
}
3题
代码:
数学计算题:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
int n=1;
int res=0;
int tmp =0;
for (int i=0;i<1;i++){
tmp = (n+1)*2;
n = tmp;
}
cout<< n<<endl;
return 0;
}
用队列来表示能生存的数字,被队列抛弃的就是死掉的数字,最后只剩一个数的时候,就是最终答案。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
queue<int> q1;
int main(){
int n;
cin>>n;
int id=1;
int num=1;
q1.push(id);
bool flag=false;
while(!q1.empty()){
if(flag && q1.size()==1){
cout<<q1.front()<<endl;
return 0;
}
num++;
if (id<n && !flag){
id++;
}
else{
flag=true;
id=q1.front();
q1.pop();
}
if(num==3){
num=0;
}
else{
q1.push(id);
}
}
return 0;
}