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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用状态机编写的Binary-to-Gray代码(VHDL): ``` library ieee; use ieee.std_logic_1164.all; entity binary_to_gray is port ( binary_in : in std_logic_vector(3 downto 0); gray_out : out std_logic_vector(3 downto 0) ); end binary_to_gray; architecture Behavioral of binary_to_gray is type state_type is (s0, s1, s2, s3); signal current_state, next_state : state_type; begin process(current_state, binary_in) begin case current_state is when s0 => gray_out <= binary_in; next_state <= s1; when s1 => gray_out <= binary_in xor (binary_in(1) & binary_in(0) & binary_in(3)); next_state <= s2; when s2 => gray_out <= binary_in xor (binary_in(2) & binary_in(1) & binary_in(0)); next_state <= s3; when s3 => gray_out <= binary_in xor (binary_in(3) & binary_in(2) & binary_in(1)); next_state <= s0; end case; end process; process(current_state) begin case current_state is when s0 => current_state <= s1; when s1 => current_state <= s2; when s2 => current_state <= s3; when s3 => current_state <= s0; end case; end process; next_state <= s0 when current_state = s3 else next_state; process(current_state, next_state) begin if current_state /= next_state then current_state <= next_state; end if; end process; end Behavioral; ``` 这个代码使用了一个状态机来实现Binary-to-Gray的转换。在每个时钟周期中,状态机会根据当前状态和输入的Binary值计算出输出的Gray值,并将状态更新为下一个状态。最终的Gray值将在下一个时钟周期中输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值