跪求代码,哪位高手来顶一下?

lingyunzhuangzhi 2009-05-11 10:33:03
一个C++问题:
输入一个整数,输出如下:
如果 n=6
6
5 1
4 2
4 1 1
3 3
3 2 1
3 1 1 1
2 2 2
2 2 1 1
2 1 1 1 1
1 1 1 1 1 1

如果输入n=5
5
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1
...全文
171 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
draculamx 2009-05-12
  • 打赏
  • 举报
回复
原来规律这么简单。。。脑子不好使了啊。。。哎。。。。。。。
夹心饼干 2009-05-11
  • 打赏
  • 举报
回复
递归吧,一直递归到+右边的是1
lgccaa 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yshuise 的回复:]
还没什么规律?
6=5+1=4+2=4+1+1
楼主自己写吧,
[/Quote]

规律找到,实现应该不难吧,楼主自己可以试着写啊
不要一味的要代码,这样学习到的东西会少很多的
jssz103b 2009-05-11
  • 打赏
  • 举报
回复
只是有点麻烦 这个题目好象是在大学时候看到的 掌握方法后一点也不难
yshuise 2009-05-11
  • 打赏
  • 举报
回复
还没什么规律?
6=5+1=4+2=4+1+1
楼主自己写吧,
draculamx 2009-05-11
  • 打赏
  • 举报
回复
这个题目的难点就在找“规律”吧。。
不过,我看了一下这个输出结果,暂时还没有找到什么“规律”。。。。
Paradin 2009-05-11
  • 打赏
  • 举报
回复
mark.up
sherrik 2009-05-11
  • 打赏
  • 举报
回复
lz最好把题目的要求发出来
liliangbao 2009-05-11
  • 打赏
  • 举报
回复
帮顶~
adfas 2009-05-11
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int n;
int a[100];
void fuckXiaoDong(int sum, int step, int begin)
{
if (sum == 0)
{
for (int i = 0; i < step; ++i)
{
cout << a[i] << ' ';
}
cout << endl;
}
else if (sum > 0)
{
for (int i = n; i >= 1; --i)
{
if (i <= begin)
{
a[step] = i;
fuckXiaoDong(sum - i, step + 1, i);
}
}
}

}
int main()
{
cin >> n;
fuckXiaoDong(n, 0, n);
return 0;
}

贴个我的
ZPZ07117007 2009-05-11
  • 打赏
  • 举报
回复
//编译过 很正确
#include <iostream.h>
//using namespace std;

void F(int n,int count,int depth)
{
if (1 == count)
cout << n << " ";


else
{
int j = 0;
int m = n - count + 1;
while (m-j > m/2)
{
for (int c = 1;c <= j+1;c++)
{
if (m + j + 1 <= n)
{
if ((c > 1 && c != j+1) || c==1)
{
F(m,1,2);
F(j + 1,c,2);

for (int i = 1;i <= count - 1 - c;i++)
{
if (m+j+1+i <= n)
F(1,1,2);
}

if (depth == 1)
cout << endl;



}

}
}

j++;
}
}

}


void main()
{
int n;
cin >> n;
cout << n << endl;
for (int i = 2;i <= n;i++)
{
F(n,i,1);
}

}
lylm 2009-05-11
  • 打赏
  • 举报
回复
其实不难,为啥不自己试着写一下呢
yinzhen 2009-05-11
  • 打赏
  • 举报
回复
mark
hjjdebug 2009-05-11
  • 打赏
  • 举报
回复
需求不明白,没法写! 7 又该输出啥样呢,8又该输出啥样呢? 。。。。
coverallwangp 2009-05-11
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

void F(int n,int count,int depth)
{
if (1 == count)
cout << n << " ";


else
{
int j = 0;
int m = n - count + 1;
while (m-j > m/2)
{
for (int c = 1;c <= j+1;c++)
{
if (m + j + 1 <= n)
{
if ((c > 1 && c != j+1) || c==1)
{
F(m,1,2);
F(j + 1,c,2);

for (int i = 1;i <= count - 1 - c;i++)
{
if (m+j+1+i <= n)
F(1,1,2);
}

if (depth == 1)
cout << endl;



}

}
}

j++;
}
}

}


void main()
{
int n;
cin >> n;
cout << n << endl;
for (int i = 2;i <= n;i++)
{
F(n,i,1);
}

}


xempo 2009-05-11
  • 打赏
  • 举报
回复
此处有千金!不敢拾之。
nuoshueihe 2009-05-11
  • 打赏
  • 举报
回复
#include <iostream.h>
int fun(int i,int j,int sum)
{
if (j!=1)
{
for (int i=1;i<sum;i++)
{
for (int j=sum;j>0;j--)
{
if (sum==i+j)
{
cout<<j<<"\t"<<i<<endl;
fun(i,j,j);

}
}
}

}
else
return 1;
return 0;

}
void f(int n)
{
fun(1,n,n);

}
void main()
{
int n;
cin>>n;
f(n);

}

输出的格式自己整理下吧
关键在于输出格式域的个数不一样

用循环可以解决这个问题,但是很麻烦
继续学习
yuxiabo1984 2009-05-11
  • 打赏
  • 举报
回复
恩 每次都是从上一列的最右边开始分解,直到==1时

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