第十五届现场编程比赛初赛(提高组)

来源:互联网 发布:网络教育考上研究生 编辑:程序博客网 时间:2024/06/05 17:09

A :次方的和

编程可以解决很多数学问题。请编程实现求一个数列 {n^K}(K为常数)的前N项和SN。

Sn = 1^K+2^K+3^K+4^K+……n^K.

Input Specification: 

第一行输入一个自然数T,表示有T组数据。
下面T行,每行输入两个整数N, K。其中0 < N < 50, 0 < K < 11。
N表示项数,K是数列元素的指数。

Output Specification: 

每组数据对应一个整数SN。

Sample Input:

1

1 2

Sample Output:

1

本题要注意的有两点:

1、数据比较大,要用long long存储。

2、不能用math.h里的pow(),即使用floor(pow()+0.5)也会存在误差。

<span style="font-size:18px;">#include <stdio.h>#include <math.h>//自己实现Pow函数long long Pow(int a, int b){    long long ans = 1;    while(b --)    {        ans *= a;    }    return ans;}int main(){   int T, N, K, i;   long long s = 0;   scanf("%d", &T);   while(T --)   {       scanf("%d%d", &N, &K);       for(i = 1; i <= N; i ++)       {           s += Pow(i, K);//           s += pow(i, K);                //一定有误差,自己测试。//           s += floor(pow(i, K) + 0.5);   //floor(m)是取不超过m的最大整数,但是还是不能避免误差。       }       printf("%lld\n", s);       s = 0;   }   return 0;}</span>


测试数据:

Input:

4
22 10
41 10
44 4
32 1


Output:

67403375450475
57013865269490101
34885686

528

B:找硬币

CapouisSensei有一天去超市购物,收银员发现柜台因为没有准备足够的硬币,导致购物的队伍陷入了瘫痪状态。收银员就去隔壁柜台借一些硬币,但是隔壁柜台也没有充足的硬币,所以收银员只能尽可能得少借些硬币。假设收银员已经知道了当前购物队伍中每个人需要找零或者付零的数目,且已经没有人再进入排队队伍。

Input Specification:

第一行输入为当前队伍的人数N.

第二行中有N个整数,表示顾客需要找零的数目(以负数表示)或顾客付出的硬币的数目(用整数表示)

Output Specification:

每行一组数据,输出一开始收银员应该拥有的最少的硬币数。

Sample Input:

7

-1 +1 +2 +3 -5 -2 +1

Sample Output:

2

这题只要保证每次"交易"结束后,手上的钱大于0即可,少多少补多少。

<span style="color:#009900;">#include <string>#include <cstdio>#include <iostream>#include <algorithm> using namespace std; int main(){    int sum = 0;    int cost = 0;    int T, x;    scanf("%d", &T);    while(T--)    {        scanf("%d", &x);        sum += x;        if(sum < 0)        {            cost += (-sum);            sum = 0;        }    }    printf("%d\n", cost);}</span>


 C:写出这个数

读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

Input Specification:

每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100

Output Specification:

在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。

Sample Input:

1234567890987654321123456789

Sample Output:

yi san wu


用字符串来代替自然数N,然后逐位统计就可以了。

<span style="color:#009900;">#include <cstdio>#include <cstring>using namespace std;int main(){    char num[10000];    cin >> num;    int length = strlen(num), sum = 0, i, N = 0;    int ans[10000];    for(i = 0; i< length; i++)        sum += num[i] - '0';    while(sum != 0)    {        ans[N] = sum % 10;        sum /= 10;        N++;    }    for(i = N - 1; i >= 0; i--)    {        if(ans[i] == 1)            printf("yi");        else if(ans[i] == 2)            printf("er");        else if(ans[i] == 3)            printf("san");        else if(ans[i] == 4)            printf("si");        else if(ans[i] == 5)            printf("wu");        else if(ans[i] == 6)            printf("liu");        else if(ans[i] == 7)            printf("qi");        else if(ans[i] == 8)            printf("ba");        else if(ans[i] == 9)            printf("jiu");        else if(ans[i] == 0)            printf("ling");        if(i != 0)            printf(" ");    }    printf("\n");    return 0;}</span>


D:EOF队的牛肉干

在ACM比赛的参赛队伍中,有一个叫做EOF的队伍,在共同的集训生活中,大家建立了深厚的友谊,队长准备做点什么来纪念这段激情燃烧的岁月,想了一想,他从家里拿来了一块上等的牛肉干,准备在上面刻下一个长度为n的只由"E" "O" "F"三种字符组成的字符串(可以只有其中一种或两种字符,但绝对不能有其他字符),队长同时禁止在串中出现O相邻的情况,他认为,"OO"看起来就像发怒的眼睛,效果不好。

你能帮EOF队的队长算一下一共有多少种满足要求的不同的字符串吗?


Input Specification:
输入数据包含多个测试实例,每个测试实例占一行,由一个整数n组成,(0<n<40)。


