zoukankan      html  css  js  c++  java
  • 研究生复试上机

    前言

    虽然我初试不一定能过线,但还是准备起复试,人生中总常常出现意外,但还是要先做好准备。

    历年真题

    有什么东西比真题还香呢?拿不到测试数据,所以代码只能看看,正确性还是不能保证。我用C++写的,你用C,Java一样的

    2017

    image.png

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
    	string s,res="";
    	int t=8;
    	while(t--)
    	{
    		cin>>s;
    		if(s.find("4") ==s.npos  &&  s.find("S")==s.npos && res=="")  res=s;
    	}
    	if(res!="") cout<<res<<endl;
    	else cout<<"Not found"<<endl;
    	return 0;
    }
    // 输入  444 S AS4 456Q ASD 789 321 45Q 74 (我长度就不弄成10了)
    // 输出  789
    

    image.png

    // 不用管notepad,那只是一个编辑器,文件自己创建就好
    #include <bits/stdc++.h>
    using namespace std;
    bool isprime(int x)
    {
    	if(x<=1) return false;
    	for(int i=2;i<x;i++)
    	{
    		if(x%i==0) return false;
    	}
    	return true;
    }
    int main() {
    	ifstream myfile("numbers.txt");
    	int x,y,f=1;
    	while(!myfile.eof()) //直到文件结尾
    	{
    		myfile>>x>>y;
    		if(isprime(y)==false) f=0;
    	}
    	if(f) cout<<"文件没有被篡改"<<endl;
    	else cout<<"文件已经被被篡改"<<endl; 
    }
    //numbers.txt 3 11 2 13 文件没有被篡改
    //numbers.txt 3 11 2 40 文件已经被篡改
    

    image.png

    // 按贺卡价钱从低到高排个序就好了 
    #include <bits/stdc++.h>
    using namespace std;
    struct Node
    {
    	double price;
    	int num;
     } node[105];
     bool cmp(Node a, Node b)
     {
     	return a.price<b.price;
     }
    int main() {
    	int m,n,num,cnt=0,i,j;
    	double p,res=0;
    	cin>>m>>n;
    	for(i=0;i<n;i++)
    	{
    		cin>>p>>num;
    		for(j=0;j<cnt;j++)
    		{
    			if(node[j].price==p) 
    			{
    				node[j].num+=num;break;
    			}
    		}
    		if(j==cnt) 
    		{
    			node[cnt].price=p;node[cnt].num=num;
    			cnt++;
    		}
    	}
    	sort(node,node+cnt,cmp);
    	for(i=0;i<cnt;i++)
    	{
    		if(m==0) break;
    		if(node[i].num>m) 
    		{
    			m-=node[i].num;
    			res+=node[i].price*node[i].num; 
    		}
    		else 
    		{
    			res+=node[i].price*m; 	
    			m=0;
    		}
    	} 
    	printf("总价为%.2lf
    ",res);
    }
    //3 3
    //1 2
    //2 1
    //1 1
    //
    //3.00
    //
    //4 3
    //1 3
    //2 2
    //3 5
    //4.00
    

    image.png

    // 典型01背包问题,不会的话请百度看了就懂
    #include <bits/stdc++.h>
    using namespace std;
    int main() {
    	int n,c,p[1005],w[1005],dp[1005]; //dp[i]表示容量为c的背包最大收益为 dp[i]
    	memset(dp,0,sizeof(dp));
    	cin>>n>>c;
    	for(int i=0;i<n;i++) cin>>w[i]>>p[i];
    	for(int i=0; i<n; i++) {
    		for(int j=c; j>=w[i]; j--)
    			dp[j]=max(dp[j],dp[j-w[i]]+p[i]);
    	}
    	cout<<dp[c]<<endl;
    }
    //5 10
    //5 1
    //4 2 
    //3 3
    //2 4
    //1 5
    // 14
    

    注意点

    • 有C++可以用,直接stl走起
    • 万能头文件#include <bits/stdc++.h>
    • double输出后2位 printf("%.2lf ",变量);
    • string 读入一样带空格的字符串getline(cin,s);
    • printf("%4d",变量); 占4位右对齐
    • printf("%-4d",变量);占4位左对齐
    • s.find("目标") ==s.npos ,string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos

    各种算法模板

    素数帅选法

    int prime[100010],n;
    void find()
    {
    	for(int i=2;i<=n;i++) prime[i]=1;
    	for(int i=2;i*i<=n;i++)
    	{
    		if(prime[i])
    		{
    			for(int j=i*i;j<=n;j+=i) prime[j]=0;
    		}
    	}
    }
    

    自定义排序模板

    sort(a+0,a+10,cmp);//长度为10的a数组从大到小
    bool cmp(int a, int b)
    {
    	return a>b;
    }
    

    为了以防万一准备个冒泡

    void mysort(int x[],int n)
    {
    	for(int i=0;i<n-1;i++)
    	{
    		for(int j=0;j<n-i-1;j++) 
    		{
    			if(x[j]>x[j+1]) 
    			{
    				int temp=x[j];
    				x[j]=x[j+1];
    				x[j+1]=temp;
    			}
    		}
    	}
    }
    

    每次写每月天数都挺累的,记录下,下次直接用

    data[12]={31,28,31,30,31,30,31,31,30,31,30,31}
    

    并查集,路径就不压缩了

    int n,a[5005];
    for(int i=1;i<=n;i++) a[i]=i;
    int find(int x)
    {
    	while(x!=a[x]) x=a[x];
    	return x;
     }
    void myunion(int x,int y)
    {
    	int fx=find(x),fy=find(y);
    	if(fx==fy) ;
    	else a[fx]=fy;
    } 
    

    杨辉三角

    for(int i=0;i<n;i++) a[i][0]=a[i][i]=1;
    for(int i=0;i<n;i++)
    {
    	for(int j=1;j<i;j++) a[i][j]=a[i-1][j]+a[i-1][j-1];
    }
    

    英语数字

    res[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    
  • 相关阅读:
    Oracle数据库事务
    MSSQL数据库事务
    MSSQL公用方法查询最新ID
    UISwitch的用法总结开关用法
    iPhone和ipad键盘高度及键盘响应事件 摘
    ios 6 的 小改变
    instance method 'addMask' not found (return type defaults to 'id')
    关于tableView group样式 时设置 cell的width 宽度
    判断 iPhone 虚拟键盘是否打开的代码 (转)
    UITextField 控件详解 我见过的史上最牛x的
  • 原文地址:https://www.cnblogs.com/xxhao/p/14305663.html
Copyright © 2011-2022 走看看