2012百度之星资格赛题目答案

这次百度之星,我做出来了7道,另外有2题的答案也被我找到了,还有一道F题希望有的人给我留下言吧,我想学习下。下面是我的答案,大多是用C++,也有几道是用Java的,代码里的一些css的东西请去掉,是csdn上加上去的。这次初赛可以说是不难的,像我这样的新手都能做出7道。不过,有些题目要好好考虑错误情况。F题,有人说百度翻译的不到位,这是别人告诉我的原文,http://162.105.81.212:8080/problem?id=3814。

A:百度计算器的加法

#include<iostream>
using namespace std;
int main()
{
 int n,m;
 cin>>n>>m;
 n=n+m;
 cout<<n<<endl;
 system("pause");
 return 0;
}


B:小诺爱USB设备

#include<iostream>
using namespace std;
int main()
{
 int n,m,a,s;
 cin>>n;
 while(n--)
 {
	 cin>>m;
	 s=1-m;
	 while(m--)
    { 
		cin>>a;
		s+=a;
	 }
	 cout<<s<<endl;
 }
 system("pause");
 return 0;
}

C:易手机的套餐

#include<iostream>
#include<iomanip>

using namespace std;
int main()
{
 int n=12;
 double s=0,m;
 while(n--)
 {
	 cin>>m;
	 s=s+m;
 }
 s=s/12;
 cout<<fixed<<showpoint;
 cout<<"$"<<setprecision(2)<<s<<endl;
 system("pause");
 return 0;
}

D:共同狂欢

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int n, m;
		Scanner scanner = new Scanner(System.in);
		String ss = scanner.nextLine();
		n = Integer.parseInt(ss);
		while (n-- > 0) {
			String temp = scanner.nextLine();
			String a[] = temp.split(":");
			if (!a[1].equals("00")) {
				System.out.println(0);
			} else {
				m = Integer.parseInt(a[0]);
				m = m + 12;
				if (m > 24)
					m -= 24;
				System.out.println(m);
			}
		}
	}

}

E:C++ 与Java

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String ss;
		while ((ss = scanner.nextLine()) != null) {
			int index1 = 0;
			boolean index2;
			index1 = ss.indexOf("_");
			index2 = ss.matches(".*[A-Z].*");
			if (index1 != -1 && !index2) {
				if (ss.charAt(0) == '_' || ss.charAt(ss.length() - 1) == '_') {
					System.out.println("Error!");
				} else {
					boolean t = true;
					String temp[] = ss.split("_");
					String result = temp[0];
					for (int a = 1; a < temp.length; a++) {
						if (temp[a].equals("")) {
							t = false;
							break;
						}
						// System.out.println(temp[a]+"KKK");
						result += temp[a].substring(0, 1).toUpperCase()
								+ temp[a].substring(1);
					}
					if (t)
						System.out.println(result);
					else
						System.out.println("Error!");
				}
			} else if (index1 == -1 && index2) {
				boolean tt = true;
				String result = "";
				int kk = 0;
				for (int i = 0; i < ss.length(); i++) {
					if (ss.charAt(0) >= 'A' && ss.charAt(0) <= 'Z') {
						tt = false;
						break;
					}
					if (ss.charAt(i) >= 'A' && ss.charAt(i) <= 'Z') {
						result += ss.substring(kk, i).toLowerCase() + "_";
						kk = i;
					}
					if (i == ss.length() - 1)
						result += ss.substring(kk).toLowerCase();
				}
				if (tt)
					System.out.println(result);
				else
					System.out.println("Error!");
			} else if (index1 == -1 && !index2) {
				System.out.println(ss);
			} else
				System.out.println("Error!");
		}
	}
}

G:聊天就是Repeat

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String ss;
		while((ss = scanner.nextLine())!=null)
		{
			System.out.println(ss);
		}
	}

}

