zoukankan      html  css  js  c++  java
  • HDU

    先上题目:

    Continued Fraction

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 332    Accepted Submission(s): 106


    Problem Description
    Dumbear loves numbers very much.
    One day Dumbear found that each number can be expressed as a continued fraction. See below.

    Formally, we say a number k can be expressed as a continued faction if

    where a0, a1, …, an are positive integers except that a0 maybe be 0 and an cannot be 1.
    Dumbear also found a sequence which looks like the Farey sequence. Initially the sequenceand if we insert an elementbetween all the two adjacent element,in Di, then we get a sequence Di+1. So you can seeandAssume initially you are on the elementin D0, and if now you are on the element k in Di, then if you go left(‘L’)(or right(‘R’)) you will be on the left(or right) element of k in Di+1. So a sequence composed of ‘L’ and ‘R’ denotes a number. Such as ‘RL’ denotes the number 

    Now give you a sequence composed of ‘L’ and ‘R’, you should print the continued fraction form of the number. You should use ‘-‘ to show the vinculum(the horizontal line), you should print one space both in front and back of ‘+’, and all parts up or down the vinculum should be right aligned. You should not print unnecessary space, ‘-‘ or other character. See details in sample.
     
    Input
    There are several test cases in the input.
    For each test case, there is a single line contains only a sequence composed of ‘L’ and ‘R’. The length of the sequence will not exceed 10000.
    The input terminates by end of file marker.
     
    Output
    For each test case, output the continued fraction form of the number which the input sequence denotes. The total amount of output will not exceed 4MB.
     
    Sample Input
    LR
    RL
     
    Sample Output
               1
    0 + -----
               1
          1 + -
               2
         1
    1 + -
         2
     
        题意:给出一个只有L和R的字符串,根据它给出的定义移动,然后将最终的结果按照他给出的那种分式输出。
        模拟+找规律。首先先根据它的定义模拟它的移动,然后可以根据自己的分析求出a[]数组的每一项是多少,然后在按照它的格式输出。这里求a[]数组分析一下就可以知道怎么怎么求了,然后关于输出,通过前后项的长度关系就可以得出要输出多少个空格多少'-'了,但是这样做的话直接提交可能会wa,这里可以通过输出前几项找一下规律。将L,LL,LR,LLL,LLR,LRL,LRR以及R,RL,RR,RLL,RLR,RRL,RRR(也就是前几项的变化的输有情况)输出,然后通过观察,我们可以发现,对于第2项开始,如果当前项等于前一项,那么a[tot]++,否则a[tot]--;tot++;a[tot]=2;当前其实如果写得好的话可以顺便把前一项的情况也包含在公式里面。然后直接构造a[],在打印,速度和准确性上面都会有保证。
     
    上代码:
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #define MAX 10002
     6 #define PUTS(M,x) for(int k=0;k<M;k++) putchar(x)
     7 #define ll long long
     8 using namespace std;
     9 
    10 char c[MAX];
    11 int l;
    12 ll a[MAX];
    13 char ss[MAX<<4][20];
    14 int len[MAX];
    15 int tot;
    16 typedef struct{
    17     ll fz,fm;
    18 }fs;
    19 
    20 fs A[3],p;
    21 
    22 void cons(int l){
    23     if(c[0]=='L'){
    24         a[0]=0; a[1]=2; tot=1;
    25     }else{
    26         a[0]=2; tot=0;
    27     }
    28     for(int i=1;i<l;i++){
    29         if(c[i]==c[i-1]) a[tot]++;
    30         else{
    31             a[tot]--;
    32             a[++tot]=2;
    33         }
    34     }
    35     tot++;
    36 }
    37 
    38 int main()
    39 {
    40     int M;
    41     //freopen("data.txt","r",stdin);
    42     while(scanf("%s",c)!=EOF){
    43         l=strlen(c);
    44 //        A[0].fz=0;  A[0].fm=1;
    45 //        A[1].fz=1;  A[1].fm=1;
    46 //        A[2].fz=1;  A[2].fm=0;
    47 //        for(int i=0;i<l;i++){
    48 //            if(c[i]=='L'){
    49 //                A[2]=A[1];
    50 //            }else{
    51 //                A[0]=A[1];
    52 //            }
    53 //            A[1].fz=A[0].fz+A[2].fz;
    54 //            A[1].fm=A[0].fm+A[2].fm;
    55 //        }
    56 //        tot=0;
    57 //        p=A[1];
    58 //        while(1){
    59 //            a[tot]=p.fz/p.fm;
    60 //            sprintf(ss[tot],"%I64d",a[tot]);
    61 //            tot++;
    62 //            p.fz=p.fz%p.fm;
    63 //            if(p.fz==0) break;
    64 //            else if(p.fz==1){
    65 //                a[tot]=p.fm;
    66 //                sprintf(ss[tot],"%I64d",a[tot]);
    67 //                tot++;
    68 //                break;
    69 //            }
    70 //            swap(p.fz,p.fm);
    71 //        }
    72         cons(l);
    73         for(int i=0;i<tot;i++) sprintf(ss[i],"%I64d",a[i]);
    74         len[tot-1]=strlen(ss[tot-1]);
    75         len[tot-2]=strlen(ss[tot-2]) + 3 + strlen(ss[tot-1]);
    76         for(int i=tot-3;i>=0;i--){
    77             len[i]=strlen(ss[i]) + 3 + len[i+1];
    78         }
    79 
    80 //        for(int i=0;i<tot;i++) printf("%I64d ",a[i]);
    81 //        printf("
    ");
    82 //        for(int i=0;i<tot;i++) printf("%d ",len[i]);
    83 //        printf("
    ");
    84         M=len[0];
    85         for(int i=0;i<tot-1;i++){
    86             PUTS(M-1,' ');  putchar('1');   putchar('
    ');
    87             PUTS(M-len[i],' ');
    88             printf("%s + ",ss[i]);
    89             PUTS(len[i+1],'-');
    90             putchar('
    ');
    91         }
    92         PUTS(M-(int)strlen(ss[tot-1]),' ');
    93         printf("%s",ss[tot-1]);
    94         printf("
    ");
    95     }
    96     return 0;
    97 }
    /*3556*/
     
  • 相关阅读:
    MySQL之数据表的插入内容 空与非空(六)
    输出杨辉三角形
    输入三个double型的数据,放入到a,b,c三个变量中去,使用条件结构与交换逻辑将这三个变量中的值从小到大排列。
    软件测试
    过程设计工具
    设计原理
    总体设计
    生活,也让别人生活
    计算器案例
    需求分析
  • 原文地址:https://www.cnblogs.com/sineatos/p/3917222.html
Copyright © 2011-2022 走看看