将下面c语言代码改为python代码 #include<stdio.h> #include<stdlib.h> struct PROCESS { char PROCESS_NAME[10]; int atime; int rtime; int ttime; int X; float Response_ratio; }; int main() { struct PROCESS p[3]; for (int i=0;i<3;i++) { scanf("%s %d %d",&p[i].PROCESS_NAME,&p[i].atime,&p[i].rtime); p[i].X=i+1; } struct PROCESS temp; int nowtime=p[0].atime; int sign=0; int flag=0; int m=0; int n=0; for(int i=1;i<3;i++) { if(p[i].atime==nowtime) { if(p[i].rtime<p[sign].rtime) { sign=i; nowtime=p[i].atime; temp=p[i]; p[i]=p[0]; p[0]=temp; } } else if(p[i].atime<nowtime) { sign=i; nowtime=p[i].atime; temp=p[i]; p[i]=p[0]; p[0]=temp; } } while (1) { if(p[m].atime>=n) { p[m].ttime=p[m].rtime; n=p[m].atime+p[m].rtime; } else { p[m].ttime=p[m].rtime+n-p[m].atime; n+=p[m].rtime; } for(int i=1;i<3;i++) { p[i].Response_ratio=(float)(n-p[i].atime)/p[i].rtime; } m++; if(m==3) { break; } if(flag==0) { if(p[1].Response_ratio>=p[2].Response_ratio) { if(p[1].Response_ratio==p[2].Response_ratio) { if(p[1].X>p[2].X) { temp=p[1]; p[1]=p[2]; p[2]=temp; } } } else { temp=p[1]; p[1]=p[2]; p[2]=temp; } flag=1; } } for(int i=0;i<2;i++) { for(int j=1;j<3;j++) { if(p[i].X>p[j].X) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } } for(int i=0;i<3;i++) { printf("%d ",p[i].ttime); } }
时间: 2023-12-17 19:06:08 浏览: 65
以下是将C语言代码转换为Python代码后的结果:
```python
class Process:
def __init__(self):
self.PROCESS_NAME = ""
self.atime = 0
self.rtime = 0
self.ttime = 0
self.X = 0
self.Response_ratio = 0.0
p = [Process() for i in range(3)]
for i in range(3):
p[i].PROCESS_NAME, p[i].atime, p[i].rtime = input().split()
p[i].atime = int(p[i].atime)
p[i].rtime = int(p[i].rtime)
p[i].X = i + 1
temp = Process()
nowtime = p[0].atime
sign = 0
flag = 0
m = 0
n = 0
for i in range(1, 3):
if p[i].atime == nowtime:
if p[i].rtime < p[sign].rtime:
sign = i
nowtime = p[i].atime
temp = p[i]
p[i] = p[0]
p[0] = temp
elif p[i].atime < nowtime:
sign = i
nowtime = p[i].atime
temp = p[i]
p[i] = p[0]
p[0] = temp
while True:
if p[m].atime >= n:
p[m].ttime = p[m].rtime
n = p[m].atime + p[m].rtime
else:
p[m].ttime = p[m].rtime + n - p[m].atime
n += p[m].rtime
for i in range(1, 3):
p[i].Response_ratio = (n - p[i].atime) / p[i].rtime
m += 1
if m == 3:
break
if flag == 0:
if p[1].Response_ratio >= p[2].Response_ratio:
if p[1].Response_ratio == p[2].Response_ratio:
if p[1].X > p[2].X:
temp = p[1]
p[1] = p[2]
p[2] = temp
else:
temp = p[1]
p[1] = p[2]
p[2] = temp
flag = 1
for i in range(2):
for j in range(1, 3):
if p[i].X > p[j].X:
temp = p[i]
p[i] = p[j]
p[j] = temp
for i in range(3):
print(p[i].ttime, end=' ')
```
阅读全文