标题翻译
How can i separate values in an array like this..[(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)]?
问题
例如,您有患者的入院和出院日期的数组(入院,出院)
类似 [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)],我想要将每个元素分开..
帮助我!
英文翻译
For example, you have array of entry and exit date of patients(entry, exit)
like [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)] and i want to separate each elements ..
Help me!
答案1
得分: 0
以下是代码的翻译部分:
import java.util.*;
public class ArraySeparation {
public static void main(String[] args) {
Integer[] patient = {1, 3, 2, 5, 4, 8, 9, 10};
int n = patient.length;
ArrayList<Integer> entry = new ArrayList<Integer>();
ArrayList<Integer> exit = new ArrayList<Integer>();
int i = 0;
while (i < n) {
if (i != n)
entry.add(patient[i]);
i += 2;
}
i = 1;
while (i < n) {
if (i != n)
exit.add(patient[i]);
i += 2;
}
System.out.println("Entry List:");
for (int l : entry) System.out.print(l + " ");
System.out.println("\nExit List:");
for (int l : exit) System.out.print(l + " ");
}
}
/*
* 输出:
* Entry List:
* 1 2 4 9
* Exit List:
* 3 5 8 10
*/
英文翻译
You can use following code to separate the array and save it into 2 lists called as entry
and exit
import java.util.*;
public class ArraySeparation {
public static void main (String[] args)
{
Integer[] patient = {1,3,2,5,4,8,9,10};
int n = patient.length;
ArrayList<Integer> entry = new ArrayList<Integer>();
ArrayList<Integer> exit = new ArrayList<Integer>();
int i = 0;
while(i < n)
{
if(i != n)
entry.add(patient[i]);
i+=2;
}i = 1;
while(i < n)
{
if(i != n)
exit.add(patient[i]);
i+=2;
}
System.out.println("Entry List:");
for(int l:entry) System.out.print(l + " ");
System.out.println("\nExit List:");
for(int l:exit) System.out.print(l + " ");
}
}
/*
* Output:
* Entry List:
* 1 2 4 9
* Exit List:
* 3 5 8 10
*/
答案2
得分: -1
利用Python的zip和解压概念,我们可以轻松地进行分离。
例如:
a = [(1, 3), (2, 5), (4, 8), (9, 10), (5, 6), (4, 7), (7, 10)]
b = []
for start_d, end_d in a:
max_d = end_d - start_d
b.append(max_d)
print(f'停留时长 max_d:{max_d}')
b.sort()
print(b)
print(b[(b.__len__() - 1)])
输出为
停留时长 max_d:2
停留时长 max_d:3
停留时长 max_d:4
停留时长 max_d:1
停留时长 max_d:1
停留时长 max_d:3
停留时长 max_d:3
[1, 1, 2, 3, 3, 3, 4]
4// 最长停留天数
英文翻译
Using Python zip and Unzip concept we can separate it easily.
for example :
a= [(1,3),(2,5),(4,8),(9,10),(5,6),(4,7),(7,10)]
b=[]
for start_d, end_d in a:
max_d = end_d - start_d
b.append(max_d)
print(f'Duration of stays max_d: {max_d}')
b.sort()
print(b)
print(b[(b.__len__()-1)])
and output
Duration of stays max_d: 2
Duration of stays max_d: 3
Duration of stays max_d: 4
Duration of stays max_d: 1
Duration of stays max_d: 1
Duration of stays max_d: 3
Duration of stays max_d: 3
[1, 1, 2, 3, 3, 3, 4]
4// max-m day stay
专注分享java语言的经验与见解,让所有开发者获益!
评论