基础算法之模拟

news2024/10/2 10:43:42

1P1093 [NOIP2007 普及组] 奖学金 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P1093icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1093

#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
    int num;//编号
    int c,m,e; 
    int sum;
}student[310];
bool cmp(stu a,stu b)
{
    if(a.sum>b.sum) return 1;
    else if(a.sum<b.sum) return 0;
    else
    {
        if(a.c>b.c) return 1;
        else if(a.c<b.c) return 0;
        else
        {
            if(a.num>b.num) return 0;
            else return 1;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        student[i].num=i;//录入编号
        cin>>student[i].c>>student[i].m>>student[i].e;//输入
        student[i].sum=student[i].c+student[i].m+student[i].e;//计算总分
    }
    sort(student+1,student+1+n,cmp);
    for(int i=1;i<=5;i++)
        cout<<student[i].num<<' '<<student[i].sum<<endl;
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// 定义学生结构体,包含学号,总分,语文成绩
struct Student {
    int id;
    int totalScore;
    int chineseScore;
};

// 自定义排序函数
bool compareStudents(const Student& a, const Student& b) {
    if (a.totalScore == b.totalScore) {
        if (a.chineseScore == b.chineseScore) {
            return a.id < b.id;
        }
        return a.chineseScore > b.chineseScore;
    }
    return a.totalScore > b.totalScore;
}

int main() {
    int n;
    cin >> n;

    vector<Student> students(n);

    for (int i = 0; i < n; ++i) {
        int chinese, math, english;
        cin >> chinese >> math >> english;

        students[i].id = i + 1;  // 学号
        students[i].totalScore = chinese + math + english;  // 总分
        students[i].chineseScore = chinese;  // 语文成绩
    }

    // 对学生按规则排序
    sort(students.begin(), students.end(), compareStudents);

    // 输出前5名学生的学号和总分
    for (int i = 0; i < 5; ++i) {
        cout << students[i].id << " " << students[i].totalScore << endl;
    }

    return 0;
}

P1067 [NOIP2009 普及组] 多项式输出 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1067

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> coefficients(n + 1);

    // 输入多项式的系数
    for (int i = 0; i <= n; ++i) {
        cin >> coefficients[i];
    }

    string result;
    bool firstTerm = true;  // 标记是否为第一个非零项

    // 遍历所有的系数,从最高次到常数项
    for (int i = 0; i <= n; ++i) {
        int degree = n - i;  // 当前项的次数
        int coeff = coefficients[i];  // 当前项的系数

        if (coeff == 0) {
            continue; // 跳过系数为0的项
        }

        // 确定当前项的符号和绝对值
        string sign = (coeff > 0) ? "+" : "-";
        int absCoeff = abs(coeff);

        // 构建当前项的字符串表示
        string term;

        if (degree > 1) { // 处理指数大于1的项
            if (absCoeff != 1) {
                term = to_string(absCoeff) + "x^" + to_string(degree);
            }
            else {
                term = "x^" + to_string(degree);
            }
        }
        else if (degree == 1) { // 处理一次项
            if (absCoeff != 1) {
                term = to_string(absCoeff) + "x";
            }
            else {
                term = "x";
            }
        }
        else { // 处理常数项
            term = to_string(absCoeff);
        }

        // 将当前项添加到结果字符串中,并根据是否为第一项调整格式
        if (firstTerm) {
            if (sign == "-") {
                result += sign + term;
            }
            else {
                result += term;
            }
            firstTerm = false;
        }
        else {
            result += sign + term;
        }
    }

    // 如果结果为空,说明所有项系数为0,输出0
    if (result.empty()) {
        result = "0";
    }

    cout << result << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,i;
    cin>>n;
    for(i=n;i>=0;i--)
	{
        cin>>a;
        if(a)
		{    
            if(i!=n&&a>0) cout<<"+";   
            if(abs(a)>1||i==0) cout<<a;//abs是绝对值函数   
            if(a==-1&&i) cout<<"-";    
            if(i>1) cout<<"x^"<<i;    
            if(i==1) cout<<"x";    
        }
    }
    return 0;
}

P1068 [NOIP2009 普及组] 分数线划定 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1068

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 选手结构体,包含报名号和笔试成绩
struct Candidate {
    int id;
    int score;
};

// 自定义排序函数
bool compareCandidates(const Candidate& a, const Candidate& b) {
    if (a.score == b.score) {
        return a.id < b.id;
    }
    return a.score > b.score;
}

int main() {
    int n, m;
    cin >> n >> m;

    vector<Candidate> candidates(n);

    // 输入选手的报名号和笔试成绩
    for (int i = 0; i < n; i++) {
        cin >> candidates[i].id >> candidates[i].score;
    }

    // 排序选手,按照成绩从高到低排序,成绩相同按报名号从小到大排序
    sort(candidates.begin(), candidates.end(), compareCandidates);

    // 计算面试分数线位置
    int cutoffRank = m * 1.5;  // 自动向下取整
    int cutoffScore = candidates[cutoffRank - 1].score;  // 第 cutoffRank 个选手的成绩

    // 确定实际进入面试的选手
    vector<Candidate> interviewCandidates;
    for (const Candidate& c : candidates) {
        if (c.score >= cutoffScore) {
            interviewCandidates.push_back(c);
        }
        else {
            break;  // 因为已经排序,当成绩小于分数线时直接停止
        }
    }

    // 输出结果
    cout << cutoffScore << " " << interviewCandidates.size() << endl;
    for (const Candidate& c : interviewCandidates) {
        cout << c.id << " " << c.score << endl;
    }

    return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
struct mark{
       int a,h;
};
mark mian[5000];
bool cmp(mark x,mark y)
{
    if(x.a>y.a) return 1;
	if(x.a==y.a&&x.h<y.h) return 1;
    return 0;  
}
int main()
{
    int n,m,pass,s=0;
    cin>>n>>m; 
    //输入考试人数与预计通过人数
    
    for(int i=0;i<n;i++)
    {
        cin>>mian[i].h>>mian[i].a;
        //循环,输入考试者的成绩与号数
    }
    
    sort(mian,mian+n,cmp);     
    //根据分数与号数排序
	pass=mian[m*3/2-1].a;      //计算分数线
    for(int i=0;i<n;i++)
    {
    	if(stu[i].a>=pass) s++;//计算通过人数
	}
	
	cout<<pass<<" "<<s<<endl;
	for(int i=0;i<s;i++)        //输出通过总人数 
        cout<<mian[i].h<<"   "<<mian[i].a<<endl;
    return 0;                   //好习惯
}

P1307 [NOIP2011 普及组] 数字反转 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1307

 

#include<iostream>
using namespace std;
int main()
{
  int n;  cin>>n;   //反转之前的数
  if(n<0) {cout<<"-";n=-n;}  //不管正负数,都转成正数方便操作。如果是负数,先输出一个"-"
  if(n%10==0) {n=n/10;}  //如果一个数的最后一位为0,去掉不看
  int sum=0;    //反转之后的数
  while(n!=0)
  {
        int k=n%10;
    sum=sum*10+k;   //sum*10+k的意思是在原数sum的基础上拓展一个个位并存储k(有点像栈的操作)
    n=n/10;   //去掉一位
  }
  cout<<sum<<endl;
  return 0;
}

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int reverseNumber(int N) {
    // 确定数字是否为负数
    bool isNegative = N < 0;
    
    // 获取绝对值,以便于处理
    N = abs(N);
    
    // 将数字转换为字符串
    string numStr = to_string(N);
    
    // 反转字符串
    reverse(numStr.begin(), numStr.end());
    
    // 去除反转后字符串前导零
    // 使用 stoi 直接将字符串转换为整数会自动去掉前导零
    int reversedNum = stoi(numStr);
    
    // 恢复符号
    if (isNegative) {
        reversedNum = -reversedNum;
    }
    
    return reversedNum;
}

int main() {
    int N;
    cin >> N;
    
    int reversed = reverseNumber(N);
    cout << reversed << endl;
    
    return 0;
}

P1308 [NOIP2011 普及组] 统计单词数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1308

【算法分析】

枚举文章中的每个单词;

判断两个单词长度是否相同;

枚举单词中的每个字母,判断是否都相同,如果都相同则答案加一。

【参考程序】

  #include<cstdio>
  #include<cstring>
  #include<iostream>
  using namespace std;
  int main()
  {
        int i,j,t=0,tt=0;
        char s[1000001],ss[11];
        cin.getline(ss,11);
        cin.getline(s,1000001);
        for(i=0;i<=strlen(s)-strlen(ss);++i)
        {
            for (j=0;j<=strlen(ss)-1;++j)    
            {
                if (toupper(s[j+i])!=toupper(ss[j])) break;
                if (i>0&&s[i-1]!=' ') break;
          }
            if (j==strlen(ss)&&(s[j+i]==' '||j+i==strlen(s))) 
                 {t++;if (t==1) tt=i;}
        }
        if (t==0) printf("-1");  
                 else  printf("%d %d\n",t,tt); 
           return 0;
  }

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

// 将字符串转换为小写
string toLowerCase(const string &str) {
    string lowerStr = str;
    transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
    return lowerStr;
}

int main() {
    // 读取单词和文章
    string word, article;
    getline(cin, word);
    getline(cin, article);

    // 将单词和文章都转换为小写,以实现不区分大小写
    string lowerWord = toLowerCase(word);
    string lowerArticle = toLowerCase(article);

    // 用于统计单词出现次数和记录第一次出现的位置
    int count = 0;
    int firstPos = -1;
    
    // 使用 istringstream 来按空格分割文章
    istringstream iss(lowerArticle);
    string currentWord;
    int currentPos = 0; // 当前单词在文章中的起始位置

    while (iss >> currentWord) {
        if (currentWord == lowerWord) {
            count++;
            if (firstPos == -1) {
                firstPos = currentPos;
            }
        }
        // 更新下一个单词的起始位置
        currentPos += currentWord.length() + 1; // +1 for the space
    }

    // 输出结果
    if (count > 0) {
        cout << count << " " << firstPos << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1979278.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

尚品汇-首页三级分类实现-nginx静态代理生成的静态页面(二十六)

目录&#xff1a; &#xff08;1&#xff09;问题详解 &#xff08;2&#xff09;首页商品分类实现 &#xff08;3&#xff09;修改web-all模块 &#xff08;4&#xff09;页面渲染 &#xff08;1&#xff09;问题详解 &#xff08;2&#xff09;首页商品分类实现 前面做了…

【书生大模型实战营(暑假场)】入门任务三 Python 关卡

入门任务二 Python 关卡 参考&#xff1a; 教程任务 1 闯关任务 1.1 使用 Python 实现 wordcount import stringdef wordcount(text):# 去除标点符号text text.translate(str.maketrans(, , string.punctuation))# 转换为小写text text.lower()# 分割字符串成单词列表wo…

CH571F蓝牙orUSB摇杆鼠标

演示视频&#xff1a; 短视频刷个爽 程序基本上是基于官方的例程上改的&#xff0c;用到的例程有&#xff1a;蓝牙的HID_Mouse,USB的CompoundDev&#xff0c;还有ADC&#xff0c;按键中断。 主要原理 就是ADC采集采集摇杆电压&#xff0c;通过蓝牙HID或者USB的HID发送给电脑或…

文心一言 VS 讯飞星火 VS chatgpt (317)-- 算法导论22.3 9题

九、请给出如下猜想的一个反例&#xff1a;如果有向图G包含一条从结点u到结点v的路径&#xff0c;则任何对图G的深度优先搜索都将导致v.d⩽u.f。如果要写代码&#xff0c;请用go语言。 文心一言&#xff1a; 首先&#xff0c;我们需要澄清问题中的几个关键点。在图的深度优先…

想做抖音短视频,视频素材去哪里找啊?

各位抖音上的短视频创作者们&#xff0c;是否曾幻想过自己的作品能够在全网爆火&#xff0c;却常因为缺少那些能够让视频更加生动的素材而感到困扰&#xff1f;不用担心&#xff0c;今天我要为大家介绍几个优秀的视频素材网站&#xff0c;让你的抖音之路顺风顺水&#xff01; …

Linux系统中的高级用户空间与内核空间交互技术

Linux作为一种开源操作系统&#xff0c;具有良好的稳定性、安全性和自定制性&#xff0c;因而在各种设备和场景中得到广泛应用。作为Linux系统的核心组成部分&#xff0c;内核空间与用户空间交互技术对系统性能和功能扩展起着关键作用。本文将深入探讨Linux系统中的高级用户空间…

Vue Vine:带给你全新的 Vue 书写体验!

你好&#xff0c;我是 Kagol&#xff0c;个人公众号&#xff1a;前端开源星球。 上个月和 TinyVue 的小伙伴们一起参加了 VueConf 24 大会&#xff0c;有幸认识沈青川大佬&#xff0c;并了解了他的 Vue Vine 项目&#xff0c;Vue Vine 让你可以在一个文件中通过函数方式定义多…

系统化学习 H264视频编码(05)码流数据及相关概念解读

说明&#xff1a;我们参考黄金圈学习法&#xff08;什么是黄金圈法则?->模型 黄金圈法则&#xff0c;本文使用&#xff1a;why-what&#xff09;来学习音H264视频编码。本系列文章侧重于理解视频编码的知识体系和实践方法&#xff0c;理论方面会更多地讲清楚 音视频中概念的…

Nginx进阶-常见配置(二)

一、nginx 日志配置 nginx 日志介绍 nginx 有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志, 所需日志模块 ngx_http_log_module 的支持&#xff0c;日志格式通过 log_format 命令来定义&#xff0c;日志对于统计和排错是非常有利的&#xff0c;下面总…

【TwinCAT3教程】TwinCAT3 PLC 简单程序编写与调试

一、PLC 简单程序编写 1.1 新建TwinCAT3项目 (1)打开 TwinCAT 3,点击 New TwinCAT Project 新建 TC3 项目。 (2)选择 TwinCAT Project,输入项目名称和项目保存路径,然后点击确定。 1.2 添加PLC项目 1.2.1 步骤 (1)在树形资源管理器右键点击 PLC,选择 添加新项 新…

STM32F28335实验:继电器

继电器控制电机&#xff1a; 5s启动 5s停止 循环 管脚图&#xff1a; 管脚用的是GPIO15 驱动&#xff1a; beep.c /** leds.c** Created on: 2024年8月2日* Author: Administrator*/#include<relay.h>/***************************************************…

【算法设计题】查找给定结点的双亲结点(二叉树),第3题(C/C++)

目录 第3题 查找给定结点的双亲结点&#xff08;二叉树&#xff09; 得分点&#xff08;必背&#xff09; 题解 定义函数和初始化变量&#xff1a; 处理特殊情况&#xff1a; 遍历树&#xff1a; 中序遍历左子树&#xff1a; 处理右子树&#xff1a; 返回结果&#x…

LSTM实战之预测股票

&#x1f4c8; 用PyTorch搭建LSTM模型&#xff0c;轻松预测股票价格&#xff01;&#x1f680; Hey小伙伴们&#xff0c;今天给大家带来一个超级实用的项目教程——如何用PyTorch和LSTM模型来预测股票价格&#xff01;&#x1f31f; &#x1f50d; 项目背景 我们都知道股市是…

《Unity3D网络游戏实战》学习与实践--制作一款大乱斗游戏

角色类 基类Base Human是基础的角色类&#xff0c;它处理“操控角色”和“同步角色”的一些共有功能&#xff1b;CtrlHuman类代表“操控角色”​&#xff0c;它在BaseHuman类的基础上处理鼠标操控功能&#xff1b;SyncHuman类是“同步角色”类&#xff0c;它也继承自BaseHuman&…

MySQL的数据结构B+tree以及SQL优化

首先呢&#xff0c;我们知道MySQL的数据结构为Btree,那么其结构究竟是什么样的&#xff0c;为什么选择Btree&#xff0c;而不选择Btree。下面我们从其结构分析 1.Btree平衡多路查找树 B-tree结构的数据可以让系统高效的找到数据所在的磁盘块。为了描述B-Tree,首先定义一条记录…

入门mem0.NET

入门mem0.NET 安装包 如果你的项目使用了EntityFrameworkCore,那么你可以跟随这个教程走 <ItemGroup><PackageReference Include"mem0.NET" Version"0.1.7" /><PackageReference Include"mem0.NET.Qdrant" Version"0.1.7…

云动态摘要 2024-08-04

给您带来云厂商的最新动态&#xff0c;最新产品资讯和最新优惠更新。 最新优惠与活动 数据库上云优选 阿里云 2024-07-04 RDS、PolarDB、Redis、MongoDB 全系产品新用户低至首年6折起&#xff01; [免费体验]智能助手ChatBI上线 腾讯云 2024-07-02 基于混元大模型打造&…

java之IO篇——File、字节流、字符流

前言 IO流是用于读写文件中的数据&#xff0c;要读写文件之前可以创建文件获取文件对象再创建IO流&#xff0c;正文会先介绍File类&#xff0c;通过File类的构造方法获取文件的对象&#xff0c;创建文件或目录以及File类的一些方法获取文件对象的属性。后面还介绍了相关的IO流体…

Radxa ROCK 3C开发板编译Opencv,支持调用树莓派摄像头模块V2

目录 1、ROCK 3C和树莓派摄像头模块V2介绍2、ROCK 3C在rsetup开启支持3、测试指令4、编译Opencv4.1 增加swap&#xff0c;确保内存够用4.2 安装依赖和下载opencv4.3 编译参考链接 5、使用opencv调用树莓派摄像头模块V2 1、ROCK 3C和树莓派摄像头模块V2介绍 ROCK 3C 是一款基于…

刷题篇 - 01

目录 题目一&#xff1a; 题目二&#xff1a; 题目三&#xff1a; 题目四&#xff1a; 题目五&#xff1a; 题目六&#xff1a; 题目七&#xff1a; 题目一&#xff1a; 387. 字符串中的第一个唯一字符 - 力扣&#xff08;LeetCode&#xff09; public int firstUniqC…