while True: m,n=map(int,input().split()) a=list(map(int,input().split())) count=0 a.sort() for i in range(n): if a[i]<=m: count+=1 m-=a[i] print(count)转为java代码
时间: 2023-12-10 20:40:48 浏览: 63
JAVA M与N进制转换
3星 · 编辑精心推荐
```
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int m = sc.nextInt();
int n = sc.nextInt();
List<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(sc.nextInt());
}
int count = 0;
Collections.sort(a);
for (int i = 0; i < n; i++) {
if (a.get(i) <= m) {
count++;
m -= a.get(i);
}
}
System.out.println(count);
}
}
}
```
阅读全文