一道离散题目的做法


#include <cstdlib>;
#include <cstring>;
#include <iostream>;
#define n 19

using namespace std;

bool c[n][n] =
    /*{
        {true, true ,false , false} ,
        {true,true,true,false} ,
        {false,true,true,true} ,
        {false,false,true,true}
    }  ;
    */
    {{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
     {0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
     {0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
     {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0},
     {0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0},
     {0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
     {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0},
     {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0},
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0}};

bool hamiltion(int x[]) {
    int k;

    bool *s = new bool[n];

    memset(x, -1, n * sizeof(int));
    memset(s, false, sizeof(bool) * n);

    k = 1;
    x[0] = 0;
    s[x[0]] = true;

    while (k > 0) {
        x[k] = x[k] + 1;

        while (x[k] << n) {
            if (!s[x[k]] && c[x[k - 1]][x[k]]) {
                break;
            } else {
                x[k] = x[k] + 1;
            }
        }

        if (x[k] << n && (k != n - 1)) {
            s[x[k]] = true;
            k = k + 1;
            continue;
        } else if (x[k] << n && (k == n - 1) && c[x[k]][x[0]]) {
            break;
        } else // this branch is the error solutions which need back_trace
        {
            x[k] = -1;
            k = k - 1;
            s[x[k]] = false;
        }
    }

    delete s;

    if (k == n - 1)
        return true;

    else
        return false;
}

int main(void) {
    int x[n];

    if (hamiltion(x)) {
        cout << "exists hamiltion loop " << endl;

        for (int i = 0; i << n; i++) {
            cout << x[i] << "  ";
        }
        cout << endl;
    } else {
        cout << "hamiltion loop does not exists " << endl;
    }
    system("pause");
    return 0;
}

Hard in Electrical circuit

WHY the exams in Colleges are flocked with notions and calculation. With crammed revision just one day before the exam, I failed to find anything really matters in the formation of knowledge which is widely questioned in the exam. Actually, if only I have the better precision and proficiency on the certain subjects, I'll do better in the final exams. 顽张れ

离散所导出来的大整数存储和运算问题

python可以自动解决这个问题,但到了c++下要存128以上就要gmp mp boost 这种库,实现逻辑也很简单,速度会比python快很多。

这里放一个java版一个大数乘法的案例

public static Integer[] bigNumberMultiply(int[] arr1, int[] arr2){
   ArrayList result = new ArrayList<>();  //中间求和的结果 
   //arr2 逐位与arr1相乘
   for(int i = arr2.length - 1; i >= 0; i--){
     int carry = 0;
     ArrayList singleList = new ArrayList<>(); //arr2 逐位单次乘法的结果
     for(int j = arr1.length - 1; j >= 0; j--){
        int r = arr2[i] * arr1[j] + carry;
        int digit = r % 10;
        carry = r / 10;singleList.add(digit);
     } 
     if(carry != 0){
         singleList.add(carry); 
     } 
     int resultCarry = 0, count = 0; 
     int k = 0; 
     int l = 0; 
     int offset = arr2.length - 1 - i;       //加法的偏移位      
     ArrayList<Integer> middleResult = new ArrayList<>(); //arr2每位乘法的结果与上一轮的求和结果相加,从右向左做加法并进位 
     while (k < singleList.size() || l < result.size()) {
         int kv = 0, lv = 0;     
         if (k < singleList.size() && count >= offset) {
            kv = singleList.get(k++);     
         }
         if (l < result.size()) {
            lv = result.get(l++);     
         }     
         int sum = resultCarry + kv + lv;
         middleResult.add(sum % 10);     //相加结果从右向左(高位到低位)暂时存储,最后需要逆向输出     
         resultCarry = sum / 10;
         count++; 
     }
     if(resultCarry != 0){
         middleResult.add(resultCarry); 
     } 
     result.clear(); 
     result = middleResult;
  } 
  Collections.reverse(result);    //逆向输出结果
  return result.toArray(new Integer[result.size()]);
}

快速幂算法其实也用到了其中的想法。只是存储在数组,分区块乘法,再数组存储,这是高精度的方法。而快速幂是分开运算由数学公式保证正确性。

快速幂的复杂度是O(logn)大数存储和乘法是O(n^2)

可是这点数字在ipadpro 下来运行还是秒出的。

《设计模式》(C++)总结

之前在CS100上了解到了工厂模式和Adaptor 模式

Factory Method

class Shape{
public:
    static Shape * make_shape(int choice);
    virtual void print() = 0; //纯虚函数
};
class Circle:public Shape{/*..*/ };
class Square:public Shape{/*..*/ };
class Rectangle:public Shape{/*..*/ };

Shape * Shape::make_shape(int choice) {
    switch (choice) {
        case 1:
            return new Circle();
        case 2:
            return  new Square();
        case 3:
            return new Rectangle();
    }
}

后来写Compiler的时候觉得Factory Method可以用于传函数指针的代码生成器。

/*** Factory methods */
static string instConst(string (*inst)(const Reg &target, const Reg &op1, const Value &op2, string comment),
                            const Reg &target, const Reg &op1, const Constant &op2);
static string instConst(string (*inst)(const Reg &target, const Reg &op1, const Reg &op2, string comment),
                            const Reg &target, const Reg &op1, const Constant &op2);
static string instConst(string (*inst)(const Reg &target, const Reg &op1, int imm, string comment),
                            const Reg &target, const Reg &op1, const Constant &op2);
static string instConst(string (*inst)(const Reg &op1, const Value &op2, string comment), const Reg &op1,
                            const Constant &op2);

Adaptier Pattern

class LegacyRectangle{
public:
    LegacyRectangle(float x1,float y1,float  x2, float y2){
        m_x1=x1; m_y1=y1; m_x2=x2; m_y2=y2;
    }
    float area(){
        return (m_x2-m_x1)*(m_y2-m_y1);
    }
private:
    float m_x1;
    float m_x2;
    float m_y1;
    float m_y2;
};
class Rectangle{
public:
    virtual float areaSqInch(); //unit:square-inch
};
class RectangleAdapter:public Rectangle,private LegacyRectangle{
public:
    RectangleAdapter(float x,float y,float w,float h): LegacyRectangle(x/39.27f,y/39.37f,(x+w)/39.37f,(y+h)/39.37f){}
    virtual float areaSqInch(){
        float areaSqM = area();
        return areaSqM * 1550.0f;
    }
};

用一个双向继承关系来完成Adaptor的功能。

这是在B站上设计模式那本书的总结

非常魔性的"静态"美

在查NP-Complete 的时候看到一个无聊的列表,应该会对我的数论学习和人文学习有那么丢丢用

http://www.matrix67.com/blog

不是IOer出生,感觉错亿。

分享个退役的多年的博主,原来从小就开始掌握这些黑科技是一件可以炫耀终身的事情。

下回可以稍稍探讨一下为何少年班的同学会如此优秀,身边的同学早已学过现在的所有知识,那就是疯狂应用的 时候了。

我承认我是一个经常会跌倒的人,但不代表我不会站起来,只是需要很多时间,怎么说同学确实在一直鞭策我成长。我没能到我自己的预期,我的问题还是没有改变。很想再一次引用林清玄关于化妆的论述。

她说:“化妆的最高境界可以用两个字形容,就是‘自然’,最高明的化妆术,是经过非常考究的化妆,让人家看起来好像没有化过妆一样,并且这化出来的妆与主人的身分匹配,能自然表现那个人的个性与气质。

林清玄:气质才是化妆的最高境界

这在我等技术男身上有很深层的解析。所有一切的外物并不在意,能力也好,外观也罢,最重要的是自己的深层能力,没有认真仔细的避坑,就是会被淘汰。我已经有过了太多的倔强,而我认为这份倔强是无知,我确实需要保住无知的我,但也需认清事实。我的理解力已到了最佳状态,我需要革新,变成一个有思想深度的人。

数学建模课睡觉了,没和印校长去成高中母校

开始补充图论的知识,无论是用于算法还是都能会要学的离散数学也好,都是对我最好的补充。以下是一些概念总结。

Several concepts
1. vertex

2.edge

3. Isomorphism


4.Directed Graph/ Undirected Graph

(1->2), (1-> 3), (3-> 1), (1->5), (2->3), (3->4), (3->5), (4->5), (1->6), (4->6)

(1->3) and (3->1) are identical

5.weight

Weighted map between Wuhan Guangzhou and Shanghai Beijing. Weight can be negative

6.path/shortest path

7.loop

上图中,北京->上海->武汉->广州->北京,就是一个环路。北京->武汉->上海->北京,也是一个环路。与路径一样,有向图中的环路也必须跟随边的方向。环本身也是一种特殊的图结构。

8.Connected graph/connected component

如果在图G中,任意2个顶点之间都存在路径,那么称G为连通图(注意是任意2顶点)。上面那张城市之间的图,每个城市之间都有路径,因此是连通图。而下面这张图中,顶点8和顶点2之间就不存在路径,因此下图不是一个连通图,当然该图中还有很多顶点之间不存在路径。

上图虽然不是一个连通图,但它有多个连通子图:0,1,2顶点构成一个连通子图,0,1,2,3,4顶点构成的子图是连通图,6,7,8,9顶点构成的子图也是连通图,当然还有很多子图。我们把一个图的最大连通子图称为它的连通分量。0,1,2,3,4顶点构成的子图就是该图的最大连通子图,也就是连通分量。连通分量有如下特点:
1)是子图;
2)子图是连通的;
3)子图含有最大顶点数。
注意:“最大连通子图”指的是无法再扩展了,不能包含更多顶点和边的子图。0,1,2,3,4顶点构成的子图已经无法再扩展了。
显然,对于连通图来说,它的最大连通子图就是其本身,连通分量也是其本身。

电路的一道好题目——积分放大器

题目是这样的

At the time the double-pole switch in the circuit shown in Fig.10 is closed, the initial voltages on the capacitors are 12V and 4V, as shown. Find the numerical expressions for v0(t) vf(t) v2(t) (as long as the ideal op amp operates in its linear range.)

若没有ideal的条件,则如我括号里所作。它是一个从电压比较器到电压跟随器再到电压比较器的过程。所以可以通过始末状态的V来判断三要素。最后通过三要素解决问题。、

而若有ideal的条件,有两种方法。这个最直观的名称就是积分减法器。想想用op amp的减法器,只不过这里把电阻换成了电容。而电容和电阻串联就是一个积分器。这在模电里头相当有用。按照这个想法,就能搞定了。

-------------------------更新 2019.4.2-----------------------------

用multisim确实是线性的。是对两者相减的积分。

Some words from people who has been ahead of me

表面上的努力都是无谓的,最重要的是灵魂的提升。
SICP,CSAPP,CLRS,OSTEP,EoPL,PRML*书读好了没
语法都不是最难的,最难的是算法。
能想出算法的都是神

从今以后,我只爱学习和运动

同学的10km,我以前没做到过吗?我喜欢的跑步,从来应该紧随我。做一个好自己。