Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    資源
    概覽術語表系統提示詞
    提示詞庫宇宙按鍵企業預言家網站精靈Excel 公式專家Google Apps 腳本編寫器Python 除錯大師時光旅行顧問故事創作夥伴引用來源SQL 魔法師解夢師雙關語大師美食創作家混成詞詩人幽默助手 HalLaTeX 達人情緒色彩師Git 高手比喻達人倫理困境導航會議記錄員慣用語解析器程式碼顧問函式製造器新詞創造者CSV 轉換器Emoji 編碼器文章潤色師多角度思考者冷知識產生器正念導師二年級簡化器VR 健身創新者PII 淨化器備忘錄大師職涯教練評分大師繞口令面試問題設計師文法精靈猜謎語程式碼解說員外星人類學家資料整理器品牌建構師效率評估器評論分類器方向解碼器勵志繆思電子郵件擷取器首席審核員課程規劃師蘇格拉底智者頭韻煉金師未來時尚顧問多語言超能力產品命名專家哲學沉思試算表魔法師科幻情境模擬器自適應編輯器巴別塔廣播推文語氣偵測器機場代碼分析師
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    提示詞庫

    效率估算器

    計算函式和演算法的時間複雜度。

    將此提示複製到我們的開發者 Console 中親自試試看!

    內容
    System您的任務是分析所提供的函式或演算法,並使用大 O 表示法計算其時間複雜度。逐步解釋您的推理過程,描述您如何得出最終的時間複雜度。在確定時間複雜度時,請考慮最壞情況。如果函式或演算法包含多個步驟或巢狀迴圈,請提供每個步驟的時間複雜度,然後給出整個函式或演算法的總體時間複雜度。除非另有說明,否則假設所使用的任何內建函式或操作的時間複雜度為 O(1)。
    Userdef example_function(n): for i in range(n): print(i) for j in range(n): for k in range(n): print(j, k)

    範例輸出

    為了分析給定函式 example_function(n) 的時間複雜度,我們將其分解為多個步驟並考慮最壞情況。

    步驟 1:分析第一個迴圈

    for i in range(n):
       print(i)
    

    此迴圈迭代 n 次,每次迭代執行一個常數時間操作(print(i))。因此,此步驟的時間複雜度為 O(n)。

    步驟 2:分析巢狀迴圈

    for j in range(n):
        for k in range(n):
            print(j, k)
    

    外層迴圈迭代 n 次,每次迭代中,內層迴圈也迭代 n 次。在內層迴圈的每次迭代中,執行一個常數時間操作(print(j, k))。因此,巢狀迴圈的時間複雜度為 O(n * n) = O(n^2)。

    步驟 3:合併時間複雜度
    函式 example_function(n) 由兩個步驟組成:第一個迴圈和巢狀迴圈。總體時間複雜度是每個步驟時間複雜度的總和。

    第一個迴圈的時間複雜度:O(n)
    巢狀迴圈的時間複雜度:O(n^2)

    為了確定總體時間複雜度,我們考慮主導項,即增長階數最高的項。在此情況下,O(n^2) 主導 O(n)。

    因此,函式 example_function(n) 的總體時間複雜度為 O(n^2)。

    在最壞情況下,當 n 很大時,巢狀迴圈將主導執行時間,函式將具有二次方時間複雜度。


    API 請求

    import anthropic
    
    client = anthropic.Anthropic(
        # defaults to os.environ.get("ANTHROPIC_API_KEY")
        api_key="my_api_key",
    )
    message = client.messages.create(
        model="claude-opus-4-6",
        max_tokens=1000,
        temperature=0,
        system="Your task is to analyze the provided function or algorithm and calculate its time complexity using Big O notation. Explain your reasoning step by step, describing how you arrived at the final time complexity. Consider the worst-case scenario when determining the time complexity. If the function or algorithm contains multiple steps or nested loops, provide the time complexity for each step and then give the overall time complexity for the entire function or algorithm. Assume any built-in functions or operations used have a time complexity of O(1) unless otherwise specified.",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "def example_function(n):\n    for i in range(n):\n        print(i)\n\n    for j in range(n):\n        for k in range(n):\n            print(j, k)"
                    }
                ]
            }
        ]
    )
    print(message.content)
    

    Was this page helpful?

    • API 請求