gpt4 book ai didi

java - 1 到 1000 之间的数字频率或 9 的数量 - Java

转载 作者:行者123 更新时间:2023-11-29 10:20:18 25 4
gpt4 key购买 nike

我在工作中遇到了这个小问题,想看看我能不能写一个小java程序来解决它。显然我不能用 java 写该死的东西。大声笑我被卡住了,但已经解决了一部分。我花了两天的大部分时间试图让它工作但没有运气。这是我的问题。我有一个 txt 文件,其中的数字按 1-1000 的顺序生成,每行一个数字。我想计算数字九在1和1000之间的次数或频率。我的做法如下:

  1. 从文本文件中读取数字并将它们保存在一个 int 数组中。
  2. 将数组中的工作数或spot[0]中的数转换为字符串选择一条路线。

3-A。与 charAt(0) 比较为 9 并在适当时递增

3-B。将字符串转换为 char 数组,并将 char 数组中的索引 0 与 9 进行比较,并在适当时递增。

旁注:如果比较 charArray[1] == '9' 则必须注释掉 else 才能编译。如果您将其注释掉,它将非常适合计算第一个数字位置的 9。第二位和第三位数字比较不起作用。感谢和抱歉这个愚蠢的问题。

生成文本文件的一种简单方法是在第 1 列中使用 excel 输入,然后在第二行中输入 2,然后在第三行(同一单元格)中输入 3,然后突出显示所有三个数字并将加号拖到包含三个单元格的右下角。这将填充到 1000。我没有附加我的 txt 文件,因为您可以轻松生成自己的文件。

**不是作业

我的代码如下:

 package com.numbers;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Numbers
{

static int[] array = new int[1002];
static char[] charArray = new char[4];

public static void main(String[] args) throws FileNotFoundException
{

Scanner scan = new Scanner (System.in);
System.out.print("Enter the name of the file : ");
String whatfile = scan.nextLine();
Scanner readnumber = new Scanner (new FileReader(whatfile));

int firstDigit = 0;
int secondDigit = 0;
int thirdDigit = 0;

for (int i = 0; i < 1000; i++)
{
array[i] = readnumber.nextInt();
String testString = "";

// System.out.println("the number being passed to parse is : " + numberAtIndex);


// Approach 1
testString = Integer.toString(array[i]);

if(testString.charAt(0) == '9')
{
firstDigit = firstDigit + 1;
}

// System.out.println("test string contains : " + testString);

// Approach 2
charArray = testString.toCharArray();

if(charArray[0] == '9')
{
firstDigit = firstDigit + 1;

}

/*
* If you comment out this 'else if' it will work. please comment out one of the approaches above (use only 1) My problem is that
* I can't seem to figure out how to search for nines in the second & third digit location.
* If it is in an array shouldn't I be able to compare the charArray[1] to a nine and have it increment if true?
*/

else if (charArray[1] == '9')
{
secondDigit = secondDigit + 1;
}


} // end for

// System.out.println("this is what is stored in array spot 0 : " + array[0]); //should be 1

System.out.println("the number of 9's in the first digit place holder is : " + firstDigit);
System.out.println("the number of 9's in the second digit place holder is : " + secondDigit);
// System.out.println("the number of 9's in the third digit place holder is : " + thirdDigit);

} // End of main

} // end of class

最佳答案

所以我的第一个想法不是“遍历所有数字”,而是想出一种公式化的方法来确定计数...请注意:

  • 每 10 个数字中有 1 个包含 9(“9”)
  • 每 100 个数字中有 10 个包含额外的 9(“90”、“91”、...)
  • 每 1000 个数字中有 100 个包含额外的 9(“900”、“901”、...)

1000 中 9 的个数

= (1000/10)*1 + (1000/100)*10 + (1000/1000)*100 = 100*1 + 10*10 + 100*1 = 300

值得注意的是,这也给出了每个数字的计数,第一个规则给出了 1 位的位数,第二个规则给出了 10 位的位数,第三个规则给出了100 位的位数。

假设您对此表示怀疑,这里有一个更简洁的 Java 程序,它通过迭代计算 9:

int nines = 0;
for(int i = 1; i <= 1000; i++){
for(char c : String.valueOf(i).toCharArray()){
if(c == '9') nines++;
}
}

关于java - 1 到 1000 之间的数字频率或 9 的数量 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7465149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com