acm训练题 初级(1)

(拖更已久…#羞愧#)

第一题:字符串
Julius Caesar生活在一个危险而又充斥着阴谋的时代。Caesar面对的最难的情况关系着他的存亡。为了让自己生存,他决心去创造第一种加密方法之一。这个加密方法听起来是这样的令人难以置信,没有一个人可以指出它(的原文)除非知道它怎样工作。
你是Caesar军队的一个分队长。你的工作是破译Caesar送来的信息并汇报给你的上级。
密码很简单,每一个字母对应着一个明文,你将明文向右五步来得到安全的信息。(比如,假如那个字母是‘A’,密文就是‘F’)当你将Caesar的信息中找出明文后,你要反过来做:
加密文本
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
明文文本
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
密文中只有字母被切换了,非字母的字符应该保持不变,所有的字母都是大写的
这个问题的输入包括一系列(非空)最多100个数据。每一个数据的格式会按照以下格式,并且在不同组数据间不会有空行分隔。所有的字符都是大写的。
一个单独的测试数据包括三个部分:

  1. 开始行:单独的一行“START” 。
  2. 加密的信息:单独的一行,由1~200个字符组成来自Caesar的一行信息。
  3. 结束行:单独的一行“END” 。
    最后一组测试数据结束会跟着单独的一行“ENDOFINPUT”。

对每一个测试数据只会有一行输出。它是Caesar的原文。

Sample Input

START
NS BFW, JAJSYX TK NRUTYFSHJ FWJ YMJ WJXZQT TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

看起来挺简单的(其实也挺简单)
顺便一提
strlen()获取字符串长度
strcmp()字符串大小比较,0是等于
strcpy()字符串赋值
gets()包含空格的字符串的输入

#include 
#include 
using namespace std;
int main(void)
{
    int i;
    char a[1000],b[1000];
   while(gets(a))
   {
     if(strcmp(a,"START")==0)//strcmp比较函数
     memset(b,0,sizeof(b));//清0数组,详情请看acm新手小白系列
     else if(strcmp(a,"END")==0)
     printf("%s\n",b);
     else if(strcmp(a,"ENDOFINPUT")==0)
     break;
     else
     {
         for(i=0;a[i]!='\0';i++)
         {
             if(a[i]>='A'&&a[i]<='Z')
             {
                 if(a[i]+21>90)
                    b[i]=(a[i]-5);
                 else
                    b[i]=(a[i]+21);
             }
            else
            b[i]=a[i];
         }
     }

   }
    return 0;
}

第二题:数的长度

N! (N的阶乘) 是非常大的数,计算公式为:N! = N * (N - 1) * (N - 2) * … * 2 * 1)。现在需要知道N!有多少(十进制)位。

Input

每行输入1个正整数N。0 < N < 1000000

Output

对于每个N,输出N!的(十进制)位数。

Sample Input

1
3
32000
1000000

Sample Output

1
1
130271
5565709

首先搞清如何计算:
N!的位数
lg(N!)+1
lg(N!)=lg(2)+…lg(N)

#include 
#include 
using namespace std;

int main()
{
    double n,m;
    int i,j;
    while(scanf("%lf",&n)!=EOF)
    {
     m=0.0;
     for(i=2;i<=n;i++)
     {
       m+=log10(i);
     }
     j=m+1;//强制转化
     printf("%d\n",j);
    }
    return 0;
}

第三题:循环节

A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).

Input

The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output

For each test case, print the value of f(n) on a single line.

Sample Input

1 1 3
1 2 10
0 0 0

Sample Output

2
5

有mod必有循环节
循环:0 4 5 3 1 1

第四题:大数取余
f(n)=1^2+2^2+3^2+.......+n^2,这个公式是可以化简的,现在的问题是想要计算f(n)对1007取余的值
Input
输入数据有多组(不超过100组),每组一个数n. (1<=n <=1,000,000,000).
Output
输出f(n)对1007取余的值。
Sample Input
3
4
100
Sample Output
14
30
1005
提示

f(n)=(1/6*n*(n+1)*(2*n+1))

公式

(a+b)%c=(a%c+b%c)%c
(a*b)%c=((a%c)*(b%c))%c

第五题:阶乘

计算N!末尾有多少个0

Input

输入数据有多组,每组1行,每行1个数N(10 <= N <=100000000)

Output

在一行内输出N!末尾0的个数。

Sample Input

10
100

Sample Output

2
24
此题,技巧题:

n!=n*n-1*n-2*n-3...3*2*1

0的来源是25=10
也就是一对2
5产生一个0
在一个大数里产生的5是比2少的,所以由5决定0的数量
比如100!
100/5=20,100/25=4,100/75=1
所以100!里有20+4+1个0
so easy~
第六题练手题
Google is Feeling Lucky
Problem:648
Time Limit:1000ms
Memory Limit:65536K
Description

Google is one of the most famous Internet search engines which hosts and develops a number of Internet-based services and products. On its search engine website, an interesting button “I’m feeling lucky” attracts our eyes. This feature could allow the user skip the search result page and goes directly to the first ranked page.
Amazing! It saves a lot of time.
The question is, when one types some keywords and presses “I’m feeling lucky” button, which web page will appear? Google does a lot and come up with excellent approaches to deal with it. In this simplified problem, let us just consider that Google assigns every web page an integer-valued relevance. The most related page will be chosen. If there is a tie, all the pages with the highest relevance are possible to be chosen.
Your task is simple, given 10 web pages and their relevance. Just pick out all the possible candidates which will be served to the user when “I’m feeling lucky”.
大意:有t组数据,每组数据10行,每行由一个字符串和一个数字组成
输出最大输出装所对应的字符串
Input

The input contains multiple test cases. The number of test cases T is in the first line of the input file.
For each test case, there are 10 lines, describing the web page and relevance. Each line contains a character string without any blank characters denoting the URL of this web page and an integer Vi denoting the relevance of this web page. The length of the URL is between 1 and 100 inclusively. (1<=Vi<=100)

Output

For each test case, output several lines which are the URLs of the web pages which are possible to be chosen. The order of the URLs is the same as the input.
Please look at the sample output for further information of output format.

Sample Input

2
www.youtube.com 1
www.google.com 2
www.google.com.hk 3
www.alibaba.com 10
www.taobao.com 5
www.bad.com 10
www.good.com 7
www.fudan.edu.cn 8
www.university.edu.cn 9
acm.university.edu.cn 10
www.youtube.com 1
www.google.com 2
www.google.com.hk 3
www.alibaba.com 11
www.taobao.com 5
www.bad.com 10
www.good.com 7
www.fudan.edu.cn 8
www.university.edu.cn 9
acm.university.edu.cn 10

Sample Output

Case #1:
www.alibaba.com
www.bad.com
acm.university.edu.cn
Case #2:
www.alibaba.com
直接做,签到现场赛题

#include 
#include 
using namespace std;

int main()
{
    char a[10][100];
    int b[10],max1,t;
    cin>>t;
    for(int j=1;j<=t;j++)
    {
        for(int i=0;i<10;i++)
          cin>>a[i]>>b[i];
        max1=-999;
        for(int i=0;i<10;i++)
            if(max1<b[i]) max1=b[i];
        cout<<"Case#"<<j<<";"<<endl;
        for(int i=0;i<10;i++)
            if(b[i]==max1)cout<<a[i]<<endl;
    }
    return 0;
}


下一期:acm训练题 初级(2)

你可能感兴趣的:(acm训练题 初级(1))