c++程序翻译成PASCAL
把这段c++程序翻译成PASCAL#include<iostream>#include<string.h>#include<stdlib.h>#include<stdio...
把这段c++程序翻译成PASCAL
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define inf 10000000000000.0
#define maxn 155
#define pn 200
int n;
struct line {
double x1, y1, x2, y2;
};
struct point {
double x, y, sum;
};
line l[maxn];
point p[pn + 1];
point bp;
inline double dis(double x1, double y1, double x2, double y2) {
return hypot(x1 - x2, y1 - y2);
}
inline double min(double a, double b) {
return a < b ? a : b;
}
double count(double x, double y) {
double sum = 0;
for (int i = 1; i <= n; i++) {
if (l[i].y1 == l[i].y2)
if (l[i].x1 <= x && x <= l[i].x2 || l[i].x1 >= x && x >= l[i].x2)
sum += fabs(l[i].y1 - y);
else
sum += min(dis(x, y, l[i].x1, l[i].y1),
dis(x, y, l[i].x2, l[i].y2));
else
if (l[i].y1 <= y && y <= l[i].y2 || l[i].y1 >= y && y >= l[i].y2)
sum += fabs(l[i].x1 - x);
else
sum += min(dis(x, y, l[i].x1, l[i].y1),
dis(x, y, l[i].x2, l[i].y2));
}
return sum;
}
void init() {
for (int i = 1; i <= pn; i++) {
p[i].x = (double)(rand() % 100);
p[i].y = (double)(rand() % 100);
p[i].sum = count(p[i].x, p[i].y);
}
bp.sum = inf;
p[0].sum = inf;
}
int choose() {
int b = 0;
for (int i = 1; i <= 10; i++) {
int k = rand() % pn + 1;
if (p[k].sum < p[b].sum)
b = k;
}
return b;
}
double rate() {
return (rand() % 140 - 70) / 100.0;
}
void mix(int a, int b) {
point na, nb;
na.x = (p[a].x + p[b].x) / 2 + (fabs(p[a].x - p[b].x) + 0.1) * rate();
na.y = (p[a].y + p[b].y) / 2 + (fabs(p[a].y - p[b].y) + 0.1) * rate();
na.sum = count(na.x, na.y);
p[rand() % pn + 1] = na;
if (na.sum < bp.sum)
bp = na;
}
void sim() {
int a, b;
a = choose();
b = choose();
mix(a, b);
}
int main() {
freopen("fence3.in", "r", stdin);
freopen("fence3.out", "w", stdout);
srand(123);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lf%lf%lf%lf", &l[i].x1, &l[i].y1, &l[i].x2, &l[i].y2);
init();
for (int i = 1; i <= 1000; i++)
sim();
printf("%.1lf %.1lf %.1lf\n", bp.x, bp.y, bp.sum);
fclose(stdin);
fclose(stdout);
return 0; 展开
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define inf 10000000000000.0
#define maxn 155
#define pn 200
int n;
struct line {
double x1, y1, x2, y2;
};
struct point {
double x, y, sum;
};
line l[maxn];
point p[pn + 1];
point bp;
inline double dis(double x1, double y1, double x2, double y2) {
return hypot(x1 - x2, y1 - y2);
}
inline double min(double a, double b) {
return a < b ? a : b;
}
double count(double x, double y) {
double sum = 0;
for (int i = 1; i <= n; i++) {
if (l[i].y1 == l[i].y2)
if (l[i].x1 <= x && x <= l[i].x2 || l[i].x1 >= x && x >= l[i].x2)
sum += fabs(l[i].y1 - y);
else
sum += min(dis(x, y, l[i].x1, l[i].y1),
dis(x, y, l[i].x2, l[i].y2));
else
if (l[i].y1 <= y && y <= l[i].y2 || l[i].y1 >= y && y >= l[i].y2)
sum += fabs(l[i].x1 - x);
else
sum += min(dis(x, y, l[i].x1, l[i].y1),
dis(x, y, l[i].x2, l[i].y2));
}
return sum;
}
void init() {
for (int i = 1; i <= pn; i++) {
p[i].x = (double)(rand() % 100);
p[i].y = (double)(rand() % 100);
p[i].sum = count(p[i].x, p[i].y);
}
bp.sum = inf;
p[0].sum = inf;
}
int choose() {
int b = 0;
for (int i = 1; i <= 10; i++) {
int k = rand() % pn + 1;
if (p[k].sum < p[b].sum)
b = k;
}
return b;
}
double rate() {
return (rand() % 140 - 70) / 100.0;
}
void mix(int a, int b) {
point na, nb;
na.x = (p[a].x + p[b].x) / 2 + (fabs(p[a].x - p[b].x) + 0.1) * rate();
na.y = (p[a].y + p[b].y) / 2 + (fabs(p[a].y - p[b].y) + 0.1) * rate();
na.sum = count(na.x, na.y);
p[rand() % pn + 1] = na;
if (na.sum < bp.sum)
bp = na;
}
void sim() {
int a, b;
a = choose();
b = choose();
mix(a, b);
}
int main() {
freopen("fence3.in", "r", stdin);
freopen("fence3.out", "w", stdout);
srand(123);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lf%lf%lf%lf", &l[i].x1, &l[i].y1, &l[i].x2, &l[i].y2);
init();
for (int i = 1; i <= 1000; i++)
sim();
printf("%.1lf %.1lf %.1lf\n", bp.x, bp.y, bp.sum);
fclose(stdin);
fclose(stdout);
return 0; 展开
3个回答
展开全部
srand 无法翻译,这是C++里的随即种子,pascal无用
Const
inf=10000000000000.0;
maxn=155;
pn=200;
Type
line=Record
x1,y1,x2,y2:double;
End;
point=Record
x,y,sum:double;
End;
Var
n,i:longint;
l:array[0..maxn] of line;
p:array[0..pn+1] of point;
bp:point;
//
Function hypot(x,y:double):double;
Begin
Exit(sqrt(x*x+y*y));
End;
Function dis(x1,y1,x2,y2:double):double;
Begin
Exit(hypot(x1-x2,y1-y2));
End;
Function min(a,b:double):double;
Begin
if a<b then Exit(a);
Exit(b);
End;
Function count(x,y:double):double;
Var
sum:double;
i:longint;
Begin
sum:=0;
For i:=1 to n do
If l[i].y1=l[i].y2 then
If ((l[i].x1<=x) and (x<=l[i].x2)) or ((l[i].x1>=x) and (x>=l[i].x2)) then sum:=sum+abs(l[i].y1-y)
else sum:=sum+min(dis(x,y,l[i].x1,l[i].y1),dis(x,y,l[i].x2,l[i].y2))
else
If ((l[i].y1<=y) and (y<=l[i].y2)) or ((l[i].y1>=y) and (y>=l[i].y2)) then sum:=sum+abs(l[i].x1-x)
else sum:=sum+min(dis(x,y,l[i].x1,l[i].y1),dis(x,y,l[i].x2,l[i].y2));
Exit(sum);
End;
Procedure Init;
Var
i:longint;
Begin
Randomize;
For i:=1 to pn do
Begin
p[i].x:=random(100);
p[i].y:=random(100);
p[i].sum:=count(p[i].x,p[i].y);
End;
bp.sum:=inf;
p[0].sum:=inf;
End;
Function choose:longint;
Var
b:longint;
k,i:longint;
Begin
Randomize;
b:=0;
For i:=1 to 10 do
Begin
k:=random(pn)+1;
If (p[k].sum<p[b].sum) then b:=k;
End;
Exit(b);
End;
Function rate:double;
Begin
Exit((random(140)-70)/100);
End;
Procedure Mix(a,b:longint);
Var
na,nb:point;
Begin
Randomize;
na.x:=(p[a].x+p[b].x)/2+(abs(p[a].x-p[b].x)+0.1)*rate;
na.y:=(p[a].y+p[b].y)/2+(abs(p[a].y-p[b].y)+0.1)*rate;
na.sum:=count(na.x,na.y);
p[random(pn)+1]:=na;
If na.sum<bp.sum then bp:=na;
End;
Procedure sim;
Var
a,b:longint;
Begin
a:=choose;
b:=choose;
mix(a,b);
End;
Begin
assign(input,'fence3.in'); reset(Input);
assign(Output,'fence3.out'); Rewrite(Output);
readln(n);
For i:=1 to n do
with l[i] do
Readln(x1,y1,x2,y2);
Init;
For i:=1 to 1000 do
sim;
Writeln(bp.y:0:1,' ',bp.y:0:1,' ',bp.sum:0:1);
Close(input); Close(output);
End.
这是我翻译的- -
Const
inf=10000000000000.0;
maxn=155;
pn=200;
Type
line=Record
x1,y1,x2,y2:double;
End;
point=Record
x,y,sum:double;
End;
Var
n,i:longint;
l:array[0..maxn] of line;
p:array[0..pn+1] of point;
bp:point;
//
Function hypot(x,y:double):double;
Begin
Exit(sqrt(x*x+y*y));
End;
Function dis(x1,y1,x2,y2:double):double;
Begin
Exit(hypot(x1-x2,y1-y2));
End;
Function min(a,b:double):double;
Begin
if a<b then Exit(a);
Exit(b);
End;
Function count(x,y:double):double;
Var
sum:double;
i:longint;
Begin
sum:=0;
For i:=1 to n do
If l[i].y1=l[i].y2 then
If ((l[i].x1<=x) and (x<=l[i].x2)) or ((l[i].x1>=x) and (x>=l[i].x2)) then sum:=sum+abs(l[i].y1-y)
else sum:=sum+min(dis(x,y,l[i].x1,l[i].y1),dis(x,y,l[i].x2,l[i].y2))
else
If ((l[i].y1<=y) and (y<=l[i].y2)) or ((l[i].y1>=y) and (y>=l[i].y2)) then sum:=sum+abs(l[i].x1-x)
else sum:=sum+min(dis(x,y,l[i].x1,l[i].y1),dis(x,y,l[i].x2,l[i].y2));
Exit(sum);
End;
Procedure Init;
Var
i:longint;
Begin
Randomize;
For i:=1 to pn do
Begin
p[i].x:=random(100);
p[i].y:=random(100);
p[i].sum:=count(p[i].x,p[i].y);
End;
bp.sum:=inf;
p[0].sum:=inf;
End;
Function choose:longint;
Var
b:longint;
k,i:longint;
Begin
Randomize;
b:=0;
For i:=1 to 10 do
Begin
k:=random(pn)+1;
If (p[k].sum<p[b].sum) then b:=k;
End;
Exit(b);
End;
Function rate:double;
Begin
Exit((random(140)-70)/100);
End;
Procedure Mix(a,b:longint);
Var
na,nb:point;
Begin
Randomize;
na.x:=(p[a].x+p[b].x)/2+(abs(p[a].x-p[b].x)+0.1)*rate;
na.y:=(p[a].y+p[b].y)/2+(abs(p[a].y-p[b].y)+0.1)*rate;
na.sum:=count(na.x,na.y);
p[random(pn)+1]:=na;
If na.sum<bp.sum then bp:=na;
End;
Procedure sim;
Var
a,b:longint;
Begin
a:=choose;
b:=choose;
mix(a,b);
End;
Begin
assign(input,'fence3.in'); reset(Input);
assign(Output,'fence3.out'); Rewrite(Output);
readln(n);
For i:=1 to n do
with l[i] do
Readln(x1,y1,x2,y2);
Init;
For i:=1 to 1000 do
sim;
Writeln(bp.y:0:1,' ',bp.y:0:1,' ',bp.sum:0:1);
Close(input); Close(output);
End.
这是我翻译的- -
展开全部
这叫c语言。。。
这不难懂啊?!
从最后面的main()开始看,那个就是begin end.
前面是一些函数。
“翻译”成pascal,我不知到怎样做到信达雅,scanf(),printf()的用法无法用read,readln,write,writeln完美模拟。include<>也不好办,这些东西pascal里都在system 单元里,这么好几句include 根本译不出来。
这不难懂啊?!
从最后面的main()开始看,那个就是begin end.
前面是一些函数。
“翻译”成pascal,我不知到怎样做到信达雅,scanf(),printf()的用法无法用read,readln,write,writeln完美模拟。include<>也不好办,这些东西pascal里都在system 单元里,这么好几句include 根本译不出来。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有专门的翻译工具,你可以试试看。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询