[英]How to print an integer with commas every 'd' digits, from right to left
我必須編寫一個程序,該程序將接收一個 int 'n' 和另一個 'd' - 並將從右到左每 d 位用逗號打印數字 n。 如果 'n' 或 'd' 為負數 - 程序將按原樣打印 'n'。 我雖然必須確保數字前后沒有逗號,但我不允許使用字符串或數組。
例如:n = 12345678
d=1: 1,2,3,4,5,6,7,8
d=3:12,345,678
我編寫了以下代碼:
public static void printWithComma(int n, int d) {
if (n < 0 || d <= 0) {
System.out.println(n);
} else {
int reversedN = reverseNum(n), copyOfrereversedN = reversedN, counter = numberLength(n);
while (reversedN > 0) {
System.out.print(reversedN % 10);
reversedN /= 10;
counter--;
if (counter % d == 0 && reversedN != 0) {
System.out.print(",");
}
}
/*
* In a case which the received number will end with zeros, the reverse method
* will return the number without them. In that case the length of the reversed
* number and the length of the original number will be different - so this
* while loop will end the zero'z at the right place with the commas at the
* right place
*/
while (numberLength(copyOfrereversedN) != numberLength(n)) {
if (counter % d == 0) {
System.out.print(",");
}
System.out.print(0);
counter--;
copyOfrereversedN *= 10;
}
}
}
使用 reversNum 函數:
// The method receives a number n and return his reversed number(if the number
// ends with zero's - the method will return the number without them)
public static int reverseNum(int n) {
if (n < 9) {
return n;
}
int reversedNum = 0;
while (n > 0) {
reversedNum += (n % 10);
reversedNum *= 10;
n /= 10;
}
return (reversedNum / 10);
}
和 numberLength 方法:
// The method receives a number and return his length ( 0 is considered as "0"
// length)
public static int numberLength(int n) {
int counter = 0;
while (n > 0) {
n /= 10;
counter++;
}
return counter;
}
有人告訴我該代碼不適用於所有情況,我無法考慮這種情況(告訴我的人不會告訴我)。
感謝您的閱讀!
您的代碼似乎過於復雜。
如果你已經了解了遞歸,你可以這樣做:
public static void printWithComma(int n, int d) {
printInternal(n, d, 1);
System.out.println();
}
private static void printInternal(int n, int d, int i) {
if (n > 9) {
printInternal(n / 10, d, i + 1);
if (i % d == 0)
System.out.print(',');
}
System.out.print(n % 10);
}
沒有遞歸:
public static void printWithComma(int n, int d) {
int rev = 0, i = d - 1;
for (int num = n; num > 0 ; num /= 10, i++)
rev = rev * 10 + num % 10;
for (; i > d; rev /= 10, i--) {
System.out.print(rev % 10);
if (i % d == 0)
System.out.print(',');
}
System.out.println(rev);
}
您通過反轉數字解決了循環數字,因此可以進行簡單的除以十來按順序接收所有數字。
逗號位置從右邊開始計算。
public static void printWithComma(int n, int d) {
if (n < 0) {
System.out.print('-');
n = -n;
}
if (n == 0) {
System.out.print('0');
return;
}
int length = numberLength(n);
int reversed = reverseNum(n);
for (int i = 0; i < length; ++i) {
int nextDigit = reversed % 10;
System.out.print(nextDigit);
reversed /= 10;
int fromRight = length - 1 - i;
if (fromRight != 0 && fromRight % d == 0) {
System.out.print(',');
}
}
}
這與您的代碼基本相同。 但是我將幫助函數的結果存儲到變量中。
零是一種特殊情況,排除前導零的規則。
每個第d 位(從右起)都需要打印逗號,但不能完全在右邊。 而且不在前面。 通過先打印數字然后可能是逗號來實現。
我在您的代碼中看到的問題是兩個 while 循環,兩次打印逗號,也許? 當 <= 0 時帶有換行符的 println。
測試您的代碼,例如:
public static void main(String[] args) {
for (int n : new int[] {0, 1, 8, 9, 10, 234,
1_234, 12_345, 123_456, 123_456_789, 1_234_567_890}) {
System.out.printf("%d : ", n);
printWithComma(n, 3);
System.out.println();
}
}
您是否可以使用整個 Java API?
像使用DecimalFormat
這樣簡單的事情怎么樣?
double in = 12345678;
DecimalFormat df = new DecimalFormat( ",##" );
System.out.println(df.format(in));
12、34、56、78
使用...
,#
= 每組 1 個,##
= 每組 2 個,###
= 每組 3 個等...我花了幾分鍾。 以下代碼片段可以很好地完成工作(下面的解釋):
public static void printWithComma(int n, int d) { // n=number, d=commaIndex
final int length = (int) (Math.log10(n) + 1); // number of digits;
for (int i = 1; i < Math.pow(10, length); i*=10) { // loop by digits
double current = Math.log10(i); // current loop
double remains = length - current - 1; // loops remaining
int digit = (int) ((n / Math.pow(10, remains)) % 10); // nth digit
System.out.print(digit); // print it
if (remains % d == 0 && remains > 0) { // add comma if qualified
System.out.print(",");
}
}
}
(Math.log10(n) + 1)
我找到整數中的一些數字( 8
表示12345678
)。for-loop
確保了進一步計算所需的n
系列 (1, 10, 100, 1000...) 的指數。 使用以 10 為底的對數,我得到循環的當前索引。,
) 找到一個合格的位置。 如果當前循環索引的模數為零,則達到第d個索引,可以打印出逗號。 最后,條件remains > 0
確保在打印結果的末尾不會留下逗號。輸出:
4 : 1234,5678
對於3 : 12,345,678
對於2 : 12,34,56,78
對於1 : 1,2,3,4,5,6,7,8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.