H:用户请求中的品牌

    #include<math.h>
    #include<stdio.h>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int maxn=100010;
    const int inf=5000000;
    int w[maxn],wa[maxn],wb[maxn],wv[maxn];
    int sa[maxn],rank[maxn],height[maxn];
    int a[maxn],f[maxn][20],n,ft[maxn],len[maxn];
    int num;
    char s[maxn];
    int cmp(int *r,int a,int b,int l)
    {
        return r[a]==r[b]&&r[a+l]==r[b+l];
    }
    void da(int *r,int *sa,int n,int m)
    {
        int i,j,p,*x=wa,*y=wb,*t;
        for (i=0; i<m; i++) w[i]=0;
        for (i=0; i<n; i++) w[x[i]=r[i]]++;
        for (i=1; i<m; i++) w[i]+=w[i-1];
        for (i=n-1; i>=0; i--) sa[--w[x[i]]]=i;
        for (p=1,j=1; p<n; m=p,j*=2)
        {
            for (p=0,i=n-j; i<n; i++) y[p++]=i;
            for (i=0; i<n; i++) if (sa[i]>=j) y[p++]=sa[i]-j;
            for (i=0; i<m; i++) w[i]=0;
            for (i=0; i<n; i++) w[wv[i]=x[y[i]]]++;
            for (i=1; i<m; i++) w[i]+=w[i-1];
            for (i=n-1; i>=0; i--) sa[--w[wv[i]]]=y[i];
            for (t=x,x=y,y=t,x[sa[0]]=0,p=1,i=1; i<n; i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
        return;
    }
    void cal(int *r,int *sa,int n)
    {
        int i,j,k=0;
        for (i=1; i<=n; i++) rank[sa[i]]=i;
        for (i=0; i<n; height[rank[i++]]=k)
            for (k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
        return;
    }
    int nmin(int a,int b)
    {
        return a<b?a:b;
    }
    void rmq(int n)
    {
        int i,j;
        for (i=1; i<=n; i++)
            f[i][0]=height[i];
        for (j=1; j<20; j++)
            for (i=1; i+(1<<j)-1<=n; i++)
                f[i][j]=nmin(f[i][j-1],f[i+(1<<j-1)][j-1]);
        return;
    }
    int lcp(int a,int b)
    {
        int x=rank[a],y=rank[b];
        if (x>y)
        {
            int t=x;
            x=y;
            y=t;
        }
        x++;
        int t=ft[y-x+1];
        return nmin(f[x][t],f[y-(1<<t)+1][t]);
    }
    int main()
    {
        int cas=1;
        int i;
        char x;
        for (i=0; i<maxn; i++) ft[i]=int(double(log(i*1.00))/log(2.00));
        while (scanf("%s",s)!=EOF)
        {
            if(s[0]=='#')  break;
            n=strlen(s);
            for (i=0; i<n; i++)  a[i]=(int)s[i];
            a[n]=0;
            da(a,sa,n+1,128);
            cal(a,sa,n);
            rmq(n);
            int k,max=0,r=0,t;
            num=0;
            for(int l=1; l<n; l++)//枚举长度
                for(int i=0; i+l<n; i+=l)
                {
                    k=lcp(i,i+l);
                    r=k/l+1;//注意为什么是k/l+1
                    t=i-(l-k%l);
                    if (t>=0&&k%l!=0)
                        if (lcp(t,t+l)>=k) r++;
                    if (r>max)
                    {
                        num=0;
                        len[++num]=l;
                        max=r;
                    }
                    else
                    if(r==max)
                    {
                        len[++num]=l;
                    }
                    //printf("l=%d  r=%d  i=%d  i+l=%d/n",l,r,i,i+l);
                }
           // printf("%d/n",max);
           int start,l;
            for(int i=1;i<n;++i)//sa应从1开始,因为sa[0]是0
            {
                int ts=sa[i];//枚举这个开始
                for(int j=1;j<=num;++j)
                {
                    int tl=len[j];
                    if(lcp(ts,ts+tl)>=(max-1)*tl)
                    {
                        start=ts;
                        l=tl*max;
                        i=n;
                        break;
                    }
                }
            }
            printf("Case %d: ", cas++);
            for (int i = 0; i < l; i++) printf("%c", s[start+ i]);
            cout<<endl;
        }
        return 0;
    }

I:地图的省钱计划


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<string>
#include<algorithm>
#include<fstream>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<complex>
#include<numeric>
using namespace std;
double x[10], y[10], t[10];
bool solve(int i, int j, int k)
{
    double x1, y1, x2, y2, t1, t2;
    x1 = x[j] -x[i];
    x2 = x[k] -x[i];
    y1 = y[j] -y[i];
    y2 = y[k] -y[i];
    t1 = t[j] -t[i];
    t2 = t[k] -t[i];
    
    double A1 = x1*x1 + y1*y1 - t1*t1;
    double A2 = x2*x2 + y2*y2 - t2*t2;
    double A = A1*y2-A2*y1, B = A1*x2-A2*x1, C = A1 * t2 - A2 * t1;
    double cita = atan2(B, A);
    double sum = asin(- C/sqrt(A*A+B*B+1e-15));
    
    double alpha = sum - cita;
    double r;
    if (abs(A1)>abs(A2))
        r = A1/(t1 + x1 *cos(alpha) + y1 * sin(alpha))/2;
    else
        r = A2/(t2 + x2 *cos(alpha) + y2 * sin(alpha))/2;
    
    if (r<0)
    {
        sum = - sum + 3.141592653579;
        alpha = sum - cita;
        if (abs(A1)>abs(A2))
            r = A1/(t1 + x1 *cos(alpha) + y1 * sin(alpha))/2;
        else
            r = A2/(t2 + x2 *cos(alpha) + y2 * sin(alpha))/2;
    }
            
        
    printf("%.6f %.6f\n", r * cos(alpha) + x[i], r * sin(alpha) + y[i]);
}
int main()
{
    for (int dd = 1; ; ++ dd)
    {
        double c;
        scanf("%lf", & c);
        c/=1000;
        if (abs(c) < 1e-6)
            break;
        scanf("%lf %lf %lf %lf %lf %lf", x, y, x+1, y+1, x+2, y+2);
        scanf("%lf %lf %lf", t, t+1, t+2);
        printf("Case %d:\n", dd);
        t[0] *= c;
        t[1] *= c;
        t[2] *= c;
        if (solve(0, 1, 2))
            continue;        
    }
    return 0;
}

J:百度的新大厦

#include<iostream>
#include<math.h>
#define INT_MAX  2147483647;

using namespace std;
int main()
{
 int n,m,a,b,s1,i,min=INT_MAX;
 cin>>n>>m;
 while(m--)
 {  
	 cin>>a>>b;
	 i=b*n/(a+b);
	 s1=a*(i+1)-b*(n-i-1);
	 if(s1>0&&s1<min)
		 min=s1;
 }
 cout<<min<<endl;
 system("pause");
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值