当前位置: 首页 > article >正文

上海创智学院(测试)算法笔试(ACM赛制)部分例题

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;
}

http://www.kler.cn/a/561907.html

相关文章:

  • k8s集群3主5从高可用架构(kubeadm方式安装k8s)
  • 广州4399游戏25届春招游戏策划管培生内推
  • Linux搜索查找类指令
  • 【透视图像目标检测(3)】透视3D目标的航向角、观察角(局部方向)和相对位置角辨析,观察角的单目投影验证
  • 爬虫基础知识
  • 解析Excel表表头
  • 鸿蒙NEXT开发-用户通知服务
  • 矩阵碰一碰发视频的后端源码技术,支持OEM
  • StepAudio:语音大模型
  • Java所有运算符理解
  • ldap放大 DDOS.c
  • LeetCodehot 力扣热题100 全排列
  • ue学习part2
  • LeetCode每日精进:622.设计循环队列
  • C#快速调用DeepSeek接口,winform接入DeepSeek查询资料 C#零门槛接入DeepSeek C#接入DeepSeek源代码下载
  • K8S能部署大数据集群吗?为什么?K8S的HPA功能可以作为大数据处理消息积压的问题(Kafka的分区)
  • R 语言科研绘图 --- 柱状图-汇总
  • react18自定义hook实现
  • NLP工程师逐步切入机器人和具身智能方向
  • Go语言--语法基础2--下载安装