我怎样才能提高效率呢?

时间:2015-09-25 00:59:00

标签: php mysql

#include <iostream>
using namespace std;
int main() {
    int n; // you don't need the values to be global
    int d=0;
    cin>> n;
    for(; ;){ // deleted i because it wasn't used
        if(n<=1){ // compare here, don't assign here
            cout<< d;
            break; // exit the loop
        }
        else if(n%2==0){
            d++;
            n=n/2; // please update n here
        }
        else{
            d++;
            n=n*3+1; // also please update n here
        }
    }
    return 0;
}

正如您所看到的那样效率非常低,并且仅出于某种原因才适用于第一个Temporal查询。有什么方法可以简化这个以提高效率并使值适用于每个查询?我没有收到任何错误。

1 个答案:

答案 0 :(得分:0)

此查询会为Branchs中不存在的每个组在$Username中插入一行。

INSERT INTO Branchs (Username, Name, Image, GroupID)
SELECT '{$gU->Username}', groups.Name, groups.Image, groups.GroupID
FROM (
    SELECT '983872' AS GroupID, 'Temporal Investigations' AS Name, 'Temporal' AS Image
    UNION
    SELECT '976892', 'Tactical Command', 'Tactical'
    UNION
    SELECT '986292', 'Applied Sciences', 'Science'
    ...
    ) AS groups
LEFT JOIN Branchs ON Branches.GroupID = groups.GroupID AND Username = '$Username'
WHERE Branchs.GroupID IS NULL

有关LEFT JOIN如何在UNION子查询中返回Branchs中没有匹配项的行,请参阅Return row only if value doesn't exist