关于新人如何优雅的使用北航OJ | Eternal_zttz

关于新人如何优雅的使用北航OJ

使用北航OJ也快一年了,在这里说一说自己的使用经验:

C基础

数据类型:一般会用的比较多的主要有int,long long,unsigned long long,double,char
如果可以的话,记住:能long long不int,能double不float

格式化输入输出:具体你们可以看这个,当然也可以看看其他的资料。

大一的上机,主要以一般的循环和判断为主,所以,要好好掌握循环判断的写法:
for 循环:

1
2
3
for (int i = 0;i <= n;i++){
... ...
}

while 循环:

1
2
3
while(n--){
... ...
}

if else语句:

1
2
3
4
5
6
7
8
if(a){
... ...
}
else(b){
... ...
}else{
... ...
}

switch语句:

1
2
3
4
5
6
switch(a){
case 1: ... ...;break;
case 2: ... ...;break;
case 3: ... ...;break;
default: ... ...;break;
}

好好掌握循环和判断的写法,剩下的就是要将你们的思路用代码的方式表示出来。

关于OJ:

首先,对于新人,在进行第一次北航OJ评测的时候,请务必看一看简介,简介很重要,有关上机的常见错误类型这里都有所提到,且常见写法也会有所提及:

这里谈一谈上机的常见错误类型

WA:代表答案错误,这个错误是最常见的,十次提交九次WA的情况很正常,这个时候就要检查自己的算法是不是有问题了, 自己的思路哪里错了,必要的时候可以试着手动模拟一遍小数据。
当然,还有可能是溢出了,如果可以的话,能longlong 不int

CE:编译错误,需要检查代码,不过一般提交前会在自己电脑上跑一遍,出现几率较小

Runtime errot: 运行时错误,一般是数组开小了,访问了不存在的区域,试着把数组开大一点,如果需要开很大的数组,比如开10000000的数组,那么最好设置为全局数组,函数内开不了太大的

对于把大数组开在函数内部的同学:函数内部声明的变量默认存储于栈空间,函数的栈空间是比较小的;全局变量是放在堆空间的,堆空间相对较大一些;如果超出了栈空间限制,程序运行会崩溃。所以,递归层数太多、函数内部开大数组等行为会导致运行时错误。—-来自16级助教

其他的错误类型记得看简介,简介里所有的错误类型都有说明。


可能你们会遇到一些题目提示的是输入多组数据,例如这个:
1

这个没有提示输入的是多少组数据,写法如下:

1
2
3
4
5
6
7
while (scanf ("%d%d%d",&n,&m,&k)!=EOF){
... ...
}
//或者这样写
while (~scanf ("%d%d%d",&n,&m,&k)){
... ...
}

关于OJ的评测:

OJ的输入评测和输出评测是分开的,也就是说, 输入数据和输出数据是在两个不同的文件中评测,所以,可以不用将全部数据都输入了才输出结果,边输入边输出也是可以的,只要对于一组输入数据,输出结果正确就行,这样写还可以减少debug的难度,因为用数组存答案,中间可能会有bug风险。

例如:
对于脸熟赛B题,它给的输入和输出案例是这个:
2
但是,你可以在输入10 5 后就输出它的答案35,而不是等到所有输入数据都输入后才输出。

可以如下所示:
3

查重

对于提交的代码,助教会查重,所以,不要复制别人的代码,简单的改改变量是没用的,具体机制我也不是很清楚,不过还是自己好好写,最起码,自己要知道为什么这么写,课后能够独立写出来。

关于OJ基本坑不止这些,多上机,多刷题以后就会知道了(脸黑)。

脸熟赛A~H (个人)题解:

题目不难,自己多模拟几遍,掌握好基本知识接下来的上机问题就不大。
A . 宋老师的见面礼

1
2
3
4
5
#include <stdio.h>
int main(){
printf("Happy National Day!\n");
return 0;
}

B. 计算器

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
for (int i = 0;i<n;i++){
long long a,b;
scanf("%lld%lld",&a,&b);
printf("%lld\n",a*b-a-b);
}
return 0;
}

C. 哦~我的女神!

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
#include<math.h>
int main(){
double x1,x2,y1,y2;
double dis;
scanf("(%lf, %lf) (%lf, %lf)",&x1,&y1,&x2,&y2);
dis=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
printf("%.3lfm\n",sqrt(dis));
return 0;
}

