Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5f5f97

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent668052e commitd5f5f97

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

‎0204_count_primes/count_primes.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include<stdio.h>
22
#include<stdlib.h>
33
#include<stdbool.h>
4-
#include<string.h>
54

6-
staticintcountPrimes(intn)
5+
6+
intcountPrimes(intn)
77
{
8-
if (n<3)return0;
9-
8+
if (n<3) {
9+
return0;
10+
}
11+
1012
inti,j;
1113
bool*marked=malloc(n);
1214
memset(marked, false,n);

‎0204_count_primes/count_primes.cc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<stdc++.h>
2+
3+
usingnamespacestd;
4+
5+
classSolution {
6+
public:
7+
intcountPrimes(int n) {
8+
if (n <3) {
9+
return0;
10+
}
11+
12+
vector<bool>marked(n);
13+
int count = n >>1;
14+
for (int i =3; i * i <= n; i +=2) {
15+
if (!marked[i]) {
16+
for (int j = i * i; j < n; j += (i <<1)) {
17+
if (!marked[j]) {
18+
marked[j] =true;
19+
--count;
20+
}
21+
}
22+
}
23+
}
24+
return count;
25+
}
26+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp