나는 루프 운동으로 조금 길을 잃었습니다. 다음 조건을 따르는 범위의 숫자를 인쇄해야합니다.
숫자의 각 숫자는 왼쪽 숫자의 합과 같아야합니다 . 예를 들면 :
112가 인쇄됩니다. 모든 구성 요소가 왼쪽 숫자의 합이기 때문입니다.
0은 왼쪽 숫자의 합이 아니기 때문에 101은 인쇄되지 않습니다.
합계가 9보다 크면 10은 버려집니다 .
이 코드가 있습니다.
public class Program {
/**
* Prints the list of numbers from desde to hasta such that the righmost
* digit of the sum of previous digits equals current digit, and that
* happens for all digits
*
* Prints a 2nd line with as many underscores as characters contains the 1st
* line
*
* If desde < 10 o the parameters make no sense, prints Err __
*
*
* @param desde
* int from (included)
* @param hasta
* int to (included)
* @return void
*/
public static void sumDigitsConsecutius(int desde, int hasta) {
int suma = 0;
int numeroSuma = 0;
for (int i = desde; i <= hasta; i++) {
int longitud = lenght(i);
int posicion = longitud - 1;
int contador = 0;
for (double numComparar = posicioNumber(i, posicion); posicion > 0; posicion--) {
numeroSuma = posicioNumber(i, lenght(i));
suma = suma + numeroSuma;
if (suma != numComparar) {
break;
} else {
numeroSuma = posicioNumber(i, (lenght(i) - 1));
contador++;
}
}
if (contador == longitud - 1) {
System.out.println(i);
}
}
}
/**
* With this method i can calculate the length of a number
*/
public static int lenght(int numero) {
int contador = 0;
while (numero > 0) {
contador++;
numero = numero / 10;
}
return contador;
}
public static int posicioNumber(int numero, int posicio) {
// Digit of n in position p
int x;
// Remove right digits from position p.
x = numero / (int) Math.pow(10, posicio - 1);
// Get the last digit.
x = x % 10;
return x;
}
public static void main(String[] args) {
sumDigitsConsecutius(80, 120);
}
}
두 가지 추가 방법을 만들었습니다. 첫 번째 방법은 숫자의 길이를 계산하고 두 번째 방법은 위치에 따라 숫자의 값을 계산합니다.
어떤 아이디어?
나는 당신의 코드를 살펴보고 논리의 일부 문제를 식별하고 다음 코드를 통해 수정하고 체인저를 이해할 수 있는지 확인했습니다. (당신의 변수가 영어가 아니기 때문에 당신의 논리를 이해하기가 상당히 어려웠 기 때문에 명심하십시오). 아래 코드는 필요한 결과를 제공합니다.
public class Program {
/**
* Prints the list of numbers from desde to hasta such that the righmost
* digit of the sum of previous digits equals current digit, and that
* happens for all digits
*
* Prints a 2nd line with as many underscores as characters contains the 1st
* line
*
* If desde < 10 o the parameters make no sense, prints Err __
*
*
* @param desde
* int from (included)
* @param hasta
* int to (included)
* @return void
*/
public static void sumDigitsConsecutius(int desde, int hasta) {
int suma = 0;
int numeroSuma = 0;
for (int i = desde; i <= hasta; i++) {
int longitud = lenght(i);
int contador = 0;
suma = 0;
for (int posicion = longitud; posicion > 0; posicion--) {
numeroSuma = posicioNumber(i, posicion);
if (posicion !=longitud && suma != numeroSuma) {
break;
} else {
contador++;
}
suma = suma + numeroSuma;
//If the sum greater than 9 then position discarded
if(suma>=10){
suma=suma%10;
}
}
if (contador == longitud ) {
System.out.println(i);
}
}
}
/**
* With this method i can calculate the length of a number
*/
public static int lenght(int numero) {
int contador = 0;
while (numero > 0) {
contador++;
numero = numero / 10;
}
return contador;
}
public static int posicioNumber(int numero, int posicio) {
// Digit of n in position p
int x;
// Remove right digits from position p.
x = numero / (int) Math.pow(10, posicio - 1);
// Get the last digit.
x = x % 10;
return x;
}
public static void main(String[] args) {
sumDigitsConsecutius(1, 1000);
}
}
위 클래스의 경우 아래 출력을 제공합니다 (숫자 범위 1-1000).
1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
112
224
336
448
550
662
774
886
998
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다