D. 血小板(emmm,这题真的不想写)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include<stdio.h>
int main(){
printf("/\\::::::::,,,,,,:::,,:,,,,,,,,::,::,,,,,,:::,,:::,:,,:,:,::,,,::,,/\\\n");
printf(":::::::::::,,,:,::::,:,,,,:,:::,,:,,,:,:,,:,,,,:::::::,::::,,:,,,,,,\n");
printf("::::::::::::,,:,:;:,:,,,,,,+: `, ` `.::,:,,,,,::::::::,::,::,,,,,,,\n");
printf(":::::::::,,,::::::,:,,,,:: `` `````````:,,,,,:::::::,::,::,,,,,,,,\n");
printf("::::::,:::;;:,,,::,,:::; ````, ``.`` ` `` :,:,::::,::,:::,:,,,,,,,,\n");
printf(";;;:;;::,:,,:,:,::,:::.``` `` ` ` ` ` ':::::::::,:,,,,,,,,,,,\n");
printf("::,:::::::::::,,,:,:,```` ```` ` `,` `` `..:,,,,:::,,,,,,,,,,,,\n");
printf(":::::::::::,,:::::,, ```` .```,``` ````` ,.,,:::,,,,,,,,,,,,:\n");
printf(":::::::::::::,:::::` `````, + ` `'` `:`,;,,,,,,,,,,,,,,:\n");
printf(":::::::::::::,,::,.```` `` ```:`+` `.`` ;` ```` `.` #,:,,,,,:,,,,,:\n");
printf(":::::::::::::::::, `````` `` . ;`,```` . `' ````,` ,::,,:,,,,,:,\n");
printf(":::::::::::::::,.` `` ``` ```'`. `+`` .::'; `. .,:,,::,,,,,,,\n");
printf(":::::::::::,:;:```` '`.``;```+ ``.# `+' .,` ` ````` ,,,,,:::,,,,\n");
printf("::::::::::::::.`` ` ` ',`+` `: , .`# `;,`` . ```.```;:,,,,,,:::,\n");
printf("::::::::::,:.`,``````:#``;`, `, .`+`````+`' ., ' ```.```;,,,,,,,,,::\n");
printf("::::::::::; ` . ```; `,`# ` `; . #` ` `+`'.::````.``.;,,,,,,,,,:,\n");
printf(":;:::::::;` ` ` `` .``#`; `.` .`+````` `:`,`#` ```.` ,::,,,,,,,,,:\n");
printf(":;:::::::. ` ````` .`+ `, ``` # `,``` `` ; :` ``.``:,:,,,,,,,,::\n");
printf(":;:::::::` .`` ``````'`. . #:`` ``:``` `` `'` .``,.,:,,,,,::::\n");
printf(":;::::::; .`` ``````#` ``' ` ```: `,` `` ````: ` ,`:,,,,,:,:::\n");
printf(":;::::::;``, ` ```` , `+ ```,, `,, ` ```` ` ``::,,,:,:::,\n");
printf(":;:::::::` .` ``` ``+ `` ` :. `.` `` `` ``.: ```` `` ,,,,,,::,::\n");
printf(":;::::::, `.` ``` ```,`` , `` ` `` ````` ` ``, `` ````,:,,,;:,:,:\n");
printf(":;:::::::` ```. ``` ` , ` `,:,````,:,` ` ```,` .` ,,,,:,,:,::\n");
printf(":;:::::::;`` `.`` `` ` , ` :. ` ``` `` ```:`````::`` .:,,:,:,::::\n");
printf(":;::::::::` ` .````` ``., ````` ` ``` `````.,``:``.`,;::::::::,:\n");
printf(":;:::::::::`. `` ` `` .` ```````` ``` ``` `` `````::;,,,,,,::::\n");
printf(":;:::::::,: : `. ``` ``` ``````` `.,::,.`` ` ` ` #;,,,,,,,,:\n");
printf(";;;;;;;;::: ` `.` ` ` `:`````` `,` ` ``` ` `,. `` `` ```,:,,,,,,,,\n");
printf("::::::::::: ` ````` `;``````,` ``.'+'':+,:;;.` ,. ``````,,,,,,,,,,\n");
printf(":::::::::::``, `:````,``````.`` :#++'''';'..,:,,:;``,.` ```:,,,:,,,,\n");
printf(":::::::::::` , ``#:. ``````,``:''''+'''+;'+++#+#,:'';``,` +::,,,:,,\n");
printf(":::::::::::: ,`` ````` :,.:#+'+'''+;';+++#;#,'';'': ``;,,,,,,,,\n");
printf(":::::::::::'``. ``````` , '##@+,+';+#'''':, '+:#;,';;;;:+:,,,,,,,,,\n");
printf(":::::::::::: .;```````: @#++@..,'','+''':, `++`#,#;;:;::,,,,,,,,,,\n");
printf(":::::::::::::`` `````.;@+ '##:..++.,+++':, '++`+;.:::;::::,,,,,,,,\n");
printf(":::::::::::::;';,```.`:,#, `;##...'..;,+'..#'#++ :@ :;::::::,,:,,,,,\n");
printf("::::::::::::::,,:;`.+;.+':` `#+`..::..,;'..++#+; : :::::+',:,,,,:,,\n");
printf("::::::::::::::,,,:::;;.#::,` #+:..,#,...@`.'+#+: : :,':,+',:,:,,,,,\n");
printf(":::::::::::::;::::;,::`#`.#@@##+````````````;#+:`,``+:':.'',,,,,,,,,\n");
printf(";:;;;'';;;:::',:;,:::;`#. ''###'```` ```````;..: '';:.',',,,,,,,,\n");
printf(",;:,:::::::::';;:,::,:`'+`+;++#'```````````` ':``` `:;;:,',;,:,,,,,,\n");
printf(",::,:,:::::::';':+:'::,.+ ::#+:'``````````````:,.``,,';::;::,,,,,,,,\n");
printf("::,,:::::::::':::',::,;``:`;,,,,```````` `````````. ';:;:,,,:,,,,,:\n");
printf(",:,,,;,::::::+:,:'':;,:``` `:;;````````````````````` ';:',,,,,,,,,,,\n");
printf(",::,:,::,:::,+:;'::'::::`.``..,```````````````````` ';:+,,,,,,,,,,,\n");
printf(":,:,,,,;:,:::':;:,::':::'```````````````````````` ;;;#::,,,,,,,,,\n");
printf(",,,,,,,,::,,:':;#;;:;+;;+;``````````````````````` ;;,':',,,,,,,,,\n");
printf(",,,,,,,,:;::,;:''+':::##; ``.``````````````````` ::;,';+:,,,,,,,,\n");
printf(",,,,,,,,,,:,,;:;''+:;:;,,#.````````````````````` ` '::,'::::::::,:,\n");
printf(",,,,,,,,,,,::;;'''':::;..`,``````````` `.,,.```` `';::;';.,,,,,,:,:\n");
printf(":::,,,,,,,,::;;;''''::,;,.```````````:````````` ,+;::''':;:,,,,,,,\n");
printf("::,,,,,,,,,,;';''''+;;,'...``````````````````` `.+';+,+'':':,,,,,,,\n");
printf("::::,,:,,,,,;';'+''';::'',,.`````````````````` ..,,;;:;;+;;::,,,,,,\n");
printf(":,,,,,,,,,:,'';''++'+,,:++:,.````````````````` .,:.:;;:` `',:,,,,,,,\n");
printf("::::,,,,,,,:+';'+'+++;;,##,'..````````````````,,,'`;';;```'.#:,,,,,,\n");
printf("::::,,,,,,,,'''+++#'+++;',,;,',.`````````````,.'`:`'+;;` ```.:,,,,,,\n");
printf("::::,,,:,,,,''''+++#'''',.,:.,.:;`.``````` :;.,.``'':,.``````.::,,,\n");
printf("::::,,,,,,,,++'+++'++`.;;',,.,,.,.::,``````',',```:';:.```````,,,,,,\n");
printf("::,,,,,,,,,:++;+++,.```'+:,,;,.....,,..,,.,.,:````:+;;;```:.```;,,,,\n");
printf(",,,,,,,,,,,:++'+:```````';',,;..............,,``.`,+;' .`:`````.,,,,\n");
printf(",,,,,,,,,,,,+'+;`````````':,,:;,,............`` ``,#;;``:```````::,,\n");
printf(",,,,,,,,,,,,':'.`````````:+;`:,:..,,........`````.,#;;`;```````.::,,\n");
printf(",:,:,,,,,,,,+;.```````````'+```...,........````'``:';.``````',`.`,,,\n");
printf(",,::,,,,,,:,+:``.`,:``````.;+``. .,......````` ``,;'`:````;,,```;,,\n");
printf(",,,,,,,,,,:,+````````:``,`..':`..````````````.:````,;`;``.`,,.````,,\n");
printf(":,,,,,,,,::::``````````,...`#`,`.``:`````````:```:``:`;``.'.,`````:,\n");
printf(":,,:,:;:::,,;.`````````.'`;``````````:, `` .```.'```;`:``.,,```````:\n");
return 0;
}

