当前位置:网站首页>2022.02.13 - NC004. Print number of loops
2022.02.13 - NC004. Print number of loops
2022-07-06 08:21:00 【A CAI continues to work hard】
List of articles
1. subject
2. Ideas
(1) Simulation
- Simulate the process of traversing a two-dimensional array clockwise .
- Be careful ! The question is ACM Pattern , You need to receive the input parameters from the console .
3. Code
import java.util.Scanner;
public class Main {
public static int width;
public static int height;
public static int[][] array;
public static int left;
public static int right;
public static int up;
public static int down;
public static int curNum;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
width = scanner.nextInt();
height = scanner.nextInt();
array = new int[height][width];
left = -1;
right = width;
up = -1;
down = height;
curNum = 0;
moveRight();
for (int[] ints : array) {
for (int anInt : ints) {
System.out.print(anInt);
}
System.out.println();
}
}
public static void moveRight() {
if (left + 1 == right) {
return;
}
for (int i = left + 1; i < right; i++) {
array[up + 1][i] = curNum;
}
curNum++;
up++;
moveDown();
}
public static void moveDown() {
if (up + 1 == down) {
return;
}
for (int i = up + 1; i < down; i++) {
array[i][right - 1] = curNum;
}
curNum++;
right--;
moveLeft();
}
public static void moveLeft() {
if (right - 1 == left) {
return;
}
for (int i = right - 1; i > left; i--) {
array[down - 1][i] = curNum;
}
curNum++;
down--;
moveUp();
}
public static void moveUp() {
if (down - 1 == up) {
return;
}
for (int i = down - 1; i > up; i--) {
array[i][left + 1] = curNum;
}
curNum++;
left++;
moveRight();
}
}
边栏推荐
- C语言 - 位段
- Hcip day 16
- Fibonacci sequence
- Flash return file download
- LDAP application (4) Jenkins access
- Day29-t77 & t1726-2022-02-13-don't answer by yourself
- C language custom type: struct
- sys.argv
- "Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
- "Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
猜你喜欢
好用的TCP-UDP_debug工具下载和使用
hcip--mpls
[research materials] 2021 China online high growth white paper - Download attached
【MySQL】锁
Golang DNS 随便写写
Online yaml to CSV tool
ESP系列引脚說明圖匯總
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
Learn Arduino with examples
[research materials] 2021 Research Report on China's smart medical industry - Download attached
随机推荐
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Configuring OSPF load sharing for Huawei devices
升级 TiDB Operator
让学指针变得更简单(三)
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
Sanzi chess (C language)
Golang DNS write casually
ESP系列引脚说明图汇总
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
MFC 给列表控件发送左键单击、双击、以及右键单击消息
Mobile Test Engineer occupation yyds dry goods inventory
Make learning pointer easier (3)
从 CSV 文件迁移数据到 TiDB
Fibonacci sequence
LDAP应用篇(4)Jenkins接入
Secure captcha (unsafe verification code) of DVWA range
Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
JS select all and tab bar switching, simple comments