pat-1025 PAT Ranking

本文探讨了排序算法在不同地点的应用,通过组内排序和全局排序,详细介绍了如何使用C++实现排序过程,包括局部排序、全局排序、排名计算及结果输出。
不同地点一起排序
先组内排序,再全局排序
将小组添加进全局

vector拼接
globallist.insert(globallist.end(),locallist.begin(),locallist.end());



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

struct Score
{
string id;
int score;
int local;
int global;
int location;
};

bool sortByScore(Score a,Score b)
{
if(a.score != b.score)
return a.score > b.score;
else
return a.id < b.id;
}


int main()
{
vector<Score> globallist;
vector<Score> locallist;

int N;
int K;

cin>>N;
for(int n=1;n<=N;n++)
{
cin>>K;
while(K--)
{
Score s;
cin >> s.id;
cin >> s.score;
s.location = n;
locallist.push_back(s);
}

sort(locallist.begin(),locallist.end(),sortByScore);
//rank
for(int i=0;i<locallist.size();i++)
{
if(i!=0 && locallist[i].score == locallist[i-1].score)
locallist[i].local = locallist[i-1].local;
else
locallist[i].local = i+1;
}
//add
globallist.insert(globallist.end(),locallist.begin(),locallist.end());
locallist.clear();
}

//rank
cout<<globallist.size()<<endl;
sort(globallist.begin(),globallist.end(),sortByScore);
for(int i=0;i<globallist.size();i++)
{
if(i!=0 && globallist[i].score == globallist[i-1].score)
globallist[i].global = globallist[i-1].global;
else
globallist[i].global = i+1;
cout<<globallist[i].id<<" "<<globallist[i].global<<" "<<globallist[i].location<<" "<<globallist[i].local<<endl;
}


}


### dv-scroll-ranking-board 使用方法及示例代码 `dv-scroll-ranking-board` 是 DataV 提供的一个用于实现排名轮播图的组件。以下是关于其使用方法和示例代码的详细说明: #### 1. 基本用法 `dv-scroll-ranking-board` 的基本用法需要通过 `config` 属性传递配置项。以下是一个简单的示例代码[^1]: ```html <template> <div> <dv-scroll-ranking-board :config="config" style="width: 95%; height: 370px;" /> </div> </template> <script> export default { data() { return { config: { header: ['排名', '名称', '值'], // 表头 data: [ { name: '公司A', value: 120 }, { name: '公司B', value: 95 }, { name: '公司C', value: 85 } ], // 数据 rowNum: 5, // 显示行数 headerBGC: '#426ab3', // 表头背景色 oddRowBGC: '#fff', // 奇数行背景色 evenRowBGC: '#f5f5f5' // 偶数行背景色 } }; } }; </script> ``` #### 2. 动态数据加载 如果需要从接口获取动态数据,可以参考以下代码[^2]: ```javascript export default { data() { return { config: { data: [] } }; }, methods: { getList() { this.$axios.get('https://example.com/api/data').then((response) => { if (response.data.code === 200) { const result = response.data.result.map(item => ({ name: item.organizationName, value: item.count })); this.config.data = result; this.config = { ...this.config }; // 重新赋值以触发渲染 } }); } }, mounted() { this.getList(); } }; ``` #### 3. 空数据展示优化 当接口返回的数据为空时,可以通过设置默认值或提示信息来优化用户体验。例如: ```javascript watch: { 'config.data': { handler(newVal) { if (newVal.length === 0) { this.config.data = [{ name: '暂无数据', value: 0 }]; } }, immediate: true } } ``` #### 4. 渲染问题解决 如果遇到 `dv-scroll-ranking-board` 渲染不出来的问题,可能是由于数据格式不正确或未及时更新配置对象。确保在更新 `config` 时使用扩展运算符 `...` 来重新赋值[^3][^4]。 例如: ```javascript this.config.data = newData; this.config = { ...this.config }; // 强制更新组件 ``` #### 5. 自定义样式 可以通过 `style` 属性为组件设置宽度和高度,并通过 `config` 配置项自定义表头背景色、行背景色等[^1]: ```html <dv-scroll-ranking-board :config="config" style="width: 500px; height: 500px;" /> ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值