加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

裴波那契大数相加问题

发布时间:2020-12-14 04:10:14 所属栏目:大数据 来源:网络整理
导读:现在给你m个1,你可以把2个1组合成一个2 111 可以拆分为 111 、12、 21 有三种 输入 第一行输入一个n表示有n个测试数据 以下n行,每行输入m个1 (1 = n,m = 200) 输出 输出这种组合种数,占一行 样例输入 31111122222 样例输出 238 代码如下: #include cstdi
现在给你m个1,你可以把2个1组合成一个2

111 可以拆分为 111 、12、 21 有三种

输入

第一行输入一个n表示有n个测试数据
以下n行,每行输入m个1
(1 <= n,m <= 200)
输出
输出这种组合种数,占一行
样例输入
3
11
111
22222
样例输出
2
3
8


代码如下:

#include <cstdio>
#include<iostream>
#include<string>
using namespace std; 
int a[500],b[500];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	while(n--)
	{
		string str;
		cin>>str;
		int len = str.size();
		if(len == 1)
		{
			printf("1n");
			continue;
		}
		int tag = 0,cnt = 1,flag = 0,temp;
		memset(a,sizeof(a));
		memset(b,sizeof(b));
		a[0] = 1;
		b[0] = 1;
		for(i = 1; i < len; i++)
		{
			if(flag == 0)
			{
				for(j = 0; j < cnt; j++)
				{
					temp = a[j] + b[j] + tag;
					a[j] = temp % 10;
					tag = temp / 10;
				}
				if(tag)
				{
					a[cnt] = tag;
					tag = 0; 
					cnt++;
				}
				flag = 1;
			}
			else
			{
				for(j = 0;j < cnt; j++)
				{
					temp = a[j] + b[j] + tag;
					b[j] = temp % 10;
					tag = temp / 10;
				}
				if(tag)
				{
					b[cnt] = tag;
					tag = 0; 
					cnt++;
				}
				flag = 0;
			}
		}
		if(flag == 0)
		{
			for(i = cnt-1; i >= 0; --i)
				printf("%d",b[i]);
			printf("n");
		}
		else
		{
			for(i = cnt - 1; i >= 0; --i)
				printf("%d",a[i]);
			printf("n");
		}
	}
	return 0;
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读