当前位置:网站首页>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 language - bit segment
- The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
- All the ArrayList knowledge you want to know is here
- Learn Arduino with examples
- Fibonacci sequence
- Colorlog结合logging打印有颜色的日志
- IP lab, the first weekly recheck
- 指针和数组笔试题解析
- 3. File operation 3-with
- Migrate data from a tidb cluster to another tidb cluster
猜你喜欢

Uibehavior, a comprehensive exploration of ugui source code

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

【MySQL】锁

Use Alibaba icon in uniapp

"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
![[research materials] 2021 Research Report on China's smart medical industry - Download attached](/img/c8/a205ddc2835c87efa38808cf31f59e.jpg)
[research materials] 2021 Research Report on China's smart medical industry - Download attached

How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship

ESP系列引脚說明圖匯總

指针和数组笔试题解析

Golang DNS 随便写写
随机推荐
Use Alibaba icon in uniapp
Migrate data from SQL files to tidb
Circuit breaker: use of hystrix
远程存储访问授权
logback1.3. X configuration details and Practice
Upgrade tidb operator
从 SQL 文件迁移数据到 TiDB
Vocabulary notes for postgraduate entrance examination (3)
C语言 - 位段
File upload of DVWA range
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
[2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
It's hard to find a job when the industry is in recession
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
【刷题】牛客网面试必刷TOP101
Step by step guide to setting NFT as an ens profile Avatar
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Pyqt5 development tips - obtain Manhattan distance between coordinates
CISP-PTE实操练习讲解