第四场个人训练赛(未完成)

A-HDU - 1008

Elevator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 72929    Accepted Submission(s): 40148


Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
 

Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
 

Output
Print the total time on a single line for each test case.
 

Sample Input
  
  
1 2
3 2 3 1
0
 

Sample Output
  
  
17
41
 

[分析]
意思是电梯上一楼花6秒,下一楼花4秒,停下来花5秒。
计算具体行为所花费的时间。

分析在代码中

[代码]

#include<cstdio>
int main()
{
    int a[105];
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n)break;
        int ans=0;
        int dq=0;//当前楼层
        int x;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            if(x>dq)ans=ans+(x-dq)*6;//如果输入楼层大于当前楼层那么乘6
            else ans=ans+(dq-x)*4;//小于乘4
            ans=ans+5;//无论到哪,都停5秒;
            dq=x;//更新当前楼层
        }
        printf("%d\n",ans);
    }
}

B-HDU - 1003
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 270669 Accepted Submission(s): 64346

Problem Description
Given a sequence a[1],a[2],a[3]……a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

[分析]
最大连续子序列
sum记录前缀和(之前数字的和)
将sum的数值画入坐标系后思路就很明朗了
找出一个min为当前最小的sum,
找出一个max为当前min之后的最大的sum(注意max一定要在min后面,为什么自己思考)
题目的含义要我们得到的最大数应该是max-(min的前面一个数)
所以我定义了一个premin,即max-premin,这个就是某个大序列的和,
我们只要比较每个可能的大序列的和就可以的到答案。
得到位置很简单,看代码就懂了。

[代码]

#include<cstdio>
#define MAX 0x3f3f3f3f
#define MIN -0x3f3f3f3f
int a[100005];
int main()
{
    int t,kase=0;
    a[0] = 0;
    scanf("%d", &t);
    while (t--)
    {
        int n,max=MIN,dif=MIN,sum=0,premin=MAX;
        int AnsStarPos, AnsEndPos, NowStarPos, NowEndPos;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            if (sum < premin)
            {
                premin = sum;
                max = MIN;
                NowStarPos = i;
            }
            sum += a[i];
            if (sum > max)
            {
                max = sum;
                NowEndPos = i;
                if (max - premin > dif)
                {
                    dif = max - premin;
                    AnsStarPos = NowStarPos;
                    AnsEndPos = NowEndPos;
                }

            }
        }
        if (kase)printf("\n");
        printf("Case %d:\n", ++kase);
        printf("%d %d %d\n", dif,AnsStarPos,AnsEndPos);
    }
}

D-HDU - 1998

奇数阶魔方

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4661    Accepted Submission(s): 2640


Problem Description
一个 n 阶方阵的元素是1,2,…,n^2,它的每行,每列和2条对角线上元素的和相等,这样
的方阵叫魔方。n为奇数时我们有1种构造方法,叫做“右上方” ,例如下面给出n=3,5,7时
的魔方.
3
8 1 6
3 5 7
4 9 2
5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
第1行中间的数总是1,最后1行中间的数是n^2,他的右边是2,从这三个魔方,你可看出“右
上方”是何意。
 

Input
包含多组数据,首先输入T,表示有T组数据.每组数据1行给出n(3<=n<=19)是奇数。
 

Output
对于每组数据,输出n阶魔方,每个数占4格,右对齐
 

Sample Input
  
  
2
3
5
 

Sample Output
  
  
8 1 6
3 5 7
4 9 2
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

[分析]
题目不难,就是按照题目的意思做就可以了。
分析在代码中。

[代码]

#include<cstdio>
#include<cstring>
int main()
{
    int n;
    int t;
    int a[22][22];
    //freopen("out.txt","w",stdout);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        int x=0;
        int y=n/2;//x,y初始定位在第零行的最中间
        int t=n*n;//t是:有多少数字需要写;
        int num=1;
        while(t--)
        {
            a[x][y]=num++;
            if(a[(x+n-1)%n][(y+n+1)%n]==0)//确定右上是否有数字(看不懂就多琢磨一下,有益处)
            {
                x=(x+n-1)%n;//确定x的位置
                y=(y+n+1)%n;//确定y的位置
            }
            else x=(x+n+1)%n;//如果右上被占,那么移到下方
        }

            for(int i=0;i<n;i++)
            {
                int flag=1;
                for(int j=0;j<n;j++)
                {
                    printf("%4.d",a[i][j]);//题目中比较坑的,对其!!
                }
                printf("\n");
            }
    }
}

F-HDU - 1020

Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46814    Accepted Submission(s): 20792


Problem Description
Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:

1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.

2. If the length of the sub-string is 1, ‘1’ should be ignored.
 

Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only ‘A’ - ‘Z’ and the length is less than 10000.
 

Output
For each test case, output the encoded string in a line.
 

Sample Input
  
  
2
ABC
ABBCCC
 

Sample Output
  
  
ABC
A2B3C
 

[分析]
题目意思就是当一个字母连续出现时,就以连续出现的个数加这个字母替代原来在字符串中的位置。如ABBCCCA改为A2BCA。

[代码]

#include<cstdio>
#include<cstring>
int main()
{
    int t;
    char a[10005];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",a);
        int len=strlen(a);
        int same=1;
        for(int i=0;i<len;i++)
        {
            if(same==1&&a[i]!=a[i+1])printf("%c",a[i]);
            else if(a[i]==a[i+1])
            {
                same++;
            }
            else if(same!=1&&a[i]!=a[i+1])
            {
                printf("%d%c",same,a[i]);
                same=1;
            }
        }
        printf("\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值