HDU 5686:2016"百度之星" - 资格赛 Problem B
原文链接:https://www.dreamwings.cn/hdu5686/2645.html
Problem B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 403 Accepted Submission(s): 136
1 3 5
1 3 8Hint如果序列是:(111)。可以构造出如下三个新序列:(111), (21), (12)。
AC代码:
#include "stdio.h"
#include "stdlib.h"
#define N 22 //22位能表示第100个以内的斐波那契数列值
//大数加法函数
char * Add(char * x1, char * x2)
{
char * y = (char *) malloc(sizeof(char *) * N);
int i = 0;
int t = 0; //表示进位
//实现大数加法,数组前面存的是数值的高位。如123在数组中是{'3','2','1','\0'}
//处理相同长度的部分
while(x1[i] != '\0' && x2[i] != '\0')
{
y[i] = (x1[i] - '0' + x2[i] - '0' + t) % 10 + '0';
t = (x1[i] - '0' + x2[i] - '0' + t) / 10;
i++;
}
//如果x1比x2长
while(x1[i] != '\0')
{
y[i] = (x1[i] - '0' + t) % 10 + '0';
t = (x1[i] - '0' + t) / 10;
i++;
}
//如果x2比x1长
while(x2[i] != '\0')
{
y[i] = (x2[i] - '0' + t) % 10 + '0';
t = (x2[i] - '0' + t) / 10;
i++;
}
//如果还有进位
if (t == 1)y[i++] = '1';
y[i] = '\0';
return y;
}
//输出
void Output(char * y)
{
//先找到\0的位置,然后逆序输出
int i = 0;
while(y[i] != '\0')i++;
i--;
while(i >= 0)
printf("%d", y[i--] - '0');
}
int main()
{
int b;
while(~scanf("%d", &b))
{
if(b)
{
getchar();
int count = b;
int i;
char * x1 = (char *)malloc(sizeof(char) * N);
char * x2 = (char *)malloc(sizeof(char) * N);
char * y = (char *)malloc(sizeof(char) * N);
//初始化y, x1, x2
for (i = 0; i < N; i++)
{
x1[i] = '\0';
x2[i] = '\0';
y[i] = '\0';
}
//给x1和x2赋初值
x1[0] = '0';
x1[1] = '\0';
x2[0] = '1';
x2[1] = '\0';
//斐波那契数列,叠加
for(i = 1; i <= count; i++)
{
y = Add(x1, x2);
x1 = x2;
x2 = y;
}
//输出结果
Output(y);
printf("\n");
}
else printf("\n");
}
return 0;
}
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static BigInteger[]dp=new BigInteger[205];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
//int n=cin.nextInt();
Init();
while(cin.hasNext())
{
int n=cin.nextInt();
if(n>=1&&n<=200)
{
System.out.print(dp[n]);
}
System.out.println();
}
}
public static void Init()
{
dp[1]=new BigInteger("1");
dp[2]=new BigInteger("2");
for(int i=3;i<=201;i++)
{
dp[i]=dp[i-1].add(dp[i-2]);
}
}
}
HDU 5686:2016"百度之星" - 资格赛 Problem B的更多相关文章
- HDU 5688:2016"百度之星" - 资格赛 Problem D
原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5685:2016"百度之星" - 资格赛 Problem A
原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others) ...
- [HDU5686]2016"百度之星" - 资格赛 Problem B
题目大意:给你n,规定一个串中相邻的两个1可以合并为一个2(别的不行),让你求长度为n的全1串最多能变成多少种不同的串. 解题思路:我们先来找一波规律,发现n=1,2,3,4,5时答案分别为1,2,3 ...
- [HDU5687]2016"百度之星" - 资格赛 Problem C
题目大意:有n个操作,每个操作是以下三个之一,要你实现这些操作. 1.insert : 往字典中插入一个单词2.delete: 在字典中删除所有前缀等于给定字符串的单词3.search: 查询是否在字 ...
- [HDU5688]2016"百度之星" - 资格赛 Problem D
题目大意:给你n个字符串,如果一个字符串可以通过重新排列变成另一个字符串,则认为这两个字符串相等.每输入一个字符串,输出这个字符串和与它相等的之前出现了几次. 解题思路:我们可以用map保存一个字符串 ...
- [HDU5685]2016"百度之星" - 资格赛 Problem A
题目大意:给你一个字符串,和一些问题,每个问题问你[l,r]子串的哈希值是多少. 哈希值计算方法为:$H(s)=\prod _{i=1} ^{i\leq len(s)}(s_i-28)(mod\ 99 ...
- 2016百度之星资格赛 Problem A(前缀积与求逆元)
题意:给出一个字符串,每次询问给出x和y要求算出从x到y的每个字符的(ASCII 码值-28)的值的积(mod9973). 分析:首先的想法肯定是算出每个位置的前缀积,然后只要F[y]/F[x-1]即 ...
- 2016百度之星资格赛 Problem B(大数+组合数)
题意:度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列.最多200个1. 比如 ...
- 2016百度之星 资格赛ABCDE
看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...
随机推荐
- 细说jQuery原型的创建和实现原理,并用实例简单模仿
在解析jQuery实现机理之前,我们先总结一下几点知识,这些都是我学习路上遇到的坑,我跌倒过很多次,现在把它补上: 1)自定义构造函数,如下例: function person(){ this.nam ...
- log4net 添加自定义日志到数据库
添加操作日志到数据库举例: (一)建立数据库的操作日志表,如下我建立了一个简单的日志表 (二)配置文件中的配置如下 <log4net> <!--错误日志记录数据库--> < ...
- Runtime Error---Description: An application error occurred on the server....
[原]Runtime Error---Description: An application error occurred on the server.... 2010-1-7阅读2010 评论3 D ...
- bzoj3594: [Scoi2014]方伯伯的玉米田--树状数组优化DP
题目大意:对于一个序列,可以k次选任意一个区间权值+1,求最长不下降子序列最长能为多少 其实我根本没想到可以用DP做 f[i][j]表示前i棵,操作j次,最长子序列长度 p[x][y]表示操作x次后, ...
- lua函数
一.函数 在lua中函数的调用方式和C语言基本相同. 如print(“hello world”), z=add(x+y).唯一的差别是,如果函数只有一个参数,并且该参数是字符串或者table构造器 ...
- Excel报表开发
读取Excel数据 /// <summary> /// 封装方法 /// </summary> /// <param name="path">& ...
- wa~哭笑天使
#include <stdio.h> #define M 301 int r[M], c[M]; int main() { int k, m, n, i, j, sum, flag, t; ...
- JS开发windows phone8.1系列之2
http://msdn.microsoft.com/zh-cn/library/windows/apps/dn629636.aspx Windows.Storage.ApplicationData.r ...
- Algorithm | Tree traversal
There are three types of depth-first traversal: pre-order,in-order, and post-order. For a binary tre ...
- PHP 上传大文件