zoukankan      html  css  js  c++  java
  • HDU 1041 Computer Transformation (简单大数)

    Computer Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=1041

    Problem Description
    A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on. 

    How many pairs of consequitive zeroes will appear in the sequence after n steps? 
     
    Input
    Every input line contains one natural number n (0 < n ≤1000).
     
    Output
    For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
     
    Sample Input
    2
    3
     
    Sample Output
    1
    1
     
     解题思路:找出规律,F[n] = F[n-2] + 2n-3, 不过不能直接一起算,因为可能会超时;我们可以这样F[n] = F[n-2] + G[n-3]; (其中F[]代表多少对0,G[]代表有多少个1);
    G[n] = G[n-1] + G[n-1];所以这里就只需要大数加法就够了!
     
    解题代码:
     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <math.h>
     4 #include <string.h>
     5 #define CLE(name) memset(name, 0, sizeof (name))
     6 using namespace std;
     7 
     8 typedef __int64 LL;
     9 const int max_n = 1002;
    10 
    11 string F[2*max_n];
    12 string P2[2*max_n];
    13 int num1[max_n], num2[max_n];
    14 
    15 string add(string a, string b)
    16 {
    17     CLE(num1);
    18     CLE(num2);
    19     int len1 = a.length();
    20     int len2 = b.length();
    21     int k = 0;
    22     for (int i = len1 - 1; i >= 0; i --)
    23     {
    24         num1[k++] = a[i] - '0'; 
    25     }
    26     k = 0;
    27     for (int i = len2 - 1; i >= 0; i --)
    28     {
    29         num2[k++] = b[i] - '0'; 
    30     }
    31     int up = 0;
    32     for (int i = 0; i < max_n/2; i ++)
    33     {
    34         num1[i] = num1[i] + num2[i] + up;
    35         up = num1[i]/10;
    36         num1[i] %= 10;
    37     
    38     }
    39     for (k = max_n - 1; k >= 0; k --)
    40     {
    41         if (num1[k] != 0)
    42             break;
    43     }
    44     a = "";
    45     for (; k >= 0; k --)
    46         a += num1[k] + '0';
    47     return a;
    48 }
    49 
    50 void pow2()
    51 {
    52     P2[0] = "1";
    53     P2[1] = "2";
    54     for (int i = 2; i <= 1000; i ++)
    55     {
    56         P2[i] = add(P2[i-1], P2[i-1]);
    57     }
    58 }
    59 void deel()
    60 {
    61     F[0] = "0";
    62     F[1] = "0";
    63     F[2] = "1";
    64     for (int i = 3; i <= max_n; i ++)
    65     {
    66         F[i] = add(F[i-2], P2[i-3]);
    67     }
    68     return;
    69 }
    70 
    71 int main()
    72 {
    73     int n;
    74     pow2();
    75     deel();
    76     while (~scanf ("%d", &n))
    77     {
    78         cout << F[n] << endl;
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    Spring之bean二生命周期
    Hibernate注解:一对多外键关联
    List<Object>和List<String>
    Eclipse设置模板代码
    Hibernate注解:一对一主键关联
    解决tomcat默认45s启动超时的问题
    ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
    Spring配置项<context:annotation-config/>说明
    Hibernate 实体关联关系映射【转】
    freemarker中使用shiro标签
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3194801.html
Copyright © 2011-2022 走看看