Java实现输入一个整数n返回一个长度为n的数组数组元素为自然数中11的倍数或者个位数是9
以下是Java代码实现:
public static int[] getArray(int n) {
int[] arr = new int[n];
int count = 0;
for (int i = 1; count < n; i++) {
if (i % 11 == 0 || i % 10 == 9) {
arr[count] = i;
count++;
}
}
return arr;
}
说明:
- 首先创建一个长度为n的数组arr。
- 使用循环遍历自然数,直到找到n个符合要求的数为止。
- 对于每一个自然数,判断其是否是11的倍数或者个位数为9,如果是,则将其添加到数组中。
- 最后返回数组arr。
原文地址: http://www.cveoy.top/t/topic/c3sz 著作权归作者所有。请勿转载和采集!