Try   HackMD

練習賽題解

pA 簽到題(signed and unsigned)

出題者:YuDong
標籤:基本架構,輸出輸入,字串

真的是一題簡單簽到題,只要會寫基本架構跟使用 cout 就好
看一下隔壁的衣服背後應該可以知道答案:NFIRC

AC Code

#include <iostream> using namespace std; int main() { cout << "I Love NFIRC 1st.\n"; }

pB. 字數總和與首字語(Word Count and Acronym)

出題者:ShiYu
標籤:EOF 輸入,getline,字串,索引值

這題會遇到的問題在於如何重複輸入一整行句子直到無輸入為止
我們分兩個觀念

  1. EOF 輸入
  2. getline 讀取整行
    結合起來
string s; while(getline(cin,s)){ }

處理完輸入之後就可以對題目要求的操作
題目需要 字數總和
我們可以使用 size( ) 函數 求出字串的字元數加總到變數 sum 中

int sum = 0; // 記得要從 0 開始加總 sum += s.size();

題目還需要首字語 我們可以在每讀入一行時紀錄他的第一個字元

string ans; // 宣告一個空字串當最後縮寫的答案 ans += s[0]; // 使用字串的相接與索引值概念將第一個字元

AC Code

#include <bits/stdc++.h> using namespace std; int main() { string s,ans; int sum = 0; while(getline(cin,s)) { sum += s.size(); ans += s[0]; } cout << sum << "\n" << ans << "\n"; }

C. 歷史成績(History Score)

出題者:ShiYu
標籤:變數,最大值,四則運算,多重判斷式

這題運用到四則運算以及判斷式
輸入非常簡單就是四個成績

int a,b,c,d; cin >> a >> b >> c >> d;

再來要處理總成績 根據題目的自訂標準:

=×0.6 +×0.4

double score = max(max(a,b),c) * 0.6 + d * 0.4;

注意:需要使用 duoble 浮點數型態 因為是乘以小數點
如果用 int 這題只能拿到 80 分

最後處理判斷的部分 只需依照題目老師說的標準寫三個判斷式就好

if (score > 60) { cout << "You passed.\n"; } else if (score > 40) { cout << "You will be down.\n"; } else { cout << "You will be stunned.\n"; }

念出來可以發現與題目中老師說的話一樣

AC Code

#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d; cin >> a >> b >> c >> d; double score = max(max(a,b),c) * 0.6 + d * 0.4; if (score > 60) { cout << "You passed.\n"; } else if (score > 40) { cout << "You will be down.\n"; } else { cout << "You will be stunned.\n"; } return 0; }

D. 社恐分組(Social Group)

出題者:YuDong

首先要了解題意,然後直接實作即可。 by fishhh

已知班上有

m 個人,目前認識了
n
個人,自己的座號則是
s

這題有兩種做法,先說第一種。

  • 第一種就是直接篩選掉「認識的人的座號」+「自己的座號
    s
    」,其他的就直接輸出。
  • 第二種則是利用建表,用索引值代替座號初始化為
    0
    ,將認識的人的座號值設為
    1

    輸出陣列索引值
    1m
    中,值為
    0
    的座號即可。
    簡單來說將認識的人標記為
    1
    ,而
    0
    就會是我們不認識的,只要輸出標記
    0
    的座號即可。

AC Code
(第一種作法)

#include <bits/stdc++.h> using namespace std; int ar[45]; int main() { int m,n,s; cin >> m >> n >> s; for(int i = 0; i <= n; i++) cin >> ar[i]; sort(ar, ar+n+1); int p = 1; for(int i = 1; i <= m; ++i) { if(i != s) { if(i == ar[p]) p++; else cout << i << " "; } } cout << "\n"; }

AC Code
(第二種作法)

#include <bits/stdc++.h> using namespace std; int main() { int m,n,s; cin >> m >> n >> s; bool b[40] = {false}; b[s-1] = true; while(n--) { int t; cin >> t; b[t-1] = true; } for(int i = 1; i <= m; i++) { if(!b[i-1]) cout << i << " "; } cout << "\n"; }

E. 披薩超人(Pizza)

出題者:ShiYu
標籤:迴圈,枚舉,變數

這題銜接到下學期的演算法內容 運用到的是枚舉
可以根據題目中的提示推出原理
也就是枚舉各個硬幣分別取多少個
可以在簡報中查看過程

AC Code

#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long ans = 0; for(int i=0; i <= n / 50; i++) { for(int j=0; j <= (n - i*50) / 10; j++) { ans += (n - i*50 - j*10) / 5 + 1; } } cout << ans << "\n"; return 0; }