Jerry's Blog

Recording what I learned everyday

View on GitHub


5 August 2019

Database(3) -- ER Graph

by Jerry Zhang

LeetCode Day 31: P202. Happy Number (Easy)

题目:

给一个正整数,求各个数字位的平方和,得到一个新的正整数,再继续求各个数字位的平方和。重复这一过程,直到最后结果为1或者进入死循环。 如果结果为1,那这个数就是Happy Number。

答案:

public class E_202_HappyNumber {
    public boolean isHappy(int n) {
        if(n<=0) return false;
        while(true){
            int sum=0;
            while(n!=0){
                sum+=(n%10)*(n%10);
                n=n/10;
            }
            if(sum/10==0){
                if(sum==1||sum==7) return true;
                else return false;
            }
            n=sum;
        }
    }
}

In order to find a rule to break out the loop, I start calculating 2 and find a loop at 4, then 3,5,6,9 will all go into that loop. So in 1-9, only 1 and 7 are happy numbers. Also I find all numbers’ calculation will goes into a single digit at some time. So what I did is just calculate happy sum and when it is a single digit, check if it is 1 or 7 ^.^

在小于10的正整数中,只有1和7是happy number。所以就不断循环,直到求和结果为小于10的正整数,再看是不是1或者7即可判断。

Database:

Should use waterfall model

Databases affect the whole project very much, so we should design the database carefully.

Should not use spiral model.

Entity Relationship Diagram

属性

矩形表示实体,椭圆表示属性。

实体关系

ER1

一对一,一对多,多对多。用菱形表示关系,连接一个或多个关连属性。

tags: Database