基础算法之模拟

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

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值