当前位置:网站首页>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();
}
}
边栏推荐
- What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
- 1202 character lookup
- Use br to back up tidb cluster data to S3 compatible storage
- 08- [istio] istio gateway, virtual service and the relationship between them
- Secure captcha (unsafe verification code) of DVWA range
- Migrate data from CSV files to tidb
- CAD ARX 获取当前的视口设置
- 使用 BR 恢复 S3 兼容存储上的备份数据
- 使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
- NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
猜你喜欢
Step by step guide to setting NFT as an ens profile Avatar
Yyds dry goods inventory three JS source code interpretation eventdispatcher
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
IoT -- 解读物联网四层架构
It's hard to find a job when the industry is in recession
C language custom type: struct
[research materials] 2021 China online high growth white paper - Download attached
08- [istio] istio gateway, virtual service and the relationship between them
将 NFT 设置为 ENS 个人资料头像的分步指南
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
随机推荐
3. File operation 3-with
[untitled]
Go learning notes (3) basic types and statements (2)
【云原生】手把手教你搭建ferry开源工单系统
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
Résumé des diagrammes de description des broches de la série ESP
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
Golang DNS 随便写写
1202 character lookup
Use Alibaba icon in uniapp
2. File operation - write
Erc20 token agreement
Leetcode question brushing (5.28) hash table
Flash return file download
JS select all and tab bar switching, simple comments
Ruffian Heng embedded bimonthly, issue 49
Analysis of pointer and array written test questions
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
08- [istio] istio gateway, virtual service and the relationship between them
IP lab, the first weekly recheck