搜索
有爱,有技术,有你^_^)y
╱人◕‿‿◕人╲订下契约(注册新用户)

合作站点账号登陆

QQ登录

只需一步,快速开始

快捷导航
楼主: jiangguo2
收起左侧

C语言作业楼。。欢迎吐槽。。

[复制链接]

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

158

主题

79

好友

2万

积分

第一章

积分
22632
 楼主| 发表于 2013-2-17 09:12:00 | 显示全部楼层
世间纷华不过一瞬执子之手难免抽离
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

3

主题

12

好友

7526

积分

LOLI控

积分
7526
发表于 2013-2-17 15:29:55 | 显示全部楼层
本帖最后由 jinlei6394 于 2013-2-17 15:35 编辑

想写好一个函数,最起码要格式清晰。
这一点不做到,就会把自己弄得头晕脑胀。
[mw_shl_code=c,true]#include<stdio.h>
int main(void)
{
        int a=1,b=0,i,count=0;/*溢出,换成long long后out put是4。。*/
        for(i=2;i<=100;i++){
                if(i%5==1){
                        if(a>=b){
                                if(a%3==0)count++;
                        }
                        else if(b%3==0)count++;
                }
                if(a<b)a+=b;
                else b+=a;
        }
        printf("%d\n",count);
        return 0;
}[/mw_shl_code]
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

3

主题

12

好友

7526

积分

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]
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

158

主题

79

好友

2万

积分

第一章

积分
22632
 楼主| 发表于 2013-2-26 21:22:06 来自手机 | 显示全部楼层
函数调用不是很明白。。好吧我只学了for循环就不想啃书了。。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

3

主题

12

好友

7526

积分

LOLI控

积分
7526
发表于 2013-2-26 21:28:14 | 显示全部楼层
jiangguo2 发表于 2013-2-26 21:22
函数调用不是很明白。。好吧我只学了for循环就不想啃书了。。

可以试试我给的这个。说实话我自己都没测试过。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

158

主题

79

好友

2万

积分

第一章

积分
22632
 楼主| 发表于 2013-2-27 16:22:21 | 显示全部楼层
jinlei6394 发表于 2013-2-26 21:28
可以试试我给的这个。说实话我自己都没测试过。

嗯,测试中……
世间纷华不过一瞬执子之手难免抽离
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

158

主题

79

好友

2万

积分

第一章

积分
22632
 楼主| 发表于 2013-2-27 16:24:21 | 显示全部楼层
jinlei6394 发表于 2013-2-26 21:28
可以试试我给的这个。说实话我自己都没测试过。

Line 3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
Line 6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'bigInt'
In function 'main':
Line 190: error: 'bigInt' undeclared (first use in this function)
Line 190: error: (Each undeclared identifier is reported only once
Line 190: error: for each function it appears in.)
Line 190: error: expected ';' before 'biA'
Line 195: error: 'biA' undeclared (first use in this function)
Line 195: error: 'biB' undeclared (first use in this function)

好多函数都不知道啊啊啊TAT果然还是操之过急了吗TAT
世间纷华不过一瞬执子之手难免抽离
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

158

主题

79

好友

2万

积分

第一章

积分
22632
 楼主| 发表于 2013-2-27 16:29:23 | 显示全部楼层
jinlei6394 发表于 2013-2-17 16:05
随便找了个大数模板,然后按照你的代码改了下。不保证正确,没测试。

[mw_shl_code=cpp,true]

cc1plus: warnings being treated as errors
t.cpp: In member function 'bigInt bigInt::operator=(const int&)':
Line 18: warning: no return statement in function returning non-void

突然发现个问题= =这是C++……
世间纷华不过一瞬执子之手难免抽离
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

3

主题

12

好友

7526

积分

LOLI控

积分
7526
发表于 2013-3-22 07:51:21 | 显示全部楼层
jiangguo2 发表于 2013-2-27 16:29
cc1plus: warnings being treated as errors
t.cpp: In member function 'bigInt bigInt::operator=(cons ...

直接return a就行 那个就是缺少返回值
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

4

主题

14

好友

3071

积分

序章

积分
3071
发表于 2013-4-16 00:43:43 | 显示全部楼层
13楼的方法不错,最经就在写一题类似的题呢
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

11

主题

37

好友

1万

积分

第一章

积分
18487
发表于 2013-4-16 18:52:26 | 显示全部楼层
高精度就是了呗。用数组存。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

签到天数: 1 天

连续签到: 1 天

[LV.1]初来乍到

4

主题

42

好友

1万

积分

喵星人

积分
14935
发表于 2013-4-16 19:10:57 | 显示全部楼层
貌似我们曾经做过这个题..
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

29

主题

54

好友

1万

积分

懒人

尔等还不速速跪下~

积分
11600
发表于 2013-4-17 15:23:26 | 显示全部楼层
看完这个帖子,我知道了long long,但是有种东西叫__int64有人知道吗
笔迹流下的那瞬间 ,   沉睡了   ,   我在迷惘......
[url=http://d.hiphotos.baidu.com/album/s%3D1400%3Bq
回复 支持 反对

使用道具 举报

该用户从未签到

14

主题

22

好友

7919

积分

序章

积分
7919
发表于 2013-4-18 13:30:16 | 显示全部楼层
本帖最后由 jains521 于 2013-4-18 14:01 编辑

斐波那契这题,
第一个规则: 被3整除, 第5*n+1个数满足  ( ((f(5*n) mod 3) + (f(5*n-1) mod 3) ) mod 3 = 0

第二个规则: 5*n+1


额, 这题用同余定理 int足够了. .
[mw_shl_code=applescript,true]1: 1 mod 3 1
1: 1 mod 3 1
2: 1+1 mod 3 =2
3: 1+2 mod 3 =0
5: 2+0 mod 3 =2
8: 0+2 mod 3= 2
13: 2+2 mod 3= 1
21: 1+2 mod 3= 0
34: 1+0 mod 3= 1
55: 0+1 mod 3= 1
89: 1+1 mod 3= 2[/mw_shl_code]  [mw_shl_code=cpp,true]#include <iostream>
using namespace std;

int main()
{
        int na=1;
        int nb=1;
        int count=0;
        for (int i=3; i<=100; i++)
        {
                nb=na+nb;
                na=nb-na;
                if( (i-1)%5 == 0)
                        if( nb % 3 == 0)
                        {
                                cout <<"The "<<i<<"th."<<endl;
                                count++;
                        }
        }
        cout <<"Has "<<count<<".\n";
        return 0;
}[/mw_shl_code]
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

15

主题

29

好友

1万

积分

第一章

积分
12726
发表于 2013-4-18 14:15:08 | 显示全部楼层
jains521 发表于 2013-4-18 13:30
斐波那契这题,
第一个规则: 被3整除, 第5*n+1个数满足  ( ((f(5*n) mod 3) + (f(5*n-1) mod 3) ) mod 3 =  ...

这个定理好神奇……完全没听说过
咱果然只会用大数死算
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

本版积分规则

小黑屋|手机版|技术宅(Z站|基宅) ( 粤ICP备18082987号-1 )

GMT+8, 2025-7-8 06:51 , Processed in 0.128916 second(s), 31 queries , Redis On.

Copyright © 2018 技术宅社区

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表