1012. The Best Rank 解析

本文介绍了一个用于学生竞赛成绩排名的程序实现,通过C++代码详细展示了如何对学生在不同科目上的成绩进行排序,并处理并列情况,确保排名准确无误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意排名的顺序。有并列的同名次。然后下一名要跳位。 比如 88 88 99 70 的排名应该为 2 2 1 4注意这点应该就能AC了

这个代码敲得好乱。。。。

更新了 代码 看着清爽多了

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <climits>
#include <cstring>
#include <map>
#include <stack>
#include <set>

#define MAX 2010

using namespace std;

struct node{
	string id;
	int g[4];
	int rank,subject;
	node(){rank = MAX;subject =-1;};
};
node list[MAX];
int r[MAX];//排序用
map <string,int> str2int;
char subject[4] = {'A','C','M','E'};


int p = 0;//指向哪一科
bool cmp(int a ,int b){
	return list[a].g[p] > list[b].g[p];
}

int n,k;

int main(){

	cin >> n >> k;
	for(int i = 1 ;i <= n ;i++){
		cin >> list[i].id >> list[i].g[1] >> list[i].g[2] >> list[i].g[3];
		list[i].g[0] = list[i].g[1] + list[i].g[2] + list[i].g[3];
		r[i] = i;//初始化排序序列
		str2int[list[i].id] = i;
	}

	for(int i = 0 ;i < 4 ;i++){
		p = i;
		sort(&r[1],&r[1]+n,cmp);
		int nowR = 1;
		int preG = list[r[1]].g[p];
		for(int j = 1 ; j <=n ;j++){
			int nowP = r[j];
			if(preG == list[nowP].g[p]){
				if(list[nowP].rank > nowR){
					list[nowP].rank = nowR;
					list[nowP].subject = p;
				}
			}
			else{
				nowR = j;
				preG = list[nowP].g[p];
				if(list[nowP].rank > nowR){
					list[nowP].rank = nowR;
					list[nowP].subject = p;
				}
			}
		}
	}

	string check;
	for(int i = 0; i < k;i++){
		cin >> check;
		int nowP = str2int[check];
		if(nowP == 0)
			cout << "N/A" << endl;
		else
			cout << list[nowP].rank << " " <<  subject[list[nowP].subject] << endl;
	}
	
	return 0;
}


function [P,Uinit,output] = cp_als(X,R,varargin) %CP_ALS Compute a CP decomposition of any type of tensor. % % P = CP_ALS(X,R) computes an estimate of the best rank-R % CP model of a tensor X using an alternating least-squares % algorithm. The input X can be a tensor, sptensor, ktensor, or % ttensor. The result P is a ktensor. % % P = CP_ALS(X,R,'param',value,...) specifies optional parameters and % values. Valid parameters and their default values are: % 'tol' - Tolerance on difference in fit {1.0e-4} % 'maxiters' - Maximum number of iterations {50} % 'dimorder' - Order to loop through dimensions {1:ndims(A)} % 'init' - Initial guess [{'random'}|'nvecs'|cell array] % 'printitn' - Print fit every n iterations; 0 for no printing {1} % % [P,U0] = CP_ALS(...) also returns the initial guess. % % [P,U0,out] = CP_ALS(...) also returns additional output that contains % the input parameters. % % Note: The "fit" is defined as 1 - norm(X-full(P))/norm(X) and is % loosely the proportion of the data described by the CP model, i.e., a % fit of 1 is perfect. % % NOTE: Updated in various minor ways per work of Phan Anh Huy. See Anh % Huy Phan, Petr Tichavsk�, Andrzej Cichocki, On Fast Computation of % Gradients for CANDECOMP/PARAFAC Algorithms, arXiv:1204.1586, 2012. % % Examples: % X = sptenrand([5 4 3], 10); % P = cp_als(X,2); % P = cp_als(X,2,'dimorder',[3 2 1]); % P = cp_als(X,2,'dimorder',[3 2 1],'init','nvecs'); % U0 = {rand(5,2),rand(4,2),[]}; %<-- Initial guess for factors of P % [P,U0,out] = cp_als(X,2,'dimorder',[3 2 1],'init',U0); % P = cp_als(X,2,out.params); %<-- Same params as previous run % % See also KTENSOR, TENSOR, SPTENSOR, TTENSOR. % %MATLAB Tensor Toolbox. %Copyright 2015, Sandia Corporation. % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. % http://www.sandia.gov/~tgkolda/TensorToolbox. % Copyright (2015) Sandia Corporation. Under the terms of Contract % DE-AC04-9
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值