- UID
- 299486
- 在线时间
- 0 小时
- 最后登录
- 1970-1-1
- 注册时间
- 2012-12-12
- 宅魂
- 378 点
- 贡献
- 204 点
- 宅币
- 5630 枚
- 灵石
- 0 块
- 元气(技能点)
- 3 点
- 活跃
- 5 ℃
- 听众
- 6
- 收听
- 0
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
LOLI控
- 积分
- 7526
|
发表于 2013-2-17 16:05:46
|
显示全部楼层
本帖最后由 jinlei6394 于 2013-2-17 16:08 编辑
随便找了个大数模板,然后按照你的代码改了下。不保证正确,没测试。
[mw_shl_code=cpp,true]
#include<stdio.h>
#include<string.h>
using namespace std;
const int MAXN = 102;
class bigInt
{
public:
int num[302], len;
bigInt() { num[0] = 0, len = 0; }
bigInt operator=(const int &a)
{
int tmp = a;
len = 0;
while (tmp)
num[len++] = tmp % 10, tmp /= 10;
if (!len) num[0] = 0, len = 1;
}
bigInt(const int &a)
{
int tmp = a;
len = 0;
while (tmp)
num[len++] = tmp % 10, tmp /= 10;
if (!len) num[0] = 0, len = 1;
}
bool operator<(const bigInt &a)
{
if (a.len != len)
return len < a.len;
for (int i = len - 1; i >= 0; i--)
if (num != a.num)
return num < a.num;
return false;
}
bool operator>(const bigInt &a)
{
if (a.len != len)
return len > a.len;
for (int i = len - 1; i >= 0; i--)
if (num != a.num)
return num > a.num;
return false;
}
bool operator==(const bigInt &a)
{
if (a.len != len)
return false;
for (int i = len - 1; i >= 0; i--)
if (num != a.num)
return false;
return true;
}
bool operator<=(const bigInt &a)
{
if (a.len != len)
return len <= a.len;
for (int i = len - 1; i >= 0; i--)
if (num != a.num)
return num <= a.num;
return true;
}
bool operator>=(const bigInt &a)
{
if (a.len != len)
return len >= a.len;
for (int i = len - 1; i >= 0; i--)
if (num != a.num)
return num >= a.num;
return true;
}
bigInt operator+(const bigInt &a)
{
bigInt res;
int i, j, c = 0, adda, addb;
for (i = 0, j = 0; i < len || j < a.len || c; )
{
adda = 0, addb = 0;
if (i < len)
adda = num[i++];
if (j < a.len)
addb = a.num[j++];
res.num[res.len++] = (adda + addb + c) % 10;
c = (adda + addb + c) / 10;
}
return res;
}
bigInt operator-(const bigInt &b)
{
bigInt res;
int i, j, c = 0, suba, subb;
for (i = 0, j = 0; i < len || j < b.len || c; )
{
suba = 0, subb = 0;
if (i < len)
suba = num[i++];
if (j < b.len)
subb = b.num[j++];
res.num[res.len++] = (suba - subb + c + 10) % 10;
c = (suba - subb + c + 10) / 10 - 1;
}
for (i = res.len - 1; i > 0; i--)
if (res.num) break;
res.len = i + 1;
return res;
}
bigInt operator*(const bigInt &b)
{
bigInt res;
int i, j, c, now, mulb, tmp;
memset(res.num, 0, sizeof(int) * (len + b.len));
for (i = 0; i < len; i++)
{
now = i, c = 0;
for (j = 0; j < b.len || c; )
{
mulb = 0;
if (j < b.len)
mulb = b.num[j++];
tmp = res.num[now] + num * mulb + c;
res.num[now++] = tmp % 10;
c = tmp / 10;
}
}
for (i = len + b.len - 1; i > 0; i--)
if (res.num) break;
res.len = i + 1;
return res;
}
bigInt operator/(const bigInt &b)
{
bigInt res, diva;
int i, j, c;
for (i = len - 1; i >= 0; i--)
{
if (diva.len > 1 || diva.num[0])
{
for (j = diva.len - 1; j >= 0; j--)
diva.num[j + 1] = diva.num[j];
diva.len++;
}
diva.num[0] = num;
if (!diva.len) diva.len = 1;
res.num = 0;
while (!(diva < b))
diva = diva - b, res.num++;
}
for (i = len - 1; i > 0; i--)
if (res.num) break;
res.len = i + 1;
return res;
}
bigInt operator%(const bigInt &b)
{
bigInt res, diva;
int i, j, c;
for (i = len - 1; i >= 0; i--)
{
if (diva.len > 1 || diva.num[0])
{
for (j = diva.len - 1; j >= 0; j--)
diva.num[j + 1] = diva.num[j];
diva.len++;
}
diva.num[0] = num;
if (!diva.len) diva.len = 1;
res.num = 0;
while (!(diva < b))
diva = diva - b, res.num++;
}
for (i = diva.len - 1; i > 0; i--)
if (diva.num) break;
diva.len = i + 1;
return diva;
}
void display()
{
int i;
for (i = len - 1; i > 1; i--)
if (num) break;
for (; i >= 0; i--)
printf("%d", num);
printf("\n");
}
};
int main(void)
{
bigInt biA=1, biB=0;
int i, count=0;
for(i=2;i<100;i++)
{
if(i%5==1){
if(biA>=biB){
if(biA%3==0)count++;
}
else if(biB%3==0)
count++;
}
if(biA<biB)biA=biA+biB;
else biB=biB+biA;
}
printf("%d\n",count);
return 0;
}
[/mw_shl_code]
|
|