光棍的yy
http://acm.nyist.net/JudgeOnline/problem.php?pid=655
时间限制:1000?ms ?|? 内存限制:65535?KB
难度:2
输入
第一行输入一个n表示有n个测试数据
以下n行,每行输入m个1
(1 <= n,m <= 200)
输出
输出这种组合种数,占一行
样例输入
3 11 111 22222
样例输出
2 3 8
描述
yy经常遇见一个奇怪的事情,每当他看时间的时候总会看见11:11,这个很纠结啊
![]()
。
现在给你m个1,你可以把2个1组合成一个2,这样就不是光棍了
![]()
,问这样的组合有多少种??
例如(111? 可以拆分为 111 12 21? 有三种)
?
我的代码
#include <stdio.h>
#include <string.h>
#define MAX 210
char res[MAX][MAX] = { 0 };
char s[MAX];
int main(int argc,char *argv[]) {
int i,j,t,d,T,n;
//一次处理完毕
res[0][0] = res[1][0] = 1;
for (i = 2; i < MAX; i++) {
d = 0;
for (j = 0; j < MAX; j++) {
t = res[i - 1][j] + res[i - 2][j] + d;
if (t > 9) {
d = t / 10;
res[i][j] = t % 10;
} else {
d = 0;
res[i][j] = t;
}
}
}
//输出
scanf("%d",&T);
while (T--) {
scanf("%s",s);
n = strlen(s);
for (i = MAX - 1; i > 0 && res[n][i] == 0; i--);
while (i >= 0)
printf("%d",res[n][i--]);
printf("\n");
}
return 0;
}
?
大数模版
string sum(string s1,string s2)
{
if(s1.length()<s2.length())
{
string temp=s1;
s1=s2;
s2=temp;
}
int i,j;
for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}
使用模版
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
string sum(string s1,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}
int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
string s;cin>>s;
if(s.size()==1){
printf("1\n");continue;
}
else if(s.size()==2)
{
printf("2\n");continue;
}
string ans,a="1",b="2";
for(int i=3;i<=s.size();i++)
{
ans=sum(a,b);
a=b;
b=ans;
}
cout<<b<<endl;
}
return 0;
}