• Bug战场:C++篇


    写在前面

    朋友们,该打仗了!该考验你们处理Bug的时候了!

    小声BB:答案在文末!

    战场1(新手)

    先来一个C++新手版(这个应该两秒钟就看出来了吧):

    #include
    using namespace std;
    
    int main()
    {
    	while (true);
    	{
    		cout<<"Hello World!"<<endl;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    战场2(新手)

    了解C++全部结构的应该一秒钟就看出来了:

    #include
    
    int main()
    {
    	cout<<1<< endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    战场3(新手)

    一秒钟,看出来:

    #include
    using namespace std;
    double a, b;
    int main()
    {
    	cin<<a<<b;
    	printf("%.3lf%%",b*100/a);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    战场4(新手)

    #include
    using namespace std;
    double x,a,y,b;
    int main(){
    	cin>>x>>a>>y>>b;
        cout<<setprecision(2)<<(x*a-y*b)/(a-b)<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    前面都是编译失败问题,接下来我们来一些错误代码,会有题目描述。

    战场5(中级)

    题目描述

    高精度计算 S = 1 ! + 2 ! + 3 ! + ⋯ + n ! S = 1! + 2! + 3! + \cdots + n! S=1!+2!+3!++n! n ≤ 50 n \le 50 n50)。

    其中 ! 表示阶乘,定义为 n ! = n × ( n − 1 ) × ( n − 2 ) × ⋯ × 1 n!=n\times (n-1)\times (n-2)\times \cdots \times 1 n!=n×(n1)×(n2)××1。例如, 5 ! = 5 × 4 × 3 × 2 × 1 = 120 5! = 5 \times 4 \times 3 \times 2 \times 1=120 5!=5×4×3×2×1=120

    输入格式

    一个正整数 n n n

    输出格式

    一个正整数 S S S,表示计算结果。

    样例 #1

    样例输入 #1

    3
    
    • 1

    样例输出 #1

    9
    
    • 1

    提示

    【数据范围】

    对于 100 % 100 \% 100% 的数据, 1 ≤ n ≤ 50 1 \le n \le 50 1n50

    【其他说明】

    注,《深入浅出基础篇》中使用本题作为例题,但是其数据范围只有 n ≤ 20 n \le 20 n20,使用书中的代码无法通过本题。

    如果希望通过本题,请继续学习第八章高精度的知识。

    给出的错误代码

    提示:有三处错误。

    #include
    using namespace std;
    struct data
    {
        int len;
        int s[1005];
    };
    data ans,anss;
    int n;
    data mul(data x,int y)
    {
        data c;
        for(int i=1;i<=1000;i++)c.s[i]=0;
        for(int i=1;i<=x.len;i++)
        {
            c.s[i]+=x.s[i]*y;
            c.s[i+1]=c.s[i]/10;
            c.s[i]=c.s[i]%10;
        }
        int lan=x.len;
        while(c.s[lan]>0)
        {
            lan++;
            c.s[lan+1]+=c.s[lan]/10;
            c.s[lan]=c.s[lan]%10;
        }
        c.len=lan;
        return c;
    }
    data add(data x,data y)
    {
        data c;
        for(int i=1;i<=1000;i++)c.s[i]=0;
        int lan=max(x.len,y.len);
        for(int i=1;i<=lan;i++)
        {
            c.s[i]+=x.s[i]+y.s[i];
            c.s[i+1]+=c.s[i]/10;
            c.s[i]=c.s[i]%10;
        }
        if(c.s[lan+1]>1)lan++;
        c.len=lan;
        return c;
    }
    int main()
    {
        cin>>n;
        for(int i=1;i<=1000;i++)ans.s[i]=0;
        ans.len=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=1000;j++)anss.s[j]=0;
            anss.s[1]=1;
            anss.len=1;
            for(int j=1;j<=i;j++)anss=mul(anss,j);
            ans=add(ans,anss);
        }
        for(int i=ans.len;i>=0;i--)cout<<ans.s[i];
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    答案

    战场1

    #include
    using namespace std;
    
    int main()
    {
    	while (true);
    	{
    		cout<<"Hello World!"<<endl;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    战场2

    #include
    using namespace stdl
    int main()
    {
    	cout<<1<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    战场3

    #include
    using namespace std;
    double a, b;
    int main()
    {
    	cin>>a>>b;
    	printf("%.3lf%%",b*100/a);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    战场4

    #include
    using namespace std;
    double x,a,y,b;
    int main(){
    	cin>>x>>a>>y>>b;
        cout<<fixed<<setprecision(2)<<(x*a-y*b)/(a-b)<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    战场5

    #include
    using namespace std;
    struct data
    {
        int len;
        int s[1005];
    };
    data ans,anss;
    int n;
    data mul(data x,int y)
    {
        data c;
        for(int i=1;i<=1000;i++)c.s[i]=0;
        for(int i=1;i<=x.len;i++)
        {
            c.s[i]+=x.s[i]*y;
            c.s[i+1]=c.s[i]/10;
            c.s[i]=c.s[i]%10;
        }
        int lan=x.len;
        while(c.s[lan+1]>0)
        {
            lan++;
            c.s[lan+1]+=c.s[lan]/10;
            c.s[lan]=c.s[lan]%10;
        }
        c.len=lan;
        return c;
    }
    data add(data x,data y)
    {
        data c;
        for(int i=1;i<=1000;i++)c.s[i]=0;
        int lan=max(x.len,y.len);
        for(int i=1;i<=lan;i++)
        {
            c.s[i]+=x.s[i]+y.s[i];
            c.s[i+1]+=c.s[i]/10;
            c.s[i]=c.s[i]%10;
        }
        if(c.s[lan+1]>0)lan++;
        c.len=lan;
        return c;
    }
    int main()
    {
        cin>>n;
        for(int i=1;i<=1000;i++)ans.s[i]=0;
        ans.len=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=1000;j++)anss.s[j]=0;
            anss.s[1]=1;
            anss.len=1;
            for(int j=1;j<=i;j++)anss=mul(anss,j);
            ans=add(ans,anss);
        }
        for(int i=ans.len;i>=1;i--)cout<<ans.s[i];
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    js判断数据类型、toString和valueOf区别,类型转换、不同类型间的运算、判断相等
    虚拟化不是区块链的发展道路,也不是真正意义上的区块链的呈现形式
    UE5蓝图接口使用方法
    Golang 实现word和Excel处理
    MySQL进阶—索引1
    Leetcode 1774. 最接近目标价格的甜点成本
    语法练习:makes10
    DELTA热金属检测器维修V5G-JC-R1激光测量传感器/检测仪原理分析
    【神印王座】两位新老婆登场!龙皓晨神勇救美,采儿却大发醋意!
    Sentry的安装、配置、使用
  • 原文地址:https://blog.csdn.net/weixin_59197425/article/details/126420721