---
title: 程式競賽練習網站-CodeForeces 入門
description:
image:
langs: ch
tags:
- 程式競賽
---
# 程式競賽練習網站-CodeForeces 入門
以下是維基百科對 CodeForces 的介紹
>Codeforces是一個提供線上解題系統的俄羅斯網站。[2]該網站由一群來自ITMO大學的程式設計師建立並維護,其中主要的領導者為Mikhail Mirzayanov。自2013年起,Codeforces的活躍使用者數就已經超過了另一大線上解題系統網站TopCoder。[3]截止2018年,該網站已經擁有超過600,000的註冊使用者。[4]
>[name=維基百科:Codeforces]
CodeForces 基本上就是超大型刷題網站,題目都是英文的雖然會對非母語族群的門檻較高,但是在這裡可以透過刷題快速增進你的程式能力。
:::info
**線上解題系統**
線上解題系統會提供很多程式設計的題目,並且讓你可以透過系統的Judge System來檢核你所撰寫的題目是否正確。
國內:
- Zero Judge
- Green Judge(已關閉)
國外:
- LeetCode
- CodeForces
:::
## 註冊帳號
進入 CodeForces 註冊帳號的頁面,把基本的資料撰寫完畢後送出。也可以直接透過 Gmail 登入。
- [註冊帳號](https://codeforces.com/register)
- [Gmail登入](https://codeforces.com/enter/Gmail)

各個欄位要填寫的東西:
- Handle:帳號暱稱
- Email:電子郵件
- Password:密碼
- Confirm Password:密碼
## 開始挑戰題目
註冊完帳號過後就可以開始嘗試去挑戰裡面的題目了。可以進入Problem Set頁面後,點擊表格中的閃電(代表困難度),把他的排序調整成由低到高。
- [PROBLEMSET](https://codeforces.com/problemset?order=BY_RATING_ASC)
接著以**4A. Watermelon**示範要如何在CodeForces中去解題。
### 解析題目
**#️⃣標題:** 4A. Watermelon
**📑標籤:**`brute force`, `math`
**⚡困難度:** 800
首先我們可以在標題底下看到幾行比較小的字
>time limit per test:1 second
>memory limit per test:64 megabytes
>input:standard input
>output:standard output
上面分別告訴了你這一題程式的限制:
- 每一筆測資的時間限制:1 秒
- 每一筆測資的記憶體大小限制:64 MB
- 測資輸入:standard input
- 測資輸出:standard output
稍微看過了對程式碼執行的限制過後就可以開始解題了。我們接下來就開始剖析題目。
>One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.
>
>Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.
上面的題目大意是在講:
- Pete 跟 Billy買了一顆 $w$ 公斤重的西瓜回家
- 他們要把西瓜分成兩半,而且兩半的公斤數都要是偶數
- 你要撰寫一個程式可以幫助他們判斷他
在閱讀完題目內容後,我們就會去看題目給你的輸入輸出格式:
>**Input**
The first (and the only) input line contains integer number $w (1 ≤ w ≤ 100)$ — the weight of the watermelon bought by the boys.
>**Output**
Print `YES`, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and `NO` in the opposite case.
我們就可以知道題目要求的輸入輸出個是大致是:
- 輸入一個數字 $w (1 ≤ w ≤ 100)$
- 如果可以被切成兩個偶數公斤數的兩半,印出`YES`,反之印出`NO`
#### 範例程式碼
```java
// CodeForces 4A
// AC | 530 ms | 1600 KB
import java.util.Scanner;
public class c_4a {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (n > 2 && n % 2 == 0) {
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
```
### 提交程式碼
撰寫完程式碼後,就可以把自己的程式上傳到系統裡面去測試。在題目的頁面稍微往下滑一點,會看到有一個 `Submit?` 的欄位,把程式碼上傳上去後,選擇你所撰寫的語言,就可以送出了。

### 程式碼批改
在提交完程式碼後,會跳轉到Contest Status頁面。上面會有你程式碼的紀錄(#)、提交的時間(When)、是誰提交的(Who)、哪個題目(Problem)、撰寫的語言(Lang)、批改的結果(Verdict)最後是程式碼的執行時間(Time)以及記憶體消耗(Memory)。

:::info
**批改結果**
大部分的線上解題系統所提供的批改結果會有以下幾種:
- AC: Accept 即表示通過
- NA: Not Accept 表示在多個測資點的情況下,有部分測資點無法通過
- WA: Wrong Answer 表示答案錯誤, 請仔細比對,務必符合題目要求
- TLE: Time Limit Exceed 表示執行超過時間限制
- MLE: Memory Limit Exceed 表示程序超過記憶體限制
- OLE: Output Limit Exceed 表示程序輸出超過限制
- RE: Runtime Error 表示執行時錯誤,通常為記憶體配置錯誤 如:使用了超過陣列大小的位置
- RF: Restricted Function 表示使用了被禁止使用的函式,並在錯誤訊息中指明使用了什麼不合法的函式。
- CE: Compile Error 表示編譯錯誤
- SE: System Error 包含 Compile, Runtime 等未定義的錯誤均屬於 System Error
可以根據系統所給出的結果進一步修改程式碼
:::
### 答案錯誤
當你很不幸的,所撰寫的程式沒有通過系統的測試。可以點開 `#` 欄位看到更詳細的批改結果。這邊以一次 CE(Compile Error)為例:

可以看到上面的 Check Log 會提程式編譯的資訊,我們就可以根據上面的指示去修改程式碼。
```diff
- int n = nextInt();
+ int n = s.nextInt();
```
## 結語
以上就是 CodeForces 基本的操作,也恭喜你完成了在 CodeForces 上的第一個題目,接下來還會有很多不論是困難或是有趣的題目等著你挑戰。最後也感謝你閱讀完這篇文章,如果有問題歡迎來聯絡我,我會盡量協助你。