机试知识点整理

考研机试指南

在线评测系统(Online Judge,OJ),提供题目的题目描述、输入输出格式、样例输入及输出,系统用户将相应的代码提交给OJ进行评测。OJ后台利用多组数据运行程序,对程序进行评测,并向用户返回相应结果。结果一般有以下八种:

  1. AC(Accepted),答案正确;

  2. CE(Compile Error),编译错误:代码无法通过编译,检查是否提交时选错了语言,或用本地编译器重新编译提交的代码,根据反馈的错误信息,进行修改后重新提交。此类结果一般是由于代码本身错误或者使用库函数却未添加相应头文件;

  3. MLE(Memory Limit Exceeded),内存超限:如果程序使用了太多空间,超过题目限制。检查定义的数组过大、递归层数过多、动态开辟的内存空间在失去用处后未释放……均可导致内存超限;

  4. OLE(Output Limit Exceeded),输出超限:输出过量内容,一般是由于未将调试输出注释导致;

  5. PE(Presentation Error),格式错误:一般我们认为是最接近AC的错误,修改程序中错误输出格式即可解决;

    #include<bits/stdc++.h>
    using namespace std;
    
    int main() {
    	int arr[4],count=1;
    	char str[100];
    	// 注意不要输入和输出Sample 
    	while(cin>>arr[0]>>arr[1]>>arr[2]>>arr[3]) {
    		int n;
    		//第一行
    		for(int i=0; i<4; i++) {
    			n=arr[i];
    			if(i==3) {
    				if(n==1) cout<<"----*"<<endl;
    				else if(n==4) cout<<"*---*"<<endl;
    				else cout<<"*****"<<endl;
    			}else {
    				if(n==1) cout<<"----* ";
    				else if(n==4) cout<<"*---* ";
    				else cout<<"***** ";
    			}
    		}
    		//第二行
    		for(int i=0; i<4; i++) {
    			n=arr[i];
    			if(i==3) {
    				if(n==4||n==8||n==9||n==0) cout<<"*---*"<<endl;
    				else if(n==5||n==6) cout<<"*----"<<endl;
    				else cout<<"----*"<<endl;
    			}else {
    				if(n==4||n==8||n==9||n==0) cout<<"*---* ";
    				else if(n==5||n==6) cout<<"*---- ";
    				else cout<<"----* ";
    			}
    		}
    		//第三行 
    		for(int i=0; i<4; i++) {
    			n=arr[i];
    			if(i==3) {
    				if(n==1||n==7) cout<<"----*"<<endl;
    				else if(n==0) cout<<"*---*"<<endl;
    				else cout<<"*****"<<endl;
    			}else {
    				if(n==1||n==7) cout<<"----* ";
    				else if(n==0) cout<<"*---* ";
    				else cout<<"***** ";
    			}
    		}
    		//第四行 
    		for(int i=0; i<4; i++) {
    			n=arr[i];
    			if(i==3) {
    				if(n==0||n==6||n==8) cout<<"*---*"<<endl;
    				else if(n==2) cout<<"*----"<<endl;
    				else cout<<"----*"<<endl;
    			}else {
    				if(n==0||n==6||n==8) cout<<"*---* ";
    				else if(n==2) cout<<"*---- ";
    				else cout<<"----* ";
    			}
    		}
    		
    		//第五行
    		for(int i=0; i<4; i++) {
    			n=arr[i];
    			if(i==3) {
    				if(n==1||n==4||n==7) cout<<"----*"<<endl;
    				else cout<<"*****"<<endl;
    			}else {
    				if(n==1||n==4||n==7) cout<<"----* ";
    				else cout<<"***** ";
    			}
    		}
    		// 注意这个回车也是要的 
    		cout<<endl;
    	} 
    }
    
  6. RE(Runtime Error),运行错误:检查是否有浮点错误、段错误(非法访问内存)、递归层数过多等;

  7. TLE(Time Limit Exceeded),运行超时:算法时间复杂度大,或者是特殊数据导致程序陷入死循环;

  8. WA(Wrong Answer),答案错误:好好检查算法逻辑吧,或者也可能是因为自己书写代码时犯了一些小错误。

