锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. 基础算法之模拟

基础算法之模拟

0
  • 软件开发
  • 发布于 2024-09-21
  • 0 次阅读
黄健
黄健

1P1093 [NOIP2007 普及组] 奖学金 - 洛谷 \| 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P1093https://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;
}
```hljs
#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)https://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)https://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)https://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)https://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;
}


原文链接: https://blog.csdn.net/2302_80084329/article/details/140913616

标签: #软件开发 1171
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.