E. 萌萌的ASCII码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(){
int a;
scanf("%d",&a);
if(a<=47||(a>=58&&a<=64)||(a>=91 && a<=96)||a>=123)
printf("other character\n");
else if(a>=48 && a<=57)
printf("digit\n");
else if(a>=65 && a<=90)
printf("uppercase\n");
else
printf("lowercase\n");
return 0;
}

F. 原力鉴定

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(){
char a ;
int num =0 ;
while(scanf("%c",&a)!=EOF){
if(a == 89)
num++;
if(a=='\n')
break;
}
printf("%d\n",num);
return 0;
}

G. 整数除法

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main(){
long long a,b;
scanf("%lld %lld",&a,&b);
if(b == 0)
printf("error\n");
else if(a*b >=0||a%b == 0)
printf("%lld\n",a/b);
else
printf("%lld\n",a/b-1);
return 0;
}

H.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<math.h>
int main(){
int n;
scanf("%d",&n);
long long a[n+1];
long long A[n+1],B[n+1];
A[0] = 0;B[0] = 0;
for (int i = 1;i<=n;i++){
scanf("%lld",&a[i]);
A[i] = A[i-1]+a[i];
B[i] = B[i-1]^a[i];
}
printf("%lld\n",A[n]|B[n]);
return 0;
}

祝刷题愉快!

-------------The End-------------