将一行数字居中放在主线下方
Centering a line of numbers below a main line
我有一组数字 1-9。我有一组可变的数字(例如 1-3、1-4 或 1-7 等)。我需要将第二组数字(变量)居中到第一组。如果第二组数字是偶数,则将第二组数字移近一点。
示例:
123456789
000123000
在上面的示例中,4 与 1 相关,5 与 2 相关,6 与 3 相关。
或
123456789
001234000
或
123456789
012345670
我很难分解问题,以便将第二行文本从第一行居中。 “0”并不重要,添加到示例中以显示 space 差异。我认为这是基础数学,但我遗漏了一些东西。感谢您的帮助!
编辑 #1:
package com.company;
public class Main {
private static final int maxNumberOfItems = 9;
public static void main(String[] args) {
int numberOfItems = 6; // Any number between 1 and 9.
int difference = maxNumberOfItems - numberOfItems;
int dividedDifference;
boolean isEven = (numberOfItems % 2) == 0;
if (isEven) {
dividedDifference = (difference - 1) / 2;
} else {
dividedDifference = difference / 2;
}
printAnswer(numberOfItems, dividedDifference, isEven);
}
private static void printAnswer(int numberOfItems, int dividedDifference, boolean isEven) {
//TODO: Print answer here to console.
// Desired output:
// 123456789 (represents maxNumberOfItems)
// --1234--- (represents numberOfItems)
// Pseudo logic.
// Figure out if the var numberOfItems is odd or even.
// If its odd, subtract the numberOfItems from the maxNumberOfItems.
// Take that number and divide by two. That gives the amount of spaces to skip on each side.
// If the number is even, subtract one and divide by two. Get the number of spaces on each side
// and add one back to the right side.
}
}
编辑#2:
@Mbo 要求我详细说明和解释 "real problem." 所以就这样吧。在 3D 世界中,我有一组基座,它们会根据要显示的项目数量生成四分之一圆。基座位置位于固定的 XYZ 坐标。这些坐标永远不会改变。位置如下图所示。最多只能有 9 个基座。我们要显示的基座基于我们要显示的 "items"(我们的变化变量)的数量。理想情况下,我们总是希望在中间显示它们。如果数字是偶数,那么我们想在中间显示基座,但比 9 更接近 1。此编号可以显示在块引号中的问题顶部。
123456789
001234000
示例图片:
Representation of the quarter circle and pedestal locations.
最好有一个 Map 或 HashMap 来保存关键项目编号和值基座位置。只要显示一个基座,就会始终使用位置 5。
所以给定一张地图,它可能看起来像这样。
示例 1:
4 项。显示 9 个可能的基座。
Item 1 (key) to pedestal 3 location (value).
Item 2 (key) to pedestal 4 location (value).
Item 3 (key) to pedestal 5 location (value).
Item 4 (key) to pedestal 6 location (value).
注意上面的例子4并没有平均分成9,所以居中偏移,位置更接近1
示例 2:
3 项。显示 9 个可能的基座。
Item 1 (key) to pedestal 4 location (value).
Item 2 (key) to pedestal 5 location (value).
Item 3 (key) to pedestal 6 location (value).
在这个例子中,3 很好地分成 9,所以它可以完美居中。
这才是真正的问题。
好像你已经完成了工作,不清楚 - 怎么了?
diff = maxNumberOfItems - numberOfItems;
//integer division gives desired result both for even and for odd diff
shift = (maxNumberOfItems - numberOfItems) / 2;
//mapping
location = key + shift;
Number of items shift mapping
9 0 (1->1, 9->9)
8 0 (1->1, 8->8)
7 1 (1->2, 7->8)
6 1 (1->2, 6->7)
5 2 (1->3, 5->7)
4 2 (1->3, 4->6)
3 3 (1->4, 3->6)
2 3 (1->4, 2->5)
1 4 (1->5)
请注意,您可以在数组移位中对这些值进行硬编码[]
您可以简化代码以确定 dividedDifference
的值。
dividedDifference
表示第二行数字右边的空格个数。无论 numberOfItems
是偶数还是奇数都是如此,因此 isEven
在您的 printAnswer
方法中不是必需的。
您在调用 printAnswer
之前已经执行了大部分必要的逻辑,因此该方法中的大部分伪代码都是多余的。
您可以像这样将输出打印到控制台(isEven
参数已被删除,因此您也需要在调用该方法的地方删除该参数):
private static void printAnswer(int numberOfItems, int dividedDifference) {
for(int i = 1; i <= maxNumberOfItems; i++)
{
System.out.print(i);
}
System.out.println();
System.out.print(StringUtils.repeat(" ", dividedDifference));
for(int i = 1; i <= numberOfItems; i++)
{
System.out.print(i);
}
System.out.print(StringUtils.repeat(" ", maxNumberOfItems - numberOfItems - dividedDifference));
}
可以找到包含 StringUtils
的存储库 here。
实现算法的方法之一是:
public class Main {
private static final int maxNumberOfItems = 9;
public static void main(String[] args) {
int numberOfItems = 1531; // Any number between 1 and 9.
int strLength = Integer.toString(numberOfItems).length(); // the length of the string presentation
printAnswer(numberOfItems, strLength, maxNumberOfItems);
}
private static void printAnswer(int number, int start, int max) {
String str = Integer.toString(number);
for (int i = start; i < max; i++) {
if (i % 2 == 0) { // in this conditions we define from
str += "0"; // which side we need to append "0"
} else {
str = "0" + str;
}
}
System.out.println(str);
}
}
此实现的主要思想是从两侧向字符串追加所需数量的“0”以对现有数字进行解释。
输出:
001531000
我有一组数字 1-9。我有一组可变的数字(例如 1-3、1-4 或 1-7 等)。我需要将第二组数字(变量)居中到第一组。如果第二组数字是偶数,则将第二组数字移近一点。
示例:
123456789
000123000
在上面的示例中,4 与 1 相关,5 与 2 相关,6 与 3 相关。
或
123456789
001234000
或
123456789
012345670
我很难分解问题,以便将第二行文本从第一行居中。 “0”并不重要,添加到示例中以显示 space 差异。我认为这是基础数学,但我遗漏了一些东西。感谢您的帮助!
编辑 #1:
package com.company;
public class Main {
private static final int maxNumberOfItems = 9;
public static void main(String[] args) {
int numberOfItems = 6; // Any number between 1 and 9.
int difference = maxNumberOfItems - numberOfItems;
int dividedDifference;
boolean isEven = (numberOfItems % 2) == 0;
if (isEven) {
dividedDifference = (difference - 1) / 2;
} else {
dividedDifference = difference / 2;
}
printAnswer(numberOfItems, dividedDifference, isEven);
}
private static void printAnswer(int numberOfItems, int dividedDifference, boolean isEven) {
//TODO: Print answer here to console.
// Desired output:
// 123456789 (represents maxNumberOfItems)
// --1234--- (represents numberOfItems)
// Pseudo logic.
// Figure out if the var numberOfItems is odd or even.
// If its odd, subtract the numberOfItems from the maxNumberOfItems.
// Take that number and divide by two. That gives the amount of spaces to skip on each side.
// If the number is even, subtract one and divide by two. Get the number of spaces on each side
// and add one back to the right side.
}
}
编辑#2:
@Mbo 要求我详细说明和解释 "real problem." 所以就这样吧。在 3D 世界中,我有一组基座,它们会根据要显示的项目数量生成四分之一圆。基座位置位于固定的 XYZ 坐标。这些坐标永远不会改变。位置如下图所示。最多只能有 9 个基座。我们要显示的基座基于我们要显示的 "items"(我们的变化变量)的数量。理想情况下,我们总是希望在中间显示它们。如果数字是偶数,那么我们想在中间显示基座,但比 9 更接近 1。此编号可以显示在块引号中的问题顶部。
123456789
001234000
示例图片: Representation of the quarter circle and pedestal locations.
最好有一个 Map 或 HashMap 来保存关键项目编号和值基座位置。只要显示一个基座,就会始终使用位置 5。
所以给定一张地图,它可能看起来像这样。
示例 1:
4 项。显示 9 个可能的基座。
Item 1 (key) to pedestal 3 location (value).
Item 2 (key) to pedestal 4 location (value).
Item 3 (key) to pedestal 5 location (value).
Item 4 (key) to pedestal 6 location (value).
注意上面的例子4并没有平均分成9,所以居中偏移,位置更接近1
示例 2:
3 项。显示 9 个可能的基座。
Item 1 (key) to pedestal 4 location (value).
Item 2 (key) to pedestal 5 location (value).
Item 3 (key) to pedestal 6 location (value).
在这个例子中,3 很好地分成 9,所以它可以完美居中。
这才是真正的问题。
好像你已经完成了工作,不清楚 - 怎么了?
diff = maxNumberOfItems - numberOfItems;
//integer division gives desired result both for even and for odd diff
shift = (maxNumberOfItems - numberOfItems) / 2;
//mapping
location = key + shift;
Number of items shift mapping
9 0 (1->1, 9->9)
8 0 (1->1, 8->8)
7 1 (1->2, 7->8)
6 1 (1->2, 6->7)
5 2 (1->3, 5->7)
4 2 (1->3, 4->6)
3 3 (1->4, 3->6)
2 3 (1->4, 2->5)
1 4 (1->5)
请注意,您可以在数组移位中对这些值进行硬编码[]
您可以简化代码以确定 dividedDifference
的值。
dividedDifference
表示第二行数字右边的空格个数。无论 numberOfItems
是偶数还是奇数都是如此,因此 isEven
在您的 printAnswer
方法中不是必需的。
您在调用 printAnswer
之前已经执行了大部分必要的逻辑,因此该方法中的大部分伪代码都是多余的。
您可以像这样将输出打印到控制台(isEven
参数已被删除,因此您也需要在调用该方法的地方删除该参数):
private static void printAnswer(int numberOfItems, int dividedDifference) {
for(int i = 1; i <= maxNumberOfItems; i++)
{
System.out.print(i);
}
System.out.println();
System.out.print(StringUtils.repeat(" ", dividedDifference));
for(int i = 1; i <= numberOfItems; i++)
{
System.out.print(i);
}
System.out.print(StringUtils.repeat(" ", maxNumberOfItems - numberOfItems - dividedDifference));
}
可以找到包含 StringUtils
的存储库 here。
实现算法的方法之一是:
public class Main {
private static final int maxNumberOfItems = 9;
public static void main(String[] args) {
int numberOfItems = 1531; // Any number between 1 and 9.
int strLength = Integer.toString(numberOfItems).length(); // the length of the string presentation
printAnswer(numberOfItems, strLength, maxNumberOfItems);
}
private static void printAnswer(int number, int start, int max) {
String str = Integer.toString(number);
for (int i = start; i < max; i++) {
if (i % 2 == 0) { // in this conditions we define from
str += "0"; // which side we need to append "0"
} else {
str = "0" + str;
}
}
System.out.println(str);
}
}
此实现的主要思想是从两侧向字符串追加所需数量的“0”以对现有数字进行解释。
输出:
001531000