java - 如何为给定的整数数组列表值执行逆金字塔?在安卓中
问题描述
4 5 7 6 3
9 3 4 9
3 7 4
1 2
3
4+5=9
5+7=12 -->1+2=3
同样,如何获得最终结果 3 之类的n
数字?
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
for(int j=i; j <= 2 * i - 1; ++j) {
System.out.print("* ");
//System.out.print(a[j]);
}
for(int j = 0; j < i - 1; ++j) {
System.out.print("* ");
}
}
}
解决方案
代码,前提是数字在数组中,因为我在您提供的数字中找不到模式。
public static void main(String[] args) {
int numList[] = {4, 5, 7, 6, 3, 9, 3, 4, 9, 3, 7, 4, 1, 2, 3};
int i,j=0,k=1; //k is for the spaces.
for (int line = 4; line >= 0; line--) {
// To display spaces
for(int space=0; space<k; space++) {
System.out.print(" ");
}
// To display the numbers.
for (i = j; i <= (j + line); i++) {
// The below if statement is for symmetry, you may ignore it.
if(k>=1){
System.out.print(" ");
}
System.out.print(" "+numList[i]+" ");
}
j = i;
//The below code is just to make the pyramid symmetrical you
//may ignore it.
if(k==0){
k++;
}
else{
k+=2;
}
System.out.println();
}
}
输出
推荐阅读
- c# - 是否可以获取 C# 程序集二进制文件
- anylogic - 代理继承和人口分组
- delphi - 使用回调访问 voilation delphi x64
- java - 如何显示 MS Graph(搜索结果)driveItem 文件的路径(文件夹)?
- rspec - 用于编辑的 Rails Rspec 测试用例
- php - Codeigniter库文件上传图片文件上传表单不起作用,url变成重复
- android - 如何从回收站视图中制作选择器?
- proxy - 查尔斯代理互联网连接仍然加密
- docker - 如何在 docker ENTRYPOINT 中使用撰写环境变量
- python - 使用 os.system() 调用 .cmd 脚本不起作用,而同一命令在 cmd 终端中有效