summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-05-18 17:03:50 +0200
committerSanto Cariotti <sancn@live.com>2017-05-18 17:03:50 +0200
commit5c1ff51bc5a99115ae6c8a62a0168da31cc29f11 (patch)
tree5bdb14959fcc46fe947e078e4a25189a7da0c7ef
parent9a41a4e2ac2d040e23417689aeab499ad915a9c3 (diff)
now it does 100/100
-rw-r--r--cpp/lswf.cpp86
1 files changed, 50 insertions, 36 deletions
diff --git a/cpp/lswf.cpp b/cpp/lswf.cpp
index ea2551f..79f46c7 100644
--- a/cpp/lswf.cpp
+++ b/cpp/lswf.cpp
@@ -1,60 +1,74 @@
-/* INPUT:
+/* INPUT:
* 19
- *
+ *
* OUTPUT:
* 1000101
*/
#include <iostream>
#include <fstream>
-#define MAXG 1000
-
-using namespace std;
-
-int fibonacci(int* fib, int N)
-{
- fib[0] = 1; fib[1] = 1;
- int lst;
-
- for(int i = 2; i < N; i++) {
- fib[i] = fib[i-1] + fib[i-2];
- lst = i;
- if(fib[i] > N) break;
- }
-
- return lst;
-}
+#include <list>
+#define MAXG 1000001
int main()
{
- ifstream in("input.txt");
- ofstream out("output.txt");
-
+ std::ifstream in("input.txt");
+ std::ofstream out("output.txt");
+ std::list<int> seq;
+ std::list<int>::iterator j;
+
int N, i;
in >> N;
int caracts[MAXG], somma = 0, potSomma;
int lastc;
+
+ auto fibonacci = [&caracts] (int N) {
+ caracts[0] = 1; caracts[1] = 1;
+
+ int lst;
+
+ for(int i = 2; i < N; i++) {
+ caracts[i] = caracts[i-1] + caracts[i-2];
+ lst = i;
+
+ if(caracts[i] > N)
+ break;
+ }
+
+ return lst;
+ };
+
if(N > 4)
- lastc = fibonacci(caracts, N);
+ lastc = fibonacci(N);
else {
- caracts[0] = 1; caracts[1] = 1; caracts[2] = 2; caracts[3] = 3;
- if(N == 1) lastc = 1;
- else if(N == 2) lastc = 2;
- else if(N == 3) lastc = 3;
- else lastc = 4;
+ caracts[0] = 1;
+ for(i = 1; i < 4; i++)
+ caracts[i] = i;
+
+ switch (N) {
+ case 1:
+ lastc = 1;
+ break;
+ case 2:
+ lastc = 2;
+ break;
+ case 3:
+ lastc = 3;
+ break;
+ default:
+ lastc = 4;
+ }
}
- int* seq = new int[lastc];
-
- seq[0] = 1;
+
for(i = lastc; i > 0; i--) {
potSomma = somma + caracts[i];
if(potSomma < N) {
somma = potSomma;
- seq[i] = 1;
- } else seq[i] = 0;
+ seq.push_front(1);
+ } else seq.push_front(0);
}
- for(i = 0; i < lastc; i++) out << seq[i];
-
- delete[] seq;
+ seq.push_front(1);
+ for(j = seq.begin(); j != seq.end(); j++) out << *j;
+
in.close();
out.close();
return 0;