先上题目:

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[],在打印,速度和准确性上面都会有保证。
 
上代码:
 
 #include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define MAX 10002
#define PUTS(M,x) for(int k=0;k<M;k++) putchar(x)
#define ll long long
using namespace std; char c[MAX];
int l;
ll a[MAX];
char ss[MAX<<][];
int len[MAX];
int tot;
typedef struct{
ll fz,fm;
}fs; fs A[],p; void cons(int l){
if(c[]=='L'){
a[]=; a[]=; tot=;
}else{
a[]=; tot=;
}
for(int i=;i<l;i++){
if(c[i]==c[i-]) a[tot]++;
else{
a[tot]--;
a[++tot]=;
}
}
tot++;
} int main()
{
int M;
//freopen("data.txt","r",stdin);
while(scanf("%s",c)!=EOF){
l=strlen(c);
// A[0].fz=0; A[0].fm=1;
// A[1].fz=1; A[1].fm=1;
// A[2].fz=1; A[2].fm=0;
// for(int i=0;i<l;i++){
// if(c[i]=='L'){
// A[2]=A[1];
// }else{
// A[0]=A[1];
// }
// A[1].fz=A[0].fz+A[2].fz;
// A[1].fm=A[0].fm+A[2].fm;
// }
// tot=0;
// p=A[1];
// while(1){
// a[tot]=p.fz/p.fm;
// sprintf(ss[tot],"%I64d",a[tot]);
// tot++;
// p.fz=p.fz%p.fm;
// if(p.fz==0) break;
// else if(p.fz==1){
// a[tot]=p.fm;
// sprintf(ss[tot],"%I64d",a[tot]);
// tot++;
// break;
// }
// swap(p.fz,p.fm);
// }
cons(l);
for(int i=;i<tot;i++) sprintf(ss[i],"%I64d",a[i]);
len[tot-]=strlen(ss[tot-]);
len[tot-]=strlen(ss[tot-]) + + strlen(ss[tot-]);
for(int i=tot-;i>=;i--){
len[i]=strlen(ss[i]) + + len[i+];
} // for(int i=0;i<tot;i++) printf("%I64d ",a[i]);
// printf("\n");
// for(int i=0;i<tot;i++) printf("%d ",len[i]);
// printf("\n");
M=len[];
for(int i=;i<tot-;i++){
PUTS(M-,' '); putchar(''); putchar('\n');
PUTS(M-len[i],' ');
printf("%s + ",ss[i]);
PUTS(len[i+],'-');
putchar('\n');
}
PUTS(M-(int)strlen(ss[tot-]),' ');
printf("%s",ss[tot-]);
printf("\n");
}
return ;
}

/*3556*/

 

HDU - 3556 - Continued Fraction的更多相关文章

  1. CSUOJ 1638 Continued Fraction

    1638: Continued Fraction Time Limit: 1 Sec  Memory Limit: 128 MB Description Input Output Sample Inp ...

  2. hdu 6223 Infinite Fraction Path

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...

  3. HDU - 6223 Infinite Fraction Path (倍增+后缀数组)

    题意:给定一个长度为n(n<=150000)的字符串,每个下标i与(i*i+1)%n连边,求从任意下标出发走n步能走出的字典序最大的字符串. 把下标看成结点,由于每个结点有唯一的后继,因此形成的 ...

  4. Continued Fractions CodeForces - 305B (java+高精 / 数学)

    A continued fraction of height n is a fraction of form . You are given two rational numbers, one is ...

  5. CF 305B——Continued Fractions——————【数学技巧】

    B. Continued Fractions time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】

    任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...

  7. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  8. BCTF warmup 50

    这是一道关于RSA的解密题:首先,我们要明白,通常是公钥加密.私钥解密,私钥签名.公钥验证.这个题目中给出的是一个公钥和一段密文. 刚开始一直以为和验证签名有关,费劲脑汁也想不出来怎么办.下面介绍些思 ...

  9. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

随机推荐

  1. IDEA Spark Streaming Kafka数据源-Consumer

    import org.apache.spark.SparkConf import org.apache.spark.streaming.kafka.KafkaUtils import org.apac ...

  2. [Apple开发者帐户帮助]八、管理档案(1)创建开发配置文件

    您可以在开发人员帐户中创建开发配置文件,以便在Xcode中手动签署应用程序时使用. 在开始之前,您需要一个App ID,一个或多个开发证书以及一个或多个已注册的设备.您可以使用Xcode为您管理的Ap ...

  3. Vue Router的params和query传参的使用和区别

    vue页面跳转有两种方式分别是:name和path this.$router.push({name: 'HelloWorld2}) this.$router.push({path: '/hello-w ...

  4. vagrant使用centos的环境安装..

    vagrant这货挺好用的..简要就是, 下好virtualbox, vagrant, 然后下个你需要的box. 然后vagrant box add boxname boxpath就行. 然后在合适的 ...

  5. Offer收割_5

    训练 投入 欲望.  ---贾森博尔特 第一题:二分枚举答案,check时候模拟一下即可. 时间复杂度: O(n*logn). 第二题: 描述 小Hi在虚拟世界中有一只小宠物小P.小P有K种属性,每种 ...

  6. JdbcTemplate:Jdbc模板和数据库元数据

    通过 Jdbc .C3P0 .Druid 的使用我们会发现即使我们做了工具的封装,但重复性的代码依旧很多.我们可以通过 JdbcTemplate 即 Jdbc 模板来使我们的代码更加简洁,逻辑更加清晰 ...

  7. Ajax——瀑布流

    基本概念 1.宽度是一致的,高度上参差不齐 2.新增内容优先放置在最矮的地方 核心难点 1.用一个数组存储每列的高度值 2.新值添加到值最小索引上,每次替换更新数组 插件使用 1.$.fn.exten ...

  8. CSS——伪类

    在a标签中运用最多: 1.a:link {color: #FF0000} /* 未访问的链接 */ 2.a:visited {color: #00FF00} /* 已访问的链接 */ 3.a:hove ...

  9. MyBatis入门2_增删改查+数据库字段和实体字段不一致情况

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 当数据库字段和实体bean中属性不一致时 之前数据库P ...

  10. js弹开页面并调用方法

    每次重新写一个功能的时候,都能发现以前写的并不太好,都可以改进,奇怪的是我还是我,为什么曾经的我就想不起来要这么写,比如下面两段代码 历史代码: if (infoTablePage != null) ...