英文:
Can this code be further reduce , maybe without variable "k" (in this example)
问题
import java.util.Scanner;
public class PracticePattern6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < 2 * n + 1; i++) {
if (i < n + 1) {
for (int j = i; j < n; j++) {
System.out.print("* ");
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print(" ");
}
for (int j = i; j < n; j++) {
System.out.print("* ");
}
} else {
for (int j = 0; j < i - n; j++) {
System.out.print("* ");
}
for (int j = 0; j < 2 * n - k; j++) {
System.out.print(" ");
}
for (int j = 0; j < i - n; j++) {
System.out.print("* ");
}
k += 2;
}
System.out.println();
}
sc.close();
}
}
英文:
JAVA
In this code, I have to use 7 for
loops and assign the variable k
. Is there any way to reduce the time complexity of this code, maybe without variable "k"
Here is my code:
import java.util.Scanner;
public class PracticePattern6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k=1;
for(int i=0;i<2*n+1;i++)
{
if(i<n+1)
{
for(int j = i ;j<n;j++)
{
System.out.print("* ");
}
for(int j =0;j<2*i+1;j++)
{
System.out.print(" ");
}
for(int j = i ;j<n;j++)
{
System.out.print("* ");
}
}
else
{
for(int j=0;j<i-n;j++)
{
System.out.print("* ");
}
for(int j=0;j<2*n-k;j++)
{
System.out.print(" ");
}
for(int j=0;j<i-n;j++)
{
System.out.print("* ");
}
k+=2;
}
System.out.println();
}
sc.close();
}
}
答案1
得分: 1
我认为这样会更简单一些,不过我没有逐行对照检查以确保它们的输出完全相同,如果输出不同,输出结果也非常相似,稍微调整一下,你就可以使这段代码得到正确的输出。
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print("* ");
}
for (int j = 0; j < i * 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < n - i; j++) {
System.out.print(" *");
}
System.out.println();
}
for (int i = n; i >= 0; i--) {
for (int j = n; j > i; j--) {
System.out.print("* ");
}
for (int j = n; j > n - i * 2; j--) {
System.out.print(" ");
}
for (int j = n; j > i; j--) {
System.out.print(" *");
}
System.out.println();
}
}
英文:
I believe this is a little simpler, however I did not do a side by side check to make sure they have the exact same output, if they do not the outputs are very similar and with a little tweaking you could make this code work for the exact correct output I believe.
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0;i<n;i++) {
for(int j = 0;j<n-i;j++) {
System.out.print("* ");
}
for(int j = 0;j<i*2;j++) {
System.out.print(" ");
}
for(int j = 0;j<n-i;j++) {
System.out.print(" *");
}
System.out.println();
}
for(int i = n;i>=0;i--) {
for(int j = n;j>i;j--) {
System.out.print("* ");
}
for(int j = n;j>n-i*2;j--) {
System.out.print(" ");
}
for(int j = n;j>i;j--) {
System.out.print(" *");
}
System.out.println();
}
答案2
得分: 1
如果允许使用字符串/字符的中间数组在打印之前构建整个图案,可以按照以下方式完成:
- 创建整个场地并用左半部分和右半部分所需的图案填充它(使用标准的
Arrays.fill
方法)。 - 为场地的顶部和底部的半部分设置“空”单元格,计算空白空间的
左
和右
位置。 - 打印场地。
示例:
public static void printRhombPattern(int size, String leftPattern, String rightPattern, String empty) {
int cols = 2 * size;
int rows = cols + 1;
String[][] field = new String[rows][cols];
for (int i = 0; i < rows; i++) {
field[i] = new String[cols];
Arrays.fill(field[i], 0, size, leftPattern);
Arrays.fill(field[i], size, cols, rightPattern);
}
int left = cols / 2 - 1;
// 从 i == 1 开始,跳过第一行和最后一行
for (int i = 1, right = left + 1; i <= size; i++, left--, right++) {
for (int j = left; j <= right; j++) {
field[i][j] = empty; // 清除顶半部分的单元格
field[rows - 1 - i][j] = empty; // 清除底半部分的单元格
}
}
// 主要打印
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(field[i][j]);
}
System.out.println();
}
}
// 测试
public static void main(String[] args) {
printRhombPattern(5, "* ", " *", " ");
}
输出:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
英文:
If it is allowed to use an intermediate array of Strings/chars to build the entire picture before printing it, it can be done in the following way:
- Create entire field and populate it with required patterns for the left and right halves (using standard
Arrays.fill
method). - Set "empty" cells for top and bottom halves of the field, calculating the
left
andright
position of the empty space. - Print the field.
Example:
public static void printRhombPattern(int size, String leftPattern, String rightPattern, String empty) {
int cols = 2 * size;
int rows = cols + 1;
String[][] field = new String[rows][cols];
for (int i = 0; i < rows; i++) {
field[i] = new String[cols];
Arrays.fill(field[i], 0, size, leftPattern);
Arrays.fill(field[i], size, cols, rightPattern);
}
int left = cols / 2 - 1;
// starting from i == 1 to skip the first and the last rows
for (int i = 1, right = left + 1; i <= size; i++, left--, right++) {
for (int j = left; j <= right; j++) {
field[i][j] = empty; // clean cells in the top half
field[rows - 1 - i][j] = empty; // clean cells in the bottom half
}
}
// main printing
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(field[i][j]);
}
System.out.println();
}
}
// test
public static void main(String[] args) {
printRhombPattern(5, "* ", " *", " ");
}
Output:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
答案3
得分: 0
短回答:不。
如果您谈论时间复杂度,特定代码用于打印菱形空格的大O符号。
当前解决方案的时间复杂度为O(N^2),这是打印二维形状的最小复杂度。
此外,您可以减少一些常数,这不会影响您的时间复杂度。
如果您刚开始编程,以下资源可能会对您有所帮助:
https://github.com/joney000/Java-Competitive-Programming
英文:
Short Answer: No.
If you talk about the time complexity, the big-O of the specified code for printing the diamond whitespace.
https://ideone.com/foelhX
The current solution is O(N^2) which is minimal to print 2-D shape.
moreover, you can reduce some constants, that would not impact your time complexity.
<br>
In case you are starting programming, this would be a helpful resource:
https://github.com/joney000/Java-Competitive-Programming
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论