1.输入输出

  1. 一般用C语言节约空间,要用C++库函数或STL时才用C++;
    cout、cin和printf、scanf最好不要混用。 大数据输入输出时最好不要用cin、cout,防止超时。

  2. 可以使用%4d;来获取输入中的前4位数字。

  3. 输入数据以0结束的循环

    // 格式一:
    int main() {
      int n;
      scanf("%d", &n);
      while (n != 0) {
        // 对n进行处理
        scanf("%d", &n);
      }
    }
    
    // 格式二:(推荐这种!!!)
    while (scanf("%d", &n), n) {
        // 对n进行处理
    }
    
    // 格式三:
    while (scanf("%d", &a)) {
          if(a == 0) break;
    }
    
    // 格式四:(多个数以0结束):
    while (scanf("%d%d", &a, &b), a || b) {
        // 对a、b进行处理
    }
    
    
  4. 数据以EOF结束的循环

    while( ( scanf(%d”,&a) ) != -1 )
    while( ( scanf(%d”,&a) ) != EOF) // 推荐!
    while( ( scanf(%d”,&a) ) == 1 )
    while( ~scanf(%d”,&a)) // 推荐!
    
  5. 处理T组数据

    #include <cstdio>
    int main () {
        int T;
        scanf("%d", &T);
        while (T--) {
            scanf("%d%d", &a, &b);
            
        }
        return 0;
    }
    
    1. 纯字符串用puts()输出。

    2. 关于gets和getchar()

      getchar()作用:从缓冲区读走一个字符,相当于清除缓冲区

      示例:如果要连续输入两个字符

      #include<stdio.h>
      int main() {
          char a, b;
          scanf("%c", &a);
          scanf("%c", &b);
          printf("%c\n", a);
          printf("%c", b);
      } 
      

    在这里插入图片描述

     前面的scanf()在读取输入时会在缓冲区中留下一个字符'\n'(输入完第一个字符后按回车键所致),所以如果不在此加一个getchar()把这个回车符取走的话,第二个scanf()会直接取走这个“无用的”回车符,从而导致读取有误。
    

    ​ 加入getchar()之后
    在这里插入图片描述

    ​ 可以看到第二scanf正确读取到了字符’b’

    gets()作用:读取字符串直到回车结束(不包括回车)

    ​ gets和scanf读取字符串时:gets会读取包括空格字符,而scanf不会

    #include<stdio.h>
    
    int main() {
    	
    	char a[100], b[100];
    	gets(a);
    	scanf("%s", &b);
    	printf("%s\n", a);
    	printf("%s", b);
    }
    

在这里插入图片描述

可以看到输入的第一行的字符都读取到了(除回车),第二行字符由于中间有个空格,所以只会读取空格前的所有字符

2.模板(使用万能头文件)

#include<bits/stdc++.h>

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;

int main(){ 
    // 用C还是C++都行(亦可以混用)
	return 0;
}

3.数据定义及类型

  1. C语言数据类型

在这里插入图片描述

​ a. int 型也被称为long int型,输如输出格式是"%d"

​ b. long long型,输入输出格式为" l l d lld lld",若初值大于231-1,需要在初值后面加上LL,否则会有编译错误;

#include <cstdio>
int main() {
	long long a = 1234567890987654321LL;
	printf("%lld",a);
	return 0;
}
  1. 定义
    1. 数组定义int a[10]={0};可以对其全部元素赋值为0;(数组大小最好比告诉的最大范围大一点;字符数组大小必须比字符串最大长度大1。)
    2. 数组太大不要这样,防止CE,当然也不要过小,可以根据题目而定如100、1000
    3. 全局变量,静态变量自动初始化为0

4.ASCII码表

在这里插入图片描述

  1. C语言字符的int值可以通过s-’\0’

在这里插入图片描述

