英文:
Cant find the fix for my program at LeetCode Challenge of PrimeNumbers
问题
我已经尝试了几种方法来更改我的 LeetCode 代码,但是我找不到解决方法,挑战是这样的:
<<统计小于非负整数 n 的质数数量。
示例:
输入:10
输出:4
解释:小于 10 的质数有 4 个,它们是 2、3、5、7。>>
我的提案代码如下:
import java.util.Scanner;
class Solution {
public int countPrimes(int n) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int cont = 0;
int prime = 0;
prime = sc.nextInt();
int a[] = new int [prime];
for(int i = 0; i < a.length; i++) {
a[i] = i;
cont = 0;
for(int y = 1; y < a.length; y++) {
if(a[i] % y == 0) {
cont ++;
}
}
if (cont == 2) {
sum ++;
}
}
return sum;
}
}
与此同时,错误标记如下:
提交结果:编译错误 更多详情
第 7 行:错误:找不到符号 [在 __Driver__.java 中] int ret = new Solution().countPrimes(param_1); ^ 符号:方法 countPrimes(int) 位置:类 Solution
运行代码状态:运行时错误
×
运行代码结果:
您的输入
10
您的答案
java.util.NoSuchElementException
在第 937 行,java.base/java.util.Scanner.throwFor
在第 1594 行,java.base/java.util.Scanner.next
在第 2258 行,java.base/java.util.Scanner.nextInt
在第 2212 行,java.base/java.util.Scanner.nextInt
在第 8 行,Solution.countPrimes
在第 54 行,__DriverSolution__.__helper__
在第 84 行,__Driver__.main
显示差异
运行时长:N/A
请帮忙!
英文:
I have tried several ways changing my code in LeetCode but I can't find the fix, the challenge is the next one :
<<Count the number of prime numbers less than a non-negative number, n.
Example:
Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.>>
My proposal code is the next one:
import java.util.Scanner;
class Solution {
public int countPrimes(int n) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int cont = 0;
int prime = 0;
prime = sc.nextInt();
int a[] = new int [prime];
for(int i = 0; i < a.length; i++) {
a[i] = i;
cont = 0;
for(int y = 1; y< a.length; y++) {
if(a[i] % y == 0) {
cont ++;
}
}
if (cont == 2) {
sum ++;
}
}
return sum;
}
}
Meanwhile the error marks as follows:
Submission Result: Compile Error More Details
Line 7: error: cannot find symbol [in __Driver__.java] int ret = new Solution().countPrimes(param_1); ^ symbol: method countPrimes(int) location: class Solution
Run Code Status: Runtime Error
×
Run Code Result:
Your input
10
Your answer
java.util.NoSuchElementException
at line 937, java.base/java.util.Scanner.throwFor
at line 1594, java.base/java.util.Scanner.next
at line 2258, java.base/java.util.Scanner.nextInt
at line 2212, java.base/java.util.Scanner.nextInt
at line 8, Solution.countPrimes
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main
Show Diff
Runtime: N/A
Please help!
答案1
得分: 3
这段代码也会通过:
public class Solution {
public static final int countPrimes(int n) {
// 判断数字是否能被质数整除的映射,这会将该数字标记为合数
boolean[] notPrime = new boolean[n];
// 记录质数的数量
int count = 0;
for (int i = 2; i < n; i++) {
// 如果notPrime的索引为false,表示我们有一个质数,执行if语句,否则继续
if (!notPrime[i]) {
// 增加质数数量
count++;
// 查看未来的数字
for (int j = 2; i * j < n; j++) {
// 找到合数并将它们的索引设置为true
notPrime[i * j] = true;
}
}
}
return count;
}
}
参考资料
英文:
This'll also pass through:
public class Solution {
public static final int countPrimes(int n) {
// mapping for if the number is divisible by prime numbers, which would make that number a composite number
boolean[] notPrime = new boolean[n];
// counting prime numbers
int count = 0;
for (int i = 2; i < n; i++) {
// If the index of notPrime would be false, we have a prime number, we go through the if, otherwise we continue
if (notPrime[i] == false) {
// Increment the number of prime numbers
count++;
// Look into future numbers
for (int j = 2; i * j < n; j++) {
// find composite numbers and set their indices to true
notPrime[i * j] = true;
}
}
}
return count;
}
}
References
答案2
得分: 1
你在输入/输出部分弄混了:你不需要任何扫描器来完成这个,只需要:
class Solution
{
public static int countPrimes(int n)
{
int sum = 0;
int a[] = new int [n];
for(int i = 0; i < a.length; i++) {
a[i] = i;
int cont = 0;
for(int y = 1; y < a.length; y++) {
if(a[i] % y == 0) {
cont++;
}
}
if (cont == 2) {
sum++;
}
}
return sum; //这是输出
}
public static void main(String args[])
{
countPrimes(10); //这是输入
}
}
Proof:
Et voilà。LeetCode接受输入(10)和输出(4)。这就是你所需要的
<details>
<summary>英文:</summary>
You got confused with the input/output part: you don't need any scanner to do this, just:
class Solution
{
public static int countPrimes(int n)
{
int sum = 0;
int a[] = new int [n];
for(int i = 0; i < a.length; i++) {
a[i] = i;
int cont = 0;
for(int y = 1; y< a.length; y++) {
if(a[i] % y == 0) {
cont++;
}
}
if (cont == 2) {
sum++;
}
}
return sum; //this is the output
}
public static void main(String args[])
´{
countPrimes(10); //this is the input
}
}
Proof:
--------------------------
[![enter image description here][1]][1]
-------------
Et voilá. LeetCode accepts the input (10) and the output (4). That's all you need :)
[1]: https://i.stack.imgur.com/l9QJo.png
</details>
# 答案3
**得分**: 1
以下是您要求的翻译内容:
您的回答会得到一个不需要的“scanner”对象,因此您可以将其移除。
您还创建了一个“array”,这会降低性能,我建议您不要使用它,因为您不需要显示质数,只需要跟踪它们。
```java
public static int countPrimes(int n) {
int sum=0;
for(int i = n; i > 1; i-- ){
int count = 0; // 用于跟踪质数的数量。
for(int j = 2; j < i; j++){
if(i % j == 0){
count++;
}
}
if(count==0) {
sum++;
}
}
return sum;
}
英文:
Your answer gets a scanner
object which is not needed therefore you could remove it.
You also create an array
which will decrease performance, which I recommend not using as you do not need to display the prime numbers but only keep track of them.
public static int countPrimes(int n) {
int sum=0;
for(int i = n; i > 1; i-- ){
int count = 0; //Keep track of the number of primes.
for(int j = 2; j < i; j++){
if(i % j == 0){
count++;
}
}
if(count==0) {
sum++;
}
}
return sum;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论