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

来源:互联网 发布:空难 尼尔森 知乎 编辑:程序博客网 时间:2024/05/16 08:39

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);    }}

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");    }}
原创粉丝点击