Death to Binary? POJ - 2116

原oj点击打开链接

vj点击打开链接

 这个题 说实话 我wa 了很多发  wa到怀疑人生 好不容易看别人的代码  不wa了 结果 给我出来一个 Presentation Error(格式错误)    搞得我头皮 发麻  主要了解一下的就是 前面需要 的空格 以及 每行需要 最后答案的 长度 这个就可以了 具体的题意 谷歌一下就行 感觉这道题 有点逗 ~~~~

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
using namespace std;
long long int mm[60];
string a,b,c;
long long int lena,lenb,suma,sumb,sumc,lenc;
void init()
{
    mm[0]=1,mm[1]=2;
    for(int i=2; i<=55; i++)
        mm[i]=mm[i-1]+mm[i-2];
}
long long int gett(long long int len,string t)
{
    long long int sum=0;
    for(int i=0; i<len; i++)
    {
        if(t[i]=='1')
        {
            sum+=mm[i];
        }
    }
    return sum;
}
void change(long long int aa,string &s)
{
    int i;
    for(i=50; i>=0; i--)
    {
        if(aa>=mm[i])
            break;
    }
    //printf("%d\n",i);
    for(int t=i;t>=0; t--)
    {
        if(aa>=mm[t])
            s+='1',aa-=mm[t];
        else
            s+='0';
    }
    if(i<0)
        s+='0';
}
int main()
{
    init();
    while(cin>>a>>b)
    {
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());
        lena=a.size();
        lenb=b.size();
        lenc=lena+lenb;
        suma=gett(lena,a);
        sumb=gett(lenb,b);
        //printf("%d %d\n",suma,sumb);
        sumc=suma+sumb;
        a.clear();
        b.clear();
        change(suma,a);
        // printf("1\n");
        change(sumb,b);
        change(sumc,c);
        printf("  ");for(int i=0;i<c.size()-a.size();i++)printf(" ");cout<<a<<endl;
        printf("+ ");for(int i=0;i<c.size()-b.size();i++)printf(" ");cout<<b<<endl;
        printf("  ");for(int i=0; i<c.size(); i++)printf("-");printf("\n");
        printf("  "),cout<<c<<endl;
        printf("\n");
        a.clear();
        b.clear();
        c.clear();
    }
    return 0;
}

阅读更多

Death to Binary?

08-29

The group of Absurd Calculation Maniacs has discovered a great new way how to count. Instead of using the ordinary decadic numbers, they use Fibonacci base numbers. Numbers in this base are expressed as sequences of zeros and ones similarly to the binary numbers, but the weights of bits (fits?) in the representation are not powers of two, but the elements of the Fibonacci progression (1, 2, 3, 5, 8,... - the progression is defined by F0 = 1, F1 = 2 and the recursive relation Fn = Fn-1 + Fn-2 for n >= 2).nnFor example 1101001Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40.nnYou may observe that every integer can be expressed in this base, but not necessarily in a unique way - for example 40 can be also expressed as 10001001Fib. However, for any integer there is a unique representation that does not contain two adjacent digits 1 - we call this representation canonical. For example 10001001Fib is a canonical Fibonacci representation of 40.nnTo prove that this representation of numbers is superior to the others, ACM have decided to create a computer that will compute in Fibonacci base. Your task is to create a program that takes two numbers in Fibonacci base (not necessarily in the canonical representation) and adds them together.nnInputnnThe input consists of several instances, each of them consisting of a single line. Each line of the input contains two numbers X and Y in Fibonacci base separated by a single space. Each of the numbers has at most 40 digits. The end of input is not marked in any special way.nnOutputnnThe output for each instance should be formated as follows:nnThe first line contains the number X in the canonical representation, possibly padded from left by spaces. The second line starts with a plus sign followed by the number Y in the canonical representation, possibly padded from left by spaces. The third line starts by two spaces followed by a string of minus signs of the same length as the result of the addition. The fourth line starts by two spaces immediately followed by the canonical representation of X + Y. Both X and Y are padded from left by spaces so that the least significant digits of X, Y and X + Y are in the same column of the output. The output for each instance is followed by an empty line.nnSample Inputnn11101 1101n1 1nSample Outputnn 100101n+ 10001n -------n 1001000nn 1n+ 1n --n 10n

Intricate to death?

05-05

Obviously, some of the descriptions are deliberately lengthened, in order to make the whole paragraph more fun. In this problem, your task is similar: given a set of rules, find a description that is at least A characters and at most B characters long. If there are more than one possible description satisfying the condition above, choose the one that comes first lexicographically. Note that shorter answers are NOT always better than longer ones.nnBTW: Rules like "Lixiaoyao: Lixiaoyao" looks rather stupid, so don't print things like that, even if no other solutions can be found.nnInputnnThe first line contains a single integer T (T <= 10), the number of test cases. Each case begins with an integer n (1 <= n <= 5), the number of rules, followed by n lines, each describing a rule. Each rule begins with the character's name, then a colon, then a single space, and the description. The description is a list of tokens separated by a single space, with no leading spaces or trailing spaces, followed by a period. Each token is either a word consisting of only lowercase letters, or a character's name, or a character's name, followed by an apostrophe('), then a lowercase letter 's'. A character's name is always an uppercase letter followed by zero or more lowercase letters. No two characters will have the same name, but not all character names appear in the left side of a rule. After the ruleset, you'll be given a single integer q (1 <= q <= 5) on the next line, indicating the number of queries. Each query appears on its own line, containing the name of the character in question, then two integers A and B (1 <= A <= B <= 100), explained above.nnEach rule is at least 15 characters, at most 100 characters, containing at least one lowercase token. On the right side of each rule, there will be at most 3 characters involved, all distinct (though one of them may appear on the left side of the rule), and there are no more than 10000 different descriptions for any character in questions, if we only count descriptions only length <= B.nnOutputnnFor each test case, print the case number in the first line, then q answers in the following q lines, one for each line. If there is no solution, print 'No solution.' (without quote), otherwise a sentence describing the character. The sentence should be in the same format as a rule in the input. The length of the sentence (including everything: the character's name, the colon, the space, the description, and the final period) should be at least A and at most B.nSample Inputnn2n2nAlice: Bob's mother.nBob: Alice's son.n2nAlice 20 20nBob 24 25n4nA: B's girlfriend.nB: A's boyfriend.nC: the only person who knows that A and B love each other.nA: D's smallest daughter.n5nA 1 100nC 1 50nC 70 80nC 71 80nC 82 90nSample OutputnnCase 1:nAlice: Bob's mother.nBob: Bob's mother's son.nCase 2:nA: A's boyfriend's girlfriend.nNo solution.nC: the only person who knows that A and A's boyfriend love each other.nC: the only person who knows that B's girlfriend and B love each other.nC: the only person who knows that A and B's girlfriend's boyfriend love each other.

没有更多推荐了,返回首页