• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

練習用です。いろんなものがごちゃまぜです。


Commit MetaInfo

Revision4857168a1f0df3cf09be0ebb9e3f40b339e54a2a (tree)
Time2017-03-15 19:42:08
Authorbellyoshi <tccwas@gmai...>
Commiterbellyoshi

Log Message

profit

Change Summary

Incremental Difference

--- a/CPP/ALDS1_1/prime.cpp
+++ b/CPP/ALDS1_1/prime.cpp
@@ -1,36 +1,139 @@
11 #include <iostream>
22 #include <cmath>
3+#include <cassert>
4+
35 using namespace std;
46
5-bool isprime(int number){
6- if (number == 1){
7- return false;
8- }
7+typedef unsigned int MEMTYPE;
98
10- if (number == 2){
11- return true;
9+class mybitset {
10+ MEMTYPE *mem;
11+ int size;
12+ bool ismemfree;
13+public:
14+ int bitlen(){
15+ return sizeof(MEMTYPE) * 8;
16+ }
17+ mybitset(){
18+ ismemfree = true;
19+ }
20+ void setsize(int size){
21+ this->size = size;
22+ mem = new MEMTYPE[size / bitlen() + 1];
23+ ismemfree = false;
1224 }
13- if (number % 2 == 0){
14- return false;
25+ ~mybitset(){
26+ if (!ismemfree){
27+ delete [] mem;
28+ }
29+ }
30+ bool get(int pos){
31+ MEMTYPE bit = 1 << (pos % bitlen());
32+ return (mem[pos / bitlen()] & bit) != 0;
33+ };
34+ void set(int pos){
35+ MEMTYPE bit = 1 << (pos % bitlen());
36+ mem[pos / bitlen()] |= bit;
37+ };
38+ void reset(int pos){
39+ MEMTYPE bit = ~(1 << (pos % bitlen()));
40+ mem[pos / bitlen()] &= bit;
1541 }
42+};
1643
17- for(int i = 3; i < number; i+=2){
18- if(number % i == 0)return false;
44+class primer {
45+ mybitset isprime;
46+ int max;
47+public:
48+ bool getPrime(int number);
49+ primer(int max);
50+};
51+
52+bool primer::getPrime(int number){
53+ assert(max > number);
54+ if (max > number){
55+ return isprime.get(number);
1956 }
57+ //assert(max * max > number);
58+ //for(int i = 0; i < max ; i++){
59+ // if (isprime.get(i)){
60+ // if (number % i == 0){
61+ // return false;
62+ // }
63+ // }
64+ //}
2065 return true;
2166 }
67+primer::primer(int max){
68+ this->max = max;
69+ isprime.setsize(max + 1);
70+ //エラトステネスの篩で素数かどうかの判定
71+ isprime.reset(0);
72+ isprime.reset(1);
73+ for(int i = 2; i < max; i++){
74+ isprime.set(i);
75+ }
76+ for(int i = 2; i < sqrt(max); i++){
77+ if(isprime.get(i)){
78+ for(int j = 2; i * j < max; j++){
79+ isprime.reset(i * j);
80+ }
81+ }
82+ }
83+}
84+
85+
86+int main(int argc, char const *argv[]) {
87+ int count;
88+ cin >> count;
89+ int prime_count = 0;
90+ primer p = primer(100000000);
91+ for (int i = 0; i < count; i++) {
92+ int number;
93+ cin >> number;
94+ if (p.getPrime(number)){
95+ prime_count++;
96+ }
97+ }
98+ cout << prime_count << endl;
99+ return 0;
100+}
22101
102+/* test code
23103 int main(int argc, char const *argv[]) {
24104 int count;
25105 cin >> count;
26106 int prime_count = 0;
107+ primer p = primer(20);
108+ mybitset b;
109+ b.setsize(100);
110+ cout << "b.bitlen() = " << b.bitlen() << endl;
111+ for(int i = 0; i < 20; i++){
112+ b.set(i);
113+ if(!b.get(i)){
114+ cout << "set error "<< i << endl;
115+ }
116+ }
117+ for(int i = 0; i < 20; i++){
118+ b.reset(i);
119+ if(b.get(i)){
120+ cout << "reset error"<< i << endl;
121+ }
122+ }
123+ for(int i = 20; i >= 0; i--){
124+ b.set(i);
125+ if(!b.get(i)){
126+ cout << "set error" << i << endl;
127+ }
128+ }
27129 for (int i = 0; i < count; i++) {
28130 int number;
29131 cin >> number;
30- if (isprime(number)){
132+ if (p.getPrime(number)){
31133 prime_count++;
32134 }
33135 }
34136 cout << prime_count << endl;
35137 return 0;
36138 }
139+*/
--- /dev/null
+++ b/CPP/ALDS1_1/profit.cpp
@@ -0,0 +1,33 @@
1+#include <iostream>
2+#include <cassert>
3+
4+using namespace std;
5+
6+int main(int argc, char const *argv[]) {
7+ int count;
8+ cin >> count;
9+ int *nums = new int[count];
10+ for (int i = 0; i < count; i++) {
11+ cin >> nums[i];
12+ }
13+
14+ assert(count >= 2);
15+ if (count < 2) {
16+ return 1;
17+ }
18+
19+ int maxprofit = nums[1] - nums[0];
20+
21+ for(int i = 0; i < count - 1; i++){
22+ for(int j = i + 1; j < count; j++){
23+ int profit = nums[j] - nums[i];
24+ if (maxprofit < profit){
25+ maxprofit = profit;
26+ }
27+ }
28+ }
29+ cout << maxprofit << endl;
30+
31+ delete [] nums;
32+ return 0;
33+}