2009年9月11日星期五

Solution of Project Euler problems 20 and 26

My recent solution about problem 20 and problem 26 of Project euler




// Problem 20
// Simply implemenent an multiplication of big integer in C++
// and then calculate it

#include
#include

#define MAX 100
#define mod 10000
#define baselen 4
#define in(a) scanf("%d",&a)
typedef int type;
/////////////////////////////////////
struct bint{
type dig[MAX], len;
bint(){len = 0, dig[0] = 0;}
};


bool setNunmber(bint& a, char number[]){
type i, j, w, k, p;
char data[MAX*baselen+1];
strcpy(data, number);
w = strlen(data) - 1, a.len = 0;
for(p=0;p<=w && data[p]=='0';p++);
while(1){
i = j = 0, k = 1;
while(i=p){
j = j+ (data[w--] - '0')*k;
k *= 10, i++;
}
a.dig[a.len++] = j;
if(w }
a.len--;
return true;
}

void getAnsToString(bint& a, char ans[])
{
type i;
i = a.len - 1;
char *p = ans;
sprintf(ans, "%d", a.dig[a.len]);
p += strlen(ans);
while(i>=0)
{
sprintf(p, "%04d",a.dig[i--]);
p += 4;
}
sprintf(p, "%c", 0);
}



void by(bint a, type b, bint& c){
type i, carry;
for( i = carry = 0; i <= a.len || carry; i++){
if( i <= a.len ) carry += b*a.dig[i];
c.dig[i] = carry%mod;
carry /= mod;
}
i--;
while( i && !c.dig[i] )i--;
c.len = i;
}

int main()
{
bint product;
char productInString[MAX*baselen+1];

setNunmber(product, "1");
for (int i = 2; i <= 100; ++i) by(product, i, product);
getAnsToString(product, productInString);

printf("%s\n", productInString);

int total_digit = 0;
for (int i = 0; i < strlen(productInString); ++i) total_digit += productInString[i] - '0';
printf("Sum of the digit = %d\n", total_digit);

return 0;
}




//Problem 26

#include
#include
#include
using namespace std;
vector recurring_cycle; //use to record the recurring_cycle

/* use to record whether an remainder have ever appeard, since when
we find an remainder have already exist, we will know we find the
recurring cycle. remainder[n] record the first position after radix
point we encounter such remainder.
*/
int remainder[1000];
int ans_d; // record when will get the max length of recurring cycle
int maxlength = 0; // record the max length of recurring cycle
int main()
{
for (int d = 2; d < 1000; ++d)
{
recurring_cycle.clear();
memset(remainder, -1, sizeof(remainder));
int current_remainder = 1;
int position = 0;
while (1)
{
if (remainder[current_remainder] != -1)
{
/*Uncomment this part to see the recurring cycle
printf("%d : ", d);
for (int i = 0; i <= position - 1; ++i) printf("%d", recurring_cycle[i]);
printf("\n");
*/
if (position - remainder[current_remainder] > maxlength)
{
maxlength = position - remainder[current_remainder];
ans_d = d;
}
break;
}
else remainder[current_remainder] = position;
current_remainder *= 10;
recurring_cycle.push_back(current_remainder / d);

if (current_remainder >= d) current_remainder %= d;
if (current_remainder == 0)
{
/* For test purpose only
printf("%d : ", d);
for (int i = 0; i <= position - 1; ++i) printf("%d", recurring_cycle[i]);
printf("\n");*/
break;
}
++position;
}
}
printf("Max length of recurring cycle is %d, when d is %d\n", maxlength, ans_d);
return 0;
}

没有评论: