当前位置:网站首页>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();
}
}
边栏推荐
- MFC sends left click, double click, and right click messages to list controls
- "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
- Image fusion -- challenges, opportunities and Countermeasures
- Analysis of pointer and array written test questions
- Restore backup data on S3 compatible storage with br
- 【MySQL】锁
- Go learning notes (3) basic types and statements (2)
- Use dumping to back up tidb cluster data to S3 compatible storage
- Huawei cloud OBS file upload and download tool class
- Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
猜你喜欢

Use Alibaba icon in uniapp

【MySQL】数据库的存储过程与存储函数通关教程(完整版)
![[t31zl intelligent video application processor data]](/img/67/b77c1de990d9b8868f8df5e55b0227.png)
[t31zl intelligent video application processor data]

ESP series pin description diagram summary

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

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers

C language custom type: struct

Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur

C language - bit segment
![[research materials] 2022 China yuancosmos white paper - Download attached](/img/b4/422dff0510bbe67f3578202d6e80b7.jpg)
[research materials] 2022 China yuancosmos white paper - Download attached
随机推荐
【刷题】牛客网面试必刷TOP101
Wincc7.5 download and installation tutorial (win10 system)
Day29-t77 & t1726-2022-02-13-don't answer by yourself
Golang DNS write casually
Hcip day 16
使用 Dumpling 备份 TiDB 集群数据到兼容 S3 的存储
2.10transfrom attribute
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
ESP series pin description diagram summary
C language - bit segment
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
Leetcode question brushing record | 203_ Remove linked list elements
Migrate data from CSV files to tidb
Use br to back up tidb cluster data to S3 compatible storage
Analysis of Top1 accuracy and top5 accuracy examples
升级 TiDB Operator
All the ArrayList knowledge you want to know is here
Oracle time display adjustment
LDAP应用篇(4)Jenkins接入