1142 Relations dp n个数的不同大小关系总数

本文探讨了在给定数量的对象中,不同关系组合的数量如何计算。通过动态规划的方法,详细解释了如何从两个对象的关系扩展到多个对象,进而得出不同关系组合的总数。对于每个输入的数字n,本文提供了其对应的不同关系组合的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1142. Relations

Time limit: 1.0 second
Memory limit: 64 MB

Background

Consider a specific set of comparable objects. Between two objects a and b, there exits one of the following three classified relations:
a = b
a
 < b
b
 < a
Because relation '=' is symmetric, it is not repeated above.
So, with 3 objects (abc), there can exist 13 classified relations:
a = b = c       a = b < c       c < a = b       a < b = c
b = c < a       a = c < b       b < a = c       a < b < c
a < c < b       b < a < c       b < c < a       c < a < b
c < b < a

Problem

Given N, determine the number of different classified relations between N objects.

Input

Includes many integers N (in the range from 2 to 10), each number on one line. Ends with −1.

Output

For each N of input, print the number of classified relations found, each number on one line.

Sample

input output
2
3
-1
3
13

题意: 输入一个n。代表有n个数。输出他们所有的不同关系 有多少种。

做法:开一个dp[ i ][ j ] i表示当前状态有几个字母,j表示当前状态有多少个不同的数 (也就是小于号+1)。

如:a < b < c   a < c < b       b < a < c       b < c < a       c < a < b  

上面这些是有 有两个等于的状态, 如果想再加一个不同于a,b,c的一个数d。  拿a < b < c 来说,可以有4种方法 d<a<b<c或者 a <d< b < c 或者 a<b<d<c 或者 a<b<c<d  

而加和abc 某一个数相同的d 拿 拿a < b < c 来说 ,方法有3种  d=a<b<c或者 a <d=b < c 或者 a<b<d=c 

再试试2推到3 或3推到4 的别的状态,

可以发现 从上一个状态 递归到 i状态,要乘上 i 状态  有多少不同数字,也就是乘上j 。

可以 有dp[ i ][ j ]= dp[ i - 1 ] [ j- 1  ]*j +  dp[ i - 1 ] [ j ] *j

上面这表示加了一个不同的数 上面的表示加了一个和原来数中某数相等的数

#include<stdio.h>
#include<string.h>

int dp[11][11]={0};//i 几个字母  j 有几个不相等的字母(i-等于号数)
int main()
{
	int n;
	dp[1][1]=1;
	int ans[11];
	for(int i=2;i<=10;i++)
	{
		ans[i]=0;
		for(int j=1;j<=10;j++)
		{
			dp[i][j]=dp[i-1][j-1]*j+dp[i-1][j]*j;
					//加一个小于号  加一个等于号
			ans[i]+=dp[i][j];
		}
	}



	while(scanf("%d",&n),n!=-1)
	{
		printf("%d\n",ans[n]);
	}
	return 0;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值