Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么两个程序Pascal的能过,C++的就过不了?

Posted by Lvzhenkai at 2005-10-06 10:12:49 on Problem 1007
program No1007;
var
  num,ord1:array[1..100] of integer;
  st:array[1..100] of string;
  n,m:integer;
procedure init;
var
  i:integer;
begin
  readln(m,n);
  for i:=1 to n do
    readln(st[i]);
end;
procedure cal;
var
  i,j,k,tot:integer;
begin
  fillchar(num,sizeof(num),0);
  for i:=1 to n do
    begin
      tot:=0;
      for j:=1 to m-1 do
        for k:=j+1 to m do
          if st[i][j]>st[i][k] then tot:=tot+1;
      num[i]:=tot;
    end;
end;
procedure sort;
var
  i,j,p:integer;
begin
  for i:=1 to n do
    ord1[i]:=i;
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if num[i]>num[j] then
          begin
            p:=num[i];num[i]:=num[j];num[j]:=p;
            p:=ord1[i];ord1[i]:=ord1[j];ord1[j]:=p;
          end;
end;
procedure print;
var
  i:integer;
begin
  for i:=1 to n do
    writeln(st[ord1[i]]);
end;
begin
  init;
  cal;
  sort;
  print;
end.

------------------------------------------------------------------------
//1007.cpp
#include <iostream>
using namespace std;
char st1[100][50];
int num[100]; 
int ord[100];
int n,m;
void cal()
{
    int i,j,k,tot(0);
    for (i=1;i<=n;i++)
    {
        tot=0;
        for (j=1;j<=m-1;j++)
            for  (k=j+1;k<=m;k++)
                if (st1[i][j]>st1[i][k]) tot++;
        num[i]=tot;
    }        
}
void sort()
{
    int i,j,p;
    for (i=1;i<=n;i++)
        ord[i]=i;
    for (i=1;i<=n-1;i++)
        for (j=i+1;j<=n;j++)
            if (num[i]>num[j])
            {
                p=num[i];num[i]=num[j];num[j]=p;
                p=ord[i];ord[i]=ord[j];ord[j]=p;
            }
}        
void print()
{
    int i,j;
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
                cout<<st1[ord[i]][j];
        cout<<endl;
    }            
}    
int main()
{
    void cal();
    void sort();
    void print();  
    int i,j;
    cin>>m>>n;
    for (i=1;i<=n;i++)
        for (j=1;j<=m;j++)
          cin>>st1[i][j];
    cal();
    sort();
    print();
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator