我使用dev-c ++进行编码并使用TDM-gcc-4.9-2
嗨,我做完作业,做完了。然后我将它提交给linux自动评分网站。但它不起作用。
所以我在linux中测试了geany和g ++ 7.3.0
我经常使用Global变量。似乎'rank'变量似乎是问题。
你解释这个代码有什么问题吗?谢谢
#include <iostream>
#include <fstream>
using namespace std;
int **data, **rank, **sorted, **superb;
int N, K, T; //student, subject, standard_subject
int memory = 50;
void inputdata(){
ifstream in("5.inp");
if( in.is_open() ){
in>>N>>K>>T;
data = new int*[N];
for(int x = 0; x<N; x++){
data[x] = new int[K+1];
}
rank = new int*[N];
for(int x = 0; x<N; x++){
rank[x] = new int[K+1];
for(int y=0; y<K+1; y++){
rank[x][y] = 1;
}
}
sorted = new int*[N];
for(int x = 0; x<N; x++){
sorted[x] = new int[K+1];
}
superb = new int*[N];
for(int x = 0; x<N; x++){
superb[x] = new int[K+1];
}
//data inputing
cout<<"N is "<<N<<" K is "<<K<<" T is "<<T<<endl;
for(int i = 0; i<N; i++){
for(int j = 0; j<K+1; j++){
in>>data[i][j];
//cout<<"i is "<<i<<" j is "<<j<<" "<<data[i][j]<<" ";
}
//cout<<endl;
}
for(int x = 0; x < N; x++){
for(int y = 0; y<K+1;y++){
cout<<data[x][y]<<" ";
}
cout<<endl;
}
}
else
cout<<"input error"<<endl;
}
void ranking(){
for(int x = 0; x<N;x++)
rank[x][0] = data[x][0]; //ID
cout<<K<<endl;
for(int j = 1; j<K+1; j++){
for(int i= 0; i<N; i++){
for(int x= 0; x<N; x++){
if(x==i) continue;
//cout<<"i is "<<i<<" j is "<<j<<" data i is "<<data[i][j]<<" x is "<<x<<" data x is "<<data[x][j]<<endl;
if(data[i][j]<data[x][j]){
rank[i][j] += 1;
}
}
}
}
}
void sortedByT(){
for(int i = 0; i<N; i++){
for(int j = 0; j<K+1; j++){
sorted[rank[i][T]-1][j] = rank[i][j];
}
}
}
void swap(int **arr, int index1, int index2){
int temp;
for(int j=0; j<K+1; j++){
temp = arr[index1][j];
arr[index1][j] = arr[index2][j];
arr[index2][j] = temp;
}
}
int takeSuperb(){
int numOfSuperb = 0;
for(int i = 0; i<N; i++){
int ranker = 0;
for(int j = 1; j<K+1; j++){
if(rank[i][j]<4)
ranker += 1;
if(ranker>=4){
for(int x=0; x<K+1; x++)
superb[numOfSuperb][x] = rank[i][x];
numOfSuperb += 1;
break;
}
}
}
for(int x = 0; x<numOfSuperb; x++) //buble sort
for(int y = x+1; y<numOfSuperb; y++)
if(superb[x][0]>superb[y][0])
swap(superb, x, y);
return numOfSuperb;
}
int combination(int n, int r){
if(n==0||r==0) return 1;
else return combination(n-1, r-1) + combination(n-1,r);
}
//int *combiArr = new int[combination(N,2)][2];
int combiArr[2], Cresult=0;
void combiSearcher(int *arr, int index, int n, int r, int target){
if(r==0){
bool s1win2 = false, s2win10 = false, s2win2 = false, s1win10 =
false;
for(int j = 1; j<K+1; j++){
if(rank[combiArr[0]][j]+2 < rank[combiArr[1]][j]) s1win2 =
true;
if(rank[combiArr[0]][j] > rank[combiArr[1]][j]+10) s2win10 =
true;
if(rank[combiArr[0]][j] > rank[combiArr[1]][j]+2) s2win2 =
true;
if(rank[combiArr[0]][j]+10 < rank[combiArr[1]][j]) s1win10
= true;
if( (s1win2&&s2win10) || (s2win2&&s1win10) ){
Cresult += 1;
//cout<<combiArr[0]+1<<" , "<<combiArr[1]+1<<endl;
break;
}
}
}
else if(target == n) return;
else{
arr[index] = target;
combiSearcher(arr, index+1, n, r-1, target+1);
combiSearcher(arr, index, n, r, target+1);
}
}
int main(){
inputdata();
ranking();
sortedByT();
cout<<"sorted by T"<<endl<<N<<endl;
for(int x = 0; x < N; x++){
for(int y = 0; y<K+1;y++){
cout<<sorted[x][y]<<" ";
}
cout<<endl;
}
int result = takeSuperb();
cout<<"superb"<<endl<<result<<endl;
if(result>0){
for(int i = 0; i<result; i++){
for(int j = 0; j<=K;j++){
cout<<superb[i][j]<<" ";
}
cout<<endl;
}
}
if(N>10){
combiSearcher(combiArr, 0, N, 2, 0);
}
else{
Cresult = 0;
}
cout<<Cresult<<endl;
从C ++ 11开始,标准库中就有一个struct std::rank
。因为你使用using namespace std;
你对rank
的定义与它发生冲突。
如果你在gcc 4.9上编译没有额外的编译器标志,那么C ++ 03将是默认使用的,并且不会发生这个问题。
对于较新的gcc版本,但是C ++ 11(或更高版本)是默认版本,并且会出现问题。
这也不是一个孤立的案例。例如,对于C ++ 17,data
会遇到同样的问题。
你的代码中的另一个例子是swap
,它也在标准库中定义,但不会立即给你编译错误,因为你的定义将被认为是std::swap
的重载,它总是需要两个参数。
这正是为什么不应该使用using namespace std;
而是始终使用std::
限定使用它的东西。
rank
保留(关键字)在std
参考:https://en.cppreference.com/w/cpp/types/rank
尝试使用另一个单词作为变量名称。