臺中一中39th電研社教學
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: C++教學講義-第四週 slideOptions: # 簡報相關的設定 theme: blood # 顏色主題 transition: 'slide' # 換頁動畫 --- TCIRC C++教學講義 === >[name=張皓凱、沈奕呈][time=Nov 12,2021] ###### tags:`tcirc` `社課` `C++` `台中一中電研社` --- 第四節社課 === [TOC] --- ## 電研 講義都會公告在社網上 [社網:tcirc.tw](https://tcirc.tw/) [IG:tcirc_39th](https://www.instagram.com/tcirc_39th/) ![ig QRcode](https://i.imgur.com/4hyS6GM.png) [online judge:judge.tcirc.tw](https://judge.tcirc.tw/) --- ## 解題方法 以下的解題方法僅供參考,並不是唯一的方法。 解題主要可以分為五個部分,分別為: 1. 讀題 2. 輸入 3. 儲存 4. 運算 5. 輸出 以下將分步驟講解 --- ### 1.讀題 將題目全部讀過一次,把題目希望你做的東西抓出來,有些題目會有題目故事來混淆,通常故事內容不重要,把關鍵字句抓出來就好。這時就可以開始想需不需要用資料結構或演算法。 ---- 原本的題目 ![](https://i.imgur.com/hTsEM4g.png) ---- 標完重點的題目 ![](https://i.imgur.com/aIrOiB5.png) --- ### 2.輸入 一般題目的輸入種類可以分為兩種 1. 有說接下來有幾行,每行有幾幾筆資料 2. 輸入直到EOF(常見於多筆測資) 分別對應的解決方法為 --- #### [1. 有指定](https://judge.tcirc.tw/ShowProblem?problemid=c014) * 先輸入接下來的行數、字數,存在變數裡 * 利用for loop來輸入每一個資料 * 如果需要輸入一整行包含空格的字串記得使用`cin.get`或`getline(cin,)` --- #### [2.EOF](https://judge.tcirc.tw/ShowProblem?problemid=a001) * 直接使用`while(cin)` --- 題目通常會給每個資料的範圍,是用來決定資料型態和算複雜度會不會TLE(time limit exceeded),常見的範圍有以下: * $-2^{31}<x<2^{31}-1$:代表可以用int存不會溢位 * 會超過32位元整數、但不超過64位元整數:需要使用long long才不會溢位 --- ### 3.儲存 輸入進來的資料可以存在數字、字元、字串、陣列。又或者是在儲存時需要用到不同的資料型態。 * 數字:輸入的單筆資料是數字 * 字元:輸入的單筆資料是字元 * 字串:輸入的資料是字串 * 陣列:輸入多筆相同型態、有關係或連續的的資料 * 資料結構:樹、圖、陣列 --- ### 4.運算 程式最主要的部分都發生在這裡,通常簡單的題目會直接說要做什麼,困難一點的的題目在文字中可以大概找出方向,再更進階的就需要用到演算法。剩下的就要靠不斷的刷題來判斷了。 --- ### 5.輸出 輸出一般都不會太刁鑽只需要特別注意是以空格還是換行分隔。能用複製的字串就用複製的,不然大小寫、空格、拼字...很容易錯。 --- ## 除錯 程式碼錯誤分成兩種:編譯錯誤(compile error,CE)和執行錯誤(runtime error,RE)。編譯錯誤是指程式碼有語法上的錯誤,編譯器會跳出錯誤而無法執行,執行錯誤是執行時出的錯誤,編譯器無法偵測,程式可以正常執行,只是結果會不同。而除錯在寫程式中是非常重要的一部分。 --- ### 編譯錯誤 常見的編譯錯誤有以下(不同的編譯器有不同的寫法,但都大同小異,以下以code blocks為例): * 沒加分號:`error: expected ';' before ''` ⇒ 加上分號 * 忘記include:`error: '某個函式名稱' was not declared in this scope` ⇒ 加上`#include` * 忘記宣告:`error: '某個變數名稱' was not declared in this scope` ⇒ 在前面加上變數宣告 其他的錯誤如果看不懂可以直接複製貼上到google查,stack overflow幾乎甚麼都有。 --- ### 執行錯誤 這類的錯誤最麻煩,如果程式跑出來跟預期的不一樣可以看看是不是有執行錯誤的發生。 * 讀取超出陣列大小的index * 除以0 * 無限while,for loop,遞迴無終止條件 * 溢位 要找出這種錯誤可以透過印出變數值來看是否發生了以上的狀況。(vscode左邊就有附帶除錯功能) --- ## [b072: 加法](https://judge.tcirc.tw/ShowProblem?problemid=b072) ```cpp= #include <iostream> #include <string> using namespace std; int main() { string a,b; getline(cin, a); getline(cin, b); long long num1=0,num2=0; for(int i=8;i<=a.size()-2;i++){ num1*=10; num1+=a[i]-'0'; } for(int i=8;i<=b.size()-2;i++){ num2*=10; num2+=b[i]-'0'; } cout << "sum = " << num1+num2; } ``` --- ## [b073: 節約簡訊](https://judge.tcirc.tw/ShowProblem?problemid=b073) ```cpp= #include <iostream> #include <cstring> using namespace std; int main() { int n; cin >> n; string s; cin >> s; string after=""; char last=s[0]; int cnt=1; for(int i=1;i<n;i++){ if(s[i]==last){ cnt++; }else{ after+=to_string(cnt); after+=last; last=s[i]; cnt=1; } } after+=to_string(cnt); after+=last; if(s.size()<=after.size()){ cout << s; }else{ cout << after; } } ``` --- ## [b027: 找出學霸](https://judge.tcirc.tw/ShowProblem?problemid=b027) ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; int hiid,hi=0,loid,lo=100; for(int i=1;i<=n;i++){ int score; cin >> score; if(score>hi){ hiid=i; hi=score; } if(score<lo){ loid=i; lo=score; } } cout << hiid << " " << hi << " " << loid << " " << lo; } ``` --- ## 題目練習 [b068: 蛇蛇矩陣](https://judge.tcirc.tw/ShowProblem?problemid=b068) [b030: 健康檢查(Ⅰ)](https://judge.tcirc.tw/ShowProblem?problemid=b030) [b069: 字串串串串串接](https://judge.tcirc.tw/ShowProblem?problemid=b069) [b053: 正確的數字](https://judge.tcirc.tw/ShowProblem?problemid=b053) --- ## [b020: 慎思湖的倒影](https://judge.tcirc.tw/ShowProblem?problemid=b020) ---- #### 分析時間: 要點:將輸出內容分成兩部分來看--上半部、下半部 - 上半部: 第一層重複cout一次 `*` 第二層重複兩次...重複l層,每層重複l次 ->雙重迴圈 - 下半部 第一層重複cout l次 `*` 第二層重複l-1次... ->和上半部方法一樣,但將第二層迴圈的變數改為有l遞減到1 ---- ```cpp= #include <iostream> using namespace std; int main() { int l; cin >> l; for(int i=1;i<=l;i++){ for(int j=1;j<=i;j++){ cout << "*"; } cout << "\n"; } for(int i=1;i<=l;i++){ for(int j=l-i;j>=0;j--){ cout << "*"; } cout << "\n"; } } ``` --- ## [b067: 位數和](https://judge.tcirc.tw/ShowProblem?problemid=b067) 妥善利用`/`和`%`這兩個運算子解題 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; while(n>=10){ int sum=0; while(n>0){ sum+=n%10; n/=10; } n=sum; } cout << n; } ``` --- ## [a002: 電話客服中心](https://judge.tcirc.tw/ShowProblem?problemid=a002) ```cpp= #include <bits/stdc++.h>//懶人包 using namespace std; string ss; int main() { while(cin>>ss){ int sum=0; int letter[26]{10,11,12,13,14,15,16,17,34,18,19,20,21, 22,35,23,24,25,26,27,28,29,32,30,31,33}; //規則一:紀錄每個英文字母對應的前兩位數字 for(int a=0;a<8;a++)//規則二(還沒加英文字) sum =sum+(ss[a]-'0')*(8-a); int c=(ss[8]-'0');//c=檢查碼 for(int a=0;a<26;a++){//找那些英文字符合 int letter_=((letter[a]%10)*9)+(letter[a]/10);//規則二 int m=(sum+letter_)%10;//規則三、四 if(((10-m)%10)==c) cout<<char(65+a);//可寫成'A'+a } cout<<endl; } return 0; } ``` --- ## [b074: 史萊姆](https://judge.tcirc.tw/ShowProblem?problemid=b074) ```cpp= #include<iostream> using namespace std; int main(){ int N,T; while(cin>>N>>T){ int NCount=N,TCount=0,day=0; while(TCount!=T){ TCount*=2; TCount+=N; day++; } cout<<day<<'\n'; } } ``` --- ## 題目練習 [b071: 電梯 (Elevator)](https://judge.tcirc.tw/ShowProblem?problemid=b071) [b070: 回文數](https://judge.tcirc.tw/ShowProblem?problemid=b070) [b075: 一元一次方程式](https://judge.tcirc.tw/ShowProblem?problemid=b075) https://hackmd.io/@tcirc39th/HJifM35wY

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully