0%

第二章-暴力求解-计算机考研机试笔记

2.1 枚举

枚举是指对每个可能的解逐一进行判断,直到找到符合题目要求的答案。

采取枚举策略前,一定要分析枚举量。枚举量过大时,需要选择其他办法解决。即使题目适合枚举法,也要进行分析以减少无用的枚举

枚举类题目的特点:数据量较小时,可以逐个判断数据是否符合题目的要求。

例题2.1:abc

例题2.2:反序数

经典函数需牢记:

1
2
3
4
5
6
7
8
9
int Reverse(int x){	//求反序数
int revx = 0;
while (x!=0){
revx*=10;
revx+=x%10;
x/=10;
}
return revx;
}

例题2.3:对称平方数1(反序数的应用)

习题2.1:与7无关的数

我的答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
int n;
while(scanf("%d",&n) != EOF){
int sum=0;
for(int i=1;i<=n;i++){
if(i%7==0 || i%10==7 || (i/10)%10==7)
continue;
else sum+=i*i;
}
printf("%d\n",sum);
}
return 0;
}

习题2.2:百鸡问题

我的答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main(){
int n;
while(scanf("%d",&n) != EOF){
for(int i=0;i<=100;i++){
for(int j=0;j<=100-i;j++){
int k=100-i-j;
if(15*i+9*j+k<=3*n)
printf("x=%d,y=%d,z=%d\n",i,j,k);
}
}

}
return 0;
}

习题2.3:Old Bill

我的答案:

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
#include <iostream>

using namespace std;

int main(){
int n,x,y,z;
int total;
while(scanf("%d",&n)!=EOF && scanf("%d %d %d",&x,&y,&z)!=EOF){
int temp,tempi,tempj=0;
for(int i=9;i>0;i--){
for(int j=9;j>=0;j--){
total=10000*i+1000*x+100*y+10*z+j;
if(total%n==0)
if(total/n>temp){
temp=total/n;
tempi=i;
tempj=j;
}
else continue;
}
}
if(temp==0)
printf("0\n");
else printf("%d %d %d\n",tempi,tempj,temp);
}
return 0;
}

第一章-计算机考研机试指南笔记

评判结果

  1. 答案正确(Accepted)

  2. 答案错误(Wrong Answer):

    • 健壮性,考虑特殊输入,如边界数据、变量溢出
    • 正确性,考虑算法设计。
  3. 格式错误(Presentation Error):

    • 离正确答案不远,需要考虑空格,换行
    • 图形排版中离正确答案仍有距离
  4. 超出时间限制(Time Limit Exceeded):

    • 确定算法时间复杂度符合要求,可以检查程序是否可能出现死循环(边界数据)
    • 时间复杂度不符合要求,算法需要优化
  5. 运行时错误(Runtime Error):

    • 访存错误,数组越界
    • 除以0
    • 调用了评判系统禁止调用的函数
    • 递归过深或其他原因造成的栈溢出
  6. 编译错误(Compile Error):

    • 根据编译信息修改
  7. 使用内存超出限制(Memory Limit Exceeded):

    • 算法空间复杂度过高
    • 程序本身的错误使得程序不断申请内存(注意数组声明和死循环)
  8. 输出超出限制(Output Limit Exceeded):

    • 提交是忘记关闭调试输出时的调试信息
    • 输出出现死循环

语言与IDE选择

语言

推荐使用C with STL

IDE

VC++ 6.0 , Visual Studio , Dev-C++ , Code::Blocks,推荐使用Code::Blocks

在线测评系统

牛客网( https://www.nowcoder.com/kaoyan