Output Specification:
对于每个测试实例,请输出全部的满足要求的刻法,每个实例的输出占一行。


Sample Input:
1
2
Sample Output:
3
8
来源:杭电OJ 2047

题解:可以参考博客http://blog.sina.com.cn/s/blog_69fc13c50100ldci.html

<span style="font-size:18px;color:#009900;">#include<iostream>using namespace std;int main(){    int n;    long long a[41]= {0,3,8};    for(int i=3; i<41; i++)        a[i]=2*(a[i-1]+a[i-2]);    while(cin>>n)        printf("%lld\n",a[n]);    return 0;}</span>


E:Stockbroker Grapevine

Stockbrokers areknown to overreact to rumours. You have been contracted to develop a method ofspreading disinformation amongst the stockbrokers to give your employer thetactical edge in the stock market. For maximum effect, you have to spread therumours in the fastest possible way.
       Unfortunately for you, stockbrokersonly trust information coming from their "Trusted sources" This meansyou have to take into account the structure of their contacts when starting arumour. It takes a certain amount of time for a specific stockbroker to passthe rumour on to each of his colleagues. Your task will be to write a programthat tells you which stockbroker to choose as your starting point for therumour, as well as the time it will take for the rumour to spread throughoutthe stockbroker community. This duration is measured as the time needed for thelast person to receive the information.

Input Specification:

Your program will input data for different sets of stockbrokers.Each set starts with a line with the number of stockbrokers. Following this isa line for each stockbroker which contains the number of people who they havecontact with, who these people are, and the time taken for them to pass themessage to each person. The format of each stockbroker line is as follows: Theline starts with the number of contacts (n), followed by n pairs of integers,one pair for each contact. Each pair lists first a number referring to thecontact (e.g. a '1' means person number one in the set), followed by the timein minutes taken to pass a message to that person. There are no specialpunctuation symbols or spacing rules.
       Each person is numbered 1 throughto the number of stockbrokers. The time taken to pass the message on will bebetween 1 and 10 minutes (inclusive), and the number of contacts will rangebetween 0 and one less than the number of stockbrokers. The number ofstockbrokers will range from 1 to 100. The input is terminated by a set ofstockbrokers containing 0 (zero) people.

Output Specification:

For each set ofdata, your program must output a single line containing the person who resultsin the fastest message transmission, and how long before the last person willreceive any given message after you give it to this person, measured in integerminutes.
It is possible that your program will receive a network of connections thatexcludes some persons, i.e. some people may be unreachable. If your programdetects such a broken network, simply output the message "disjoint".Note that the time taken to pass the message from person A to person B is notnecessarily the same as the time taken to pass it from B to A, if suchtransmission is possible at all.

Sample Input:

3

2 2 4 3 5

2 1 2 3 6

2 1 2 2 2

5

3 4 4 2 8 5 3

1 5 8

4 1 6 4 10 2 7 5 2

0

2 2 5 1 5

0

Sample Output:

3 2

3 10

最短路问题我现在正在学,所以就先贴上别人的博客,回头有时间补上。。。

http://www.cnblogs.com/Fatedayt/archive/2011/09/10/2173066.html



F:零一迷离

CapouisSensei上组合数学课时,陈老师在黑板上制定了一套游戏规则,来考验同学们的快速理解能力。“010”中出现01或者10的总次数为2。现在有M个0,N个1,用这M+N个数字组成一个字符串,出现01或10的总次数为K,问有多少个这样的字符串。

Input Specification:

第一行输入一个整数T, 表示有T组测试数据。

后面的T行,共有T组测试数据。每一行输入N,M,K三个非负整数。

Output Specification:

每一行输出可以组成的字符串的数目。

Sample Input:

1

5 4 4

Sample Output:

30

这题是自己写的,估计网上找不到代码,就解释一下,没想到好方法,就是穷举所有01字符串(全排列),然后从左到右依次查找。当然要注意剪枝。偷懒用了STL和string类。

<span style="color:#009900;">#include <string>#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int main(){    int l, y, k;    int ans, cnt;    string s;    scanf("%d%d%d", &y, &l, &k);    ans = cnt = 0;    for(int i = 0; i < l; i ++)    {        s += "0";    }    for(int i = 0; i < y; i ++)    {        s += "1";    }    do    {        int i = 0;        while(cnt <= k && (i = s.find("01", i)) != string::npos)        {            cnt ++;            i ++;        }        i = 0;        while(cnt <= k && (i = s.find("10", i)) != string::npos)        {            cnt ++;            i ++;        }        if(cnt == k)        {            ans ++;        }        cnt = 0;    }    while(next_permutation(s.begin(), s.end()));    printf("%d\n", ans);    return 0;}</span>

如果认为自己的程序正确,但是运行提示答案错误,需要核对测试数据的,请联系我。






0 0
原创粉丝点击