2012年清华大学计算机研究生机试真题

题目1483:求最大最小数

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:3106

解决:1553

题目描述:

输入N个(N<=10000)数字,求出这N个数字中的最大值和最小值。每个数字的绝对值不大于1000000。

输入:

输入包括多组测试用例,每组测试用例由一个整数N开头,接下去一行给出N个整数。

输出:

输出包括两个整数,为给定N个数中的最大值与最小值。

样例输入:
5
1 2 3 4 5
3
3 7 8
样例输出:
5 1
8 3

很简单 没什么说的

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}



int main()
{
    //fread;
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int mx=-1000010,mi=1000010;
        for(int i=0;i<n;i++)
        {
            int a;
            scanf("%d",&a);
            mi=min(mi,a);
            mx=max(mx,a);

        }
        printf("%d %d\n",mx,mi);
    }
    return 0;
}


题目1482:玛雅人的密码

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:2320

解决:582

题目描述:

玛雅人有一种密码,如果字符串中出现连续的2012四个数字就能解开密码。给一个长度为N的字符串,(2=<N<=13)该字符串中只含有0,1,2三种数字,问这个字符串要移位几次才能解开密码,每次只能移动相邻的两个数字。例如02120经过一次移位,可以得到20120,01220,02210,02102,其中20120符合要求,因此输出为1.如果无论移位多少次都解不开密码,输出-1。

输入:

输入包含多组测试数据,每组测试数据由两行组成。
第一行为一个整数N,代表字符串的长度(2<=N<=13)。
第二行为一个仅由0、1、2组成的,长度为N的字符串。

输出:

对于每组测试数据,若可以解出密码,输出最少的移位次数;否则输出-1。

样例输入:
5
02120
样例输出:
1

执行移位操作 相邻位置的数字可以进行交换 问至少进行几次操作使得这个字符串中出现2012

因为只有13位  每位上只可能是0 1 2三个数字 所以最后就3^13种情况 我们记录下每个字符串对应于初始字符串需要移动的次数  BFS搜索 当前移动次数少的优先

讲当前的字符串转换为对应的3进制数 从而方便标记当前字符串有没有搜索到过

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int n;
string str;
struct node
{
    int step;
    string s;
    bool operator<(const node p)const
    {
        return step>p.step;
    }
};

int vis[1600000];
priority_queue<node> que;

int judge(string s)
{
    for(int i=0;i<n-3;i++)
    {
        if(s.substr(i,4)=="2012")
            return 1;
    }
    return 0;
}

int getnum(string s)
{
    int x=0;
    for(int i=0;i<n;i++)
    {
        x*=3;
        x+=s[i]-'0';
    }
    return x;
}

int main()
{
//    fread;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        cin>>str;
//        cout<<"str  "<<str<<endl;
        while(!que.empty())
            que.pop();
        memset(vis,0,sizeof vis);
        node no;
        no.s=str;
        no.step=0;
        que.push(no);
        int flag=0;
        while(!que.empty())
        {
            no=que.top(); que.pop();
            if(judge(no.s))
            {
                flag=1;
                printf("%d\n",no.step);
                break;
            }
            for(int i=0;i<n-1;i++)
            {
                swap(no.s[i],no.s[i+1]);
                int num=getnum(no.s);
//                cout<<"num  "<<num<<endl;
                if(!vis[num])
                {
                    vis[num]=1;
                    node p;
                    p.s=no.s;
                    p.step=no.step+1;
//                    cout<<"step "<<p.step<<endl;
                    que.push(p);
                }
                swap(no.s[i],no.s[i+1]);
            }
        }
        if(!flag)
            puts("-1");
    }
    return 0;
}


题目1491:求1和2的个数

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1107

解决:326

题目描述:

给定正整数N,函数F(N)表示小于等于N的自然数中1和2的个数之和,例如:1,2,3,4,5,6,7,8,9,10序列中1和2的个数之和为3,因此F(10)=3。输入N,求F(N)的值,1=<N<=10^100(10的100次方)若F(N)很大,则求F(N)mod20123的值。

输入:

输入包含多组测试数据,每组仅输入一个整数N。

输出:

对于每组测试数据,输出小于等于N的自然数中1和2的个数之和,且对20123取模。

样例输入:
10
11
样例输出:
3
5
提示:

建议用scanf ("%s")输入,而不建议用gets()!


刚开始看到这题 显然是要根据每一位上的数字进行分析 直接暴力肯定是不行的

我第一想法是从高位开始 根据判断该位是1 2 或者大于2来进行计算 比如说在千位如果是3则1 2在千位就会出现2000次 但是如果是2 则1在千位上就会出现1000次 而2在千位上出现的次数此时就会由后面的数字确定

所以这个时候还跟后面的数字有关。。。顿时觉得有点烦。。

其实去仔细的分析 其实也不是很难。。具体的分析过程可以参考编程之美1的个数

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <string>
#include <cmath>
#include <cstdio>

using namespace std;

char ch[110];

int getnum(int s,int t)
{
    int x=0;
    for(int i=s;i<=t;i++)
    {
        x=x*10+ch[i]-'0';
        x%=20123;
    }
    return x;
}

int fac(int n)
{
    int x=1;
    for(int i=0;i<n;i++)
    {
        x*=10;
        x%=20123;
    }
    return x;
}

int main()
{
    while(scanf("%s",ch)!=EOF)
    {
        int len=strlen(ch);
        int sum=0;
        for(int i=0;i<len;i++)
        {
            int high=getnum(0,i-1);
            int low=getnum(i+1,len-1);
            int p=fac(len-1-i);
            if(ch[i]=='0')
            {
                sum+=(2*high*p)%20123;
            }
            if(ch[i]=='1')
            {
                sum+=(2*high*p+low+1)%20123;
            }
            if(ch[i]=='2')
            {
                sum+=((2*high+1)*p+low+1)%20123;
            }
            if(ch[i]>'2')
            {
                sum+=(2*high+2)*p%20123;
            }
            sum%=20123;
        }
        printf("%d\n",sum);
    }
    return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值