杭电ACM1297——Children’s Queue~~大数相加的应用

题目的意思很明确,不能单独有一个女生站一起。

假设有N个人。

1.最后一个人是男生,则有F(N - 1)。

2.最后一个人是女生,则第N - 1也是女生,则有F(N - 1)。

但还有一种就是,第N - 2个是女生,(是男生的话,包含在F(N - 1)中),但是第N - 3 个是男生,则不包含在上面的情况中,但是也是符合的。也就是最后三个是女生,倒数第四个是男生。也就是还有F(N - 4)种。

所以有递推公式:F(N) = F(N - 1) + F(N - 2) + F(N - 4)。

下面的是AC的代码:

#include <iostream>
#include <cstring>
using namespace std;

char num[1001][300];

void add(int x, int y)         //大数相加函数
{
	int temp[300];
	int len1 = strlen(num[x]);
	int len2 = strlen(num[y]);
	int i, j, k = 0;
	for(i = len1 - 1, j = len2 - 1; i >= 0 && j >= 0; i--, j--)
	{
		temp[k++] = num[x][i] - '0' + num[y][j] - '0';
	}
	while(i >= 0)
	{
		temp[k++] = num[x][i--] - '0';
	}
	while(j >= 0)
	{
		temp[k++] = num[y][j--] - '0';
	}
	for(i = 0; i < k - 1; i++)
	{
		if(temp[i] >= 10)
		{
			temp[i + 1] += temp[i] / 10;
			temp[i] %= 10;
		}
	}
	if(temp[k - 1] >= 10)
	{
		temp[k] = temp[k - 1] / 10;
		temp[k - 1] %= 10;
		k++;
	}
	i = k - 1;
	j = 0;
	while(i >= 0)
	{
		num[x][j++] = temp[i--] + '0';
	}
	num[x][k] = '\0';
}

int main()
{
	int i;
	num[0][0] = '1'; num[0][1] = '\0';
	num[1][0] = '1'; num[1][1] = '\0';
	num[2][0] = '2'; num[2][1] = '\0';
	num[3][0] = '4'; num[3][1] = '\0';
	num[4][0] = '7'; num[4][1] = '\0';
	for(i = 5; i <= 1000; i++)
	{
		strcpy(num[i], num[i - 1]);
		add(i, i - 2);
		add(i, i - 4);
	}
	while(cin >> i)
	{
		cout << num[i] << endl;
	}
	return 0;
}
时间: 2024-10-23 02:34:43

杭电ACM1297——Children’s Queue~~大数相加的应用的相关文章

hdu 杭电1509 Windows Message Queue

Problem Description Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue.

HDU 1297 Children’s Queue (递推、大数相加)

Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17918    Accepted Submission(s): 5976 Problem Description There are many students in PHT School. One day, the headmaster whose na

大数相加(类似杭电acm1002)

/*输入两个非常大的整数(完全超出了int.long的表示范围),这个整数的长度可能超过100位,计算并输出这两个数相加的结果.*/ //自己用题目所给的案例测试,输出是正确的,也能输出正确的结果,不知道为什么提交以后一直wa,(如果有人测试了我代码以后知道我wa的原因请评论中提出哦,十分感谢),但是算法思想应该是对的,也参考了其他人的博客,(http://blog.csdn.net/hackbuteer1/article/details/6667026的大数相加的题目) 我的代码如下: //大

杭电acm 1002 大数模板(一)

从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的,但是经过自己的使用与调试也明白了其中的内涵. 首先定义大数的结构体: struct BigNum{ static const int BASE=100000000; static const int WIDTH=8; vector<int> s; BigNum(long long num=0){

杭电 1002 A + B Problem II(大数处理)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 209179    Accepted Submission(s): 40226 Problem Description I have a very simple problem for you. Given two integers A and B, yo

Big Number------HDOJ杭电1212(大数运算)

Problem Description As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B. To make the problem easier, I promise that B will be smaller than 100000. Is it t

大数A+B 【杭电-1002】 附题

/* A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 209912    Accepted Submission(s): 40404 Problem Description I have a very simple problem for you. Given two integers A and B,

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

HDOJ 1297 Children’s Queue

JAVA大数.... Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10390    Accepted Submission(s): 3333 Problem Description There are many students in PHT School. One day, the headmas