переделать c Pascal d C++
04.07.2010, 21:59. Просмотров 687. Ответов 0
Здраствуйте можете мне помочь допеределать код Pascal в код C++.
Pascal | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
| const
FileIn = 'bus.dat'; {Input file}
FileOut = 'bus.sol'; {Output file}
MaxD = 100; {Maximal bus lines}
MaxS = 250; {Maximal bus stops}
type
TNodS = array[1..MaxS, 1..MaxS] of byte;
var
ManyTest, Test : integer; {Vars to work with many testes}
Fi,Fo : Text; {Text files}
S, D : integer;
Nods : ^TNodS;
Z : array[1..MaxD, 1..MaxS] of byte; {Matrics of meeting}
A : array[1..MaxD, 1..MaxD] of byte; {Matrics of smezhnost'}
B : array[1..MaxD] of boolean; {Search at depth}
ManyB : integer; {Many truth in B}
L : array[1..MaxD, 1..MaxS] of byte; {List of lines}
Many : array[1..MaxD] of integer; {Many of stops in lines}
procedure Swap(var a,b : integer);
var
c: integer;
begin
c:=a;a:=b;b:=c;
end;
function Nod(a,b : integer) : integer;
begin
if a<b then Swap(a,b);
a:=a mod b;
if a<b then Swap(a,b);
if B=0 then Nod:=a else Nod:=Nods^[b,a];
end;
procedure GetNod;
var
i,j : integer;
begin
New(NodS);
FillChar(Nods^,sizeof(Nods^),0);
for i:=1 to MaxS do
for j:=i to MaxS do
begin
Nods^[i,j]:=nod(i,j);
Nods^[j,i]:=Nods^[i,j];
end;
end;
procedure Init;
var
i,j : integer;
begin
FillChar(Z, SizeOf(Z), 0);
ReadLn(Fi, D, S);
for i:=1 to d do
begin
Read(Fi, Many[i]);
for j:=1 to Many[i] do
begin
Read(Fi, L[i,j]);
Z[i,L[i,j]]:=j;
end;
end;
FillChar(A, SizeOf(A), 0);
FillChar(B, SizeOf(B), false);
ManyB:=0;
end;
function Check(y,x : integer) : boolean;
var
i,CurrNod, ShiftX, ShiftY : integer;
Answer : boolean;
begin
if Many[x]<Many[y] then Swap(X,Y);
Answer:=false; i:=1;
while (i<=many[y])and(not Answer) do
begin
if Z[x,L[y,i]]<>0 then
begin
CurrNod := Nods^[Many[y], Many[x]];
ShiftY:=Many[y]-i; if ShiftY<=0 then ShiftY:=ShiftY+Many[y];
ShiftX:=Many[x]-Z[x,L[y,i]]; if ShiftX<=0 then ShiftX:=ShiftX+Many[x];
if Abs(ShiftY-ShiftX) mod CurrNod = 0 then Answer:=true;
end;
Inc(I);
end;
Check:=Answer;
end;
procedure Search(Start : integer);
var
i : integer;
begin
if B[Start] then exit;
B[Start]:=true;
Inc(ManyB);
for i:=1 to d do
if not B[i] then
begin
if A[Start,i]=0 then
begin
if Check(Start,i) then A[Start,i]:=1 else A[Start,i]:=2;
A[i, Start]:=A[Start,i];
end;
if A[Start, i]=1 then Search(i);
end;
end;
procedure Run;
begin
Search(1);
end;
procedure Done;
begin
if ManyB=d then WriteLn(Fo, 1) else Writeln(Fo, 0);
end;
begin
GetNod;
Assign(Fi, FileIN); Reset(Fi);
Assign(Fo, FileOut); Rewrite(Fo);
ReadLn(Fi, ManyTest);
for Test:=1 to ManyTest do
begin
Init;
Run;
Done;
end;
Close(Fi);
Close(Fo);
Dispose(Nods);
end. |
|
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
| #include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "iostream"
#include "fstream"
using namespace std;
const int maxd=100,maxs=250;
/*FileIn = 'bus.dat'; {Input file}
FileOut = 'bus.sol'; {Output file}*/
typedef double tnods[maxs][maxs];
int manytest,test,s,d,manyb; //{Vars to work with many testes}
//Fi,Fo : Text; //{Text files}
tnods nods;
double z[maxd][maxs]; //{Matrics of meeting}
double a[maxd][maxs]; //{Matrics of smezhnost}
double l[maxd][maxs]; //{List of lines}
bool b[maxd]; //{Search at depth}
int many[maxd]; //{Many of stops in lines}
FILE *fp,*fi,*fo;
void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
}
int nod(int a,int b)
{
int k;
if (a<b) swap(a,b);
k=(a % b);
if (a<b) swap(a,b);
if (b=0) return a;
//else *nod=nods[b][a];
}
void getnod()
{
int i,j;
nods=malloc(sizeof(int));
//fillchar(*nods,sizeof(*nods),0);
for (i=1;i<maxs;i++)
{
for(j=i;j<maxs;j++)
{
&nods[i][j]=nod[i][j];
nods[j][i]=&nods[i][j];
}
}
}
void init()
{
int i,j;
//fillChar(Z, SizeOf(Z), 0);
readln(Fi, D, S);
for(i=1;i<d;i++)
{
//read(Fi, many[i]);
for(j=1;j<many[i];j++)
{
read(Fi, L[i,j]);
z[i][l[i,j]]=j;
}
}
//FillChar(A, SizeOf(A), 0);
//FillChar(B, SizeOf(B), false);
manyb=0;
}
bool check(int y,int x)
{
int i,currnod, shiftx, shifty;
bool answer;
{
if (many[x]<many[y])
swap(x,y);
answer=false;
i=1;
while (i<=many[y])and(!answer) do
{
if (z[x][l[y,i]]!=0)
{
currnod=nods^[many[y]][many[x]];
shifty:=many[y]-i;
{
if (shifty<=0) shifty=(shifty+many[y]);
shiftx=many[x]-z[x,L[y,i]];
if (shiftx<=0) shiftx=(shiftx+many[x])
if (abs(shifty-shiftx) mod currnod=0) answer=true;
}
i++;
}
}
}
return answer;
}
void search(int start)
{
int i;
{
if b[start]
exit(0);
b[start]=true;
manyb++;
for(i=1;i<d;i++)
{
if (!b[i])
{
if (a[start,i]=0)
{
if check(start,i)
a[start,i]=1
else a[Start,i]=2;
a[i, start]=a[start,i];
}
if a[start,i]=1
search(i);
}
}
}
}
void run()
{
search(1);
}
void done()
{
if manyb=d
//WriteLn(Fo, 1)
else //Writeln(Fo, 0);
}
void main()
{
getNod;
ifstream u;
u.open("bus.dat");
//assign(Fi, FileIN); Reset(Fi);
//assign(Fo, FileOut); Rewrite(Fo);
readLn(Fi, ManyTest);
for(test=1;test<manytest;test++)
{
init;
run;
done;
}
u.close();
//close(Fi);
//close(Fo);
dispose(Nods); |
|
0
|