```c
int main() {
	string s="I want to take the MS's rocket to live in the MS's ice castle.";
	//计算每个字符串的ascii码 
	for(int i=0; i<s.length(); i++) {
		if(i==s.length()-1) cout<<s[i]-'\0';
		else cout<<s[i]-'\0'<<" ";
	}
}
```
  1. 字符A的ASCII码值65,Z为90, a为97,z为122,大小写之差为32!!!

5.排序

bool cmp(int m,int n)
{
    return m>n;
}

int a[n]={...};
sort(a,a+n);
sort(a,a+n,cmp); //默认从小到大

6.素数

// 第一种判断是素数 
int isPrime1(int a) {
	int i;
	for(i=2; i<a; i++) {
		if(a%i==0) break;
	}
	if(i==a) return 1; //是素数
	else return 0;//不是素数
}
// 第二种判断时候是素数 
int isPrime2(int a) {
	int flag=1;
	for(int i=2; i<=sqrt(a); i++) {
		if(a%i==0) {
			flag=0;// 不是素数 
			break;
		}
	}
	if(flag) return 1;
	else return 0;
}

7.最大公因数和最小公倍数

主要介绍两种方法:辗转相除和穷举法

//辗转相除法函数嵌套求两数的最大公约数
int divisor (int a,int b) {
	int temp;    //定义整型变量
    // a b
 	if(a<b) {    //通过比较求出两个数中的最大值和最小值
        temp=a;
        a=b;
        b=temp;
	}    //设置中间变量进行两数交换
    while(b!=0) {    //通过循环求两数的余数,直到余数为0
        temp=a%b;
        a=b;    //变量数值交换
        b=temp;
    }
    return a;    //返回最大公约数到调用函数处
}


//穷举法求两数的最大公约数
int divisor (int a,int b) {
    int  temp;    //定义义整型变量
    temp=(a>b)?b:a;    //采种条件运算表达式求出两个数中的最小值
    while(temp>0) {   
        if (a%temp==0&&b%temp==0)    //只要找到一个数能同时被a,b所整除,则中止循环
        	break;    
       	temp--;    //如不满足if条件则变量自减,直到能被a,b所整除
    }
  	return  temp;    //返回满足条件的数到主调函数处
}

最小公倍数 = 两数之积 / 最大公因数

在这里插入图片描述

8.栈

一般用于解决括号匹配问题

#include <stack>

stack<int> S;

S.push(i);
int x = S.top();
S.pop();

在这里插入图片描述

9.爬楼梯问题

在这里插入图片描述

#include<stdio.h>
// 1 2 3 5 8 13
int fib(int n) {
	if(n==1) return 1;
	if(n==2) return 2;
	return fib(n-1)+fib(n-2);
}

int main() {
	int n;
	scanf("%d", &n);
	printf("%d", fib(n));
}
#include<stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int dp[n+1];
    dp[1] = 1;
    dp[2] = 2;
    for(int i=3;i<=n;i++)
    {
        dp[i]=dp[i-1]+dp[i-2];
    }
    printf("%d", dp[n]);
}

10.有很多数学题是有规律的,直接推公式或用递归、循环

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;

//1   			A
//2   			A 
//3   			B
//1+3 			A
//2+3 			A
//1+5 2+4    	B
//1+6    		A
//2+6 		    A
//1+8 2+7 		B
//1+9 			A 
int main() {
	// 很明显是n>=0的整数 
	int n;
	while(cin>>n) {
		int N=n+1;
		if(N%3==0) cout<<"PangPang will get the gold nugget!"<<endl;
		else cout<<"Brother Chao will get the gold nugget!"<<endl;
	}
}

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;

// 这道题注意技巧!!! 
int main() {
    string s1, s2;
    int sum1=0, sum2=0;
    while(cin>>s1>>s2) {
        if(s1=="0"&&s2=="0") break;
        sum1=s1[s1.length()-1]-'a';
        sum2=s2[s2.length()-1]-'a';
        if((sum1+sum2)%2==0) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
}

在这里插入图片描述

#include<stdio.h>

int main() {
	int n, sum=1;
	scanf("%d", &n);
	for(int i=0; i<n-1; i++) {
		sum=(sum+1)*2;
	}
	printf("%d", sum);
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

codewen77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值