1F:应聘资料Proof of abilitiesProject_Euler_No_104_Pandigital_Fibonacci_ends.cs
//Project Euler #104: Pandigital Fibonacci ends
using System;
using System.Collections.Generic;
using System.IO;
class Solution
{
static bool checkk(short[] gn, short k)
{
bool contain = false;
int i = 1;
while (i <= k)
{
bool first = false;
bool last = false;
for (int j = 0; j <= k - 1; j++)
{
if (gn[j] == i)
{
first = true;
break;
//Console.WriteLine("first:"+first);
}
}
if (first == false)
{
contain = false;
break;
}
for (int j = gn.Length - 1; j > gn.Length - k - 1; j--)
{
if (gn[j] == i)
{
last = true;
break;
//Console.WriteLine("last:"+first);
}
}
if (last == false)
{
contain = false;
break;
}
if (first && last)
{
contain = true;
i++;
}
else
{
contain = false;
break;
}
}
//Console.WriteLine("contain"+contain);
return contain;
}
//adder
static short[] adder(short[] x, short[] y)
{
int digit = x.Length;
short[] outcome = new short[digit + 1];
short[] newy = new short[digit];
/*Console.WriteLine("newx:");
for (int i=0;i<digit;i++){
2F:应聘资料Proof of abilitiesProject_Euler_No_104_Pandigital_Fibonacci_ends.cs
Console.Write(newx[i]);
}
Console.WriteLine();*/
for (int i = y.Length - 1; i >= 0; i--)
{
if (y.Length < digit)
{
newy[i + 1] = y[i];
}
else
{
newy[i] = y[i];
}
}
/*Console.WriteLine("newy:");
for (int i=0;i<digit;i++){
Console.Write(newy[i]);
}
Console.WriteLine();*/
for (int i = digit - 1; i >= 0; i--)
{
short adding = (Int16)(x[i] + newy[i] + outcome[i + 1]);
if (adding >= 10)
{
outcome[i + 1] = (Int16)(adding % 10);
outcome[i] = 1;
}
else
{
outcome[i + 1] = adding;
}
}
if (outcome[0] == 0)
{
short[] modoutcome = new short[digit];
for (int i = 0; i < digit; i++)
{
modoutcome[i] = outcome[i + 1];
}
return modoutcome;
}
else
{
return outcome;
}
}
//trans int to array int
static short[] mgtran(short interger)
{
//Console.WriteLine("in:"+interger);
short[] array = new short[1];
array[0] = interger;
//Console.WriteLine("out:"+array[0]);
return array;
}
static void Main(String[] args)
{
//read
short a = Convert.ToInt16(Console.ReadLine());
short b = Convert.ToInt16(Console.ReadLine());
short k = Convert.ToInt16(Console.ReadLine());
//start to do fibo
int n = 1;
//int gnone=a;
3F:应聘资料Proof of abilitiesProject_Euler_No_104_Pandigital_Fibonacci_ends.cs
short[] gnone = mgtran(a);
//int gntwo=b;
short[] gntwo = mgtran(b);
//int gnthree=0;
short[] gnthree = mgtran(0);
while (n <= 1000000)
{
//circle fibo
switch (n)
{
case 1: gnthree = adder(gnone, new short[1]);
break;
case 2: gnthree = adder(gntwo, new short[1]);
break;
case 3: gnthree = adder(gntwo, gnone);
break;
default:
gnone = adder(gntwo, new short[1]);
gntwo = adder(gnthree, new short[1]);
gnthree = adder(gntwo, gnone);
break;
}
/*Console.WriteLine("fibo:"+n);
for (int i=0;i<gnthree.Length;i++){
Console.Write(gnthree[i]);
}
Console.WriteLine();*/
if (gnthree.Length >= k)
{
if (checkk(gnthree, k))
{
Console.WriteLine((n));
break;
}
}
n++;
}
if (n == 1000001)
{
Console.WriteLine("no solution");
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be
named Solution */
}
}

Project_Euler_No_104_Pandigital_Fibonacci_ends

  • 1.
    1F:应聘资料Proof of abilitiesProject_Euler_No_104_Pandigital_Fibonacci_ends.cs //ProjectEuler #104: Pandigital Fibonacci ends using System; using System.Collections.Generic; using System.IO; class Solution { static bool checkk(short[] gn, short k) { bool contain = false; int i = 1; while (i <= k) { bool first = false; bool last = false; for (int j = 0; j <= k - 1; j++) { if (gn[j] == i) { first = true; break; //Console.WriteLine("first:"+first); } } if (first == false) { contain = false; break; } for (int j = gn.Length - 1; j > gn.Length - k - 1; j--) { if (gn[j] == i) { last = true; break; //Console.WriteLine("last:"+first); } } if (last == false) { contain = false; break; } if (first && last) { contain = true; i++; } else { contain = false; break; } } //Console.WriteLine("contain"+contain); return contain; } //adder static short[] adder(short[] x, short[] y) { int digit = x.Length; short[] outcome = new short[digit + 1]; short[] newy = new short[digit]; /*Console.WriteLine("newx:"); for (int i=0;i<digit;i++){
  • 2.
    2F:应聘资料Proof of abilitiesProject_Euler_No_104_Pandigital_Fibonacci_ends.cs Console.Write(newx[i]); } Console.WriteLine();*/ for(int i = y.Length - 1; i >= 0; i--) { if (y.Length < digit) { newy[i + 1] = y[i]; } else { newy[i] = y[i]; } } /*Console.WriteLine("newy:"); for (int i=0;i<digit;i++){ Console.Write(newy[i]); } Console.WriteLine();*/ for (int i = digit - 1; i >= 0; i--) { short adding = (Int16)(x[i] + newy[i] + outcome[i + 1]); if (adding >= 10) { outcome[i + 1] = (Int16)(adding % 10); outcome[i] = 1; } else { outcome[i + 1] = adding; } } if (outcome[0] == 0) { short[] modoutcome = new short[digit]; for (int i = 0; i < digit; i++) { modoutcome[i] = outcome[i + 1]; } return modoutcome; } else { return outcome; } } //trans int to array int static short[] mgtran(short interger) { //Console.WriteLine("in:"+interger); short[] array = new short[1]; array[0] = interger; //Console.WriteLine("out:"+array[0]); return array; } static void Main(String[] args) { //read short a = Convert.ToInt16(Console.ReadLine()); short b = Convert.ToInt16(Console.ReadLine()); short k = Convert.ToInt16(Console.ReadLine()); //start to do fibo int n = 1; //int gnone=a;
  • 3.
    3F:应聘资料Proof of abilitiesProject_Euler_No_104_Pandigital_Fibonacci_ends.cs short[]gnone = mgtran(a); //int gntwo=b; short[] gntwo = mgtran(b); //int gnthree=0; short[] gnthree = mgtran(0); while (n <= 1000000) { //circle fibo switch (n) { case 1: gnthree = adder(gnone, new short[1]); break; case 2: gnthree = adder(gntwo, new short[1]); break; case 3: gnthree = adder(gntwo, gnone); break; default: gnone = adder(gntwo, new short[1]); gntwo = adder(gnthree, new short[1]); gnthree = adder(gntwo, gnone); break; } /*Console.WriteLine("fibo:"+n); for (int i=0;i<gnthree.Length;i++){ Console.Write(gnthree[i]); } Console.WriteLine();*/ if (gnthree.Length >= k) { if (checkk(gnthree, k)) { Console.WriteLine((n)); break; } } n++; } if (n == 1000001) { Console.WriteLine("no solution"); } /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ } }