hdu 1041 Computer Transformation

题目:hdu 1041 Computer Transformation

思路:打表,找规律

复制代码
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
string solve(string s)
{
    string str="";
    for(int i=0;i<s.size();i++)
        if(s[i]=='0')
            str+="10";
        else
            str+="01";
    return str;
}
int get(string s)
{
    int ans=0;
    for(int i=1;i<s.size();i++)
        if(s[i]=='0' && s[i-1]=='0')
            ans++;
    return ans;
}
int main()
{
    string s="1";
    for(int i=0;i<20;i++)
    {
        cout<<i<<":"<<get(s)<<endl;
        //cout<<i<<":"<<s<<":"<<get(s)<<endl;
        s=solve(s);
    }
    return 0;

}
打表
复制代码
复制代码
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        BigInteger dp[]=new BigInteger[1010];
        dp[0]=BigInteger.ZERO;
        dp[1]=BigInteger.ZERO;
        BigInteger two=BigInteger.valueOf(2);
        for(int i=2;i<1010;i++)
        {
            if(i%2==1)
                dp[i]=dp[i-1].multiply(two).subtract(BigInteger.ONE);
            else
                dp[i]=dp[i-1].multiply(two).add(BigInteger.ONE);
        }
        while(cin.hasNext())
        {
            int n=cin.nextInt();
            System.out.println(dp[n]);
        }
    }

}
AC代码
复制代码

 

posted @ 2013-11-15 14:56  over_flow  阅读(219)  评论(0)    收藏  举报
编辑推荐:
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
· 大数据高并发核心场景实战,数据持久化之冷热分离
· 运维排查 | SaltStack 远程命令执行中文乱码问题
阅读排行:
· C#.Net筑基-优雅LINQ的查询艺术
· 博客园众包平台:诚征3D影像景深延拓实时处理方案(预算8-15万)
· Cursor生成UI,加一步封神
· 为什么说方法的参数最好不要超过4个?
· 一个基于 .NET 8 开源免费、高性能、低占用的博客系统
点击右上角即可分享
微信分享提示