最近社團蠻多新人加入,然後有遇到一些問題,想說來跟大家分享一些小技巧
P.S. 題目裡給的sample 通常與送出時測試的測值會不同,通過 sample 不代表一定會 AC !
通常題目會有幾種方法來輸入
題目會先講先輸入t,就是執行t次
接下來會輸入n,接下來的測值數量
通常,sample架構會長這樣:
t
n a1 a2 .....an
n2 b1 b2 .....bn2
....總計有t排(不包含t那行)
程式碼:
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)//執行t次
{
int n;
cin>>n;
vector<int> v(n);//建立一個 含有n數量的陣列
for(int i=0;i<n;i++)
cin>>v[i];
}
}
題目沒有講說什麼時候結束
放在同個sample裡(如果要你輸入一次就結束,那會有sample1 、 sample2 每個sample 都只會有一項測值)
sample input架構大概長這樣:
a b
d e
f g
output :
a1
b1
c1
程式碼:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)//當友直輸入時就無限執行,直到程式結束(EOF)
{
//你的程式碼
}
}
sample 有一長串,跟EOF型很像但結尾遇到 0 時結束
sample input架構大概長這樣:
a b
a2 b2
a3 b3
0
程式碼:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(1)//死迴圈
{
cin>>a>>b
if(a==0)//當輸入 0 時,結束迴圈
{
break;
}
//你的程式碼
}
}
因為有些題目給的sample會誤導你往錯誤的方向想
有可能成為解題關鍵!?
總比直接抄別人的code好