zoukankan      html  css  js  c++  java
  • 算法竞赛入门经典习题解答

     

     

     

     

     

     思考题

     因为浮点数+=0.1之后变成0.10000000000000001,而不是真正的0.1,所以造成了永远无法等于10.1,形成死循环。 

    倒三角形第一种解法:逆序 

     倒三角形第二种解法顺序;

     

     

    程序3-1 逆序输出

    蛇形填数

    #include <iostream>

    #include <cstdio>

    int const maxn = 101;

    int n,a[maxn][maxn];

    using namespace std;

    int main()

    {

        cin>>n;

        int x = 0,y = n-1,tot;

        tot = a[x][y] = 1;

        while(tot < n*n){

        while(x+1<n && !a[x+1][y]) a[++x][y] = ++tot;

        while(y-1>=0 && !a[x][y-1]) a[x][--y] = ++tot;

        while(x-1>=0 && !a[x-1][y]) a[--x][y] = ++tot;

        while(y+1<n && !a[x][y+1]) a[x][++y] = ++tot;

        }

        for(int i = 0;i<n;i++)

        {

            for(int j = 0;j<n;j++)

                printf("%4d",a[i][j]);

            cout<<endl;

        }

        return 0;

    }

    错误之处:

    1、没有写终止打破循环的条件tot < n*n;

    2、条件里面没有写下一个位置的条件即使+1或-1,判断为空里面用了自增,而应该要用下一个位置+1或-1.

    3、赋值语句里面应该要用自增或自减。

    运行效果如下所示:

     TeX 中的引号

     

     例题 3-3 回文词(Palindrome, Uva401)

     

     Uva1586 分子量

     

  • 相关阅读:
    [Win32]一个调试器的实现(十)显示变量
    [Win32]防止套接字被继承
    [C++]实现委托模型
    [Win32]一个调试器的实现(十一)显示函数调用栈
    [Win32]IP数据报的首部如何定义
    FMECA方法及工程应用
    C#控制台应用程序自动关闭
    ckedit 3.0 配置(一)
    [转]“余则成”教你办公室生存法则20条
    Element UI之Select选择器优化
  • 原文地址:https://www.cnblogs.com/longxue1991/p/12331481.html
Copyright © 2011-2022 走看看