求杨辉三角任意行任意列的值。行列都从1开始。(要求,数字长度无限制)

我们可以发现杨辉三角第row行col列数字满足公式,C(row,col)(注意这里是从0开始的,而我们给的行列是从1开始的,计算时行列要减1),而又能发现当 col == row || col == 1 时它的值为1。

C(row - 1,col - 1)  = ((row-1) * (row-2)*(row-3) .....(col个数相乘))/  (1 * 2 * 3 ....(col - 1))

但是假如我们以整形相乘,则最大计算行数不超过20行,因此我们改一个思路,用字符串相乘,相除的方法,理论上可以计算很大的数字(看电脑的性能)。

我们计算1000 599的值  结果为:(有兴趣的朋友可以在自己的电脑上跑一下,但要等几十秒)445017180952561060523085881552445271481571278068184641186677872653728026234701912177431641173778646797336006612785701953702286024536191622598804044936731315174233671427315519200466728310828200923259002715806617826262734061413109114812916824061554811089082195550918780030816924756984653766510

验证它的正确性:999 598 的值为 266386660870502016209014371539902174520500124409183599028662029876806165854205949431535657078998629414221153107553403171485452495167810400714799618490655982456648383897432112594473577106982246398507391015067424884990105073798838088746871131920730507538809962902351782240669190194871694647020

999 599的值为 178630520082059044314071510012543096961071153659001042158015842776921860380495962745895984094780017383114853505232298782216833529368381221884004426446075332717585287529883406605993151203845954524751611700739192941272628987614271026066045692140824303550272232648566997790147734562112959119490

可以发现它是正确的。

 再如果我们计算10000 599 3273707828078474625032846128323424952456099828962116718616551354149659500221466304735891860331961003141545245515636964007407484461436597524564932656957213047670487690237183465265235726072361519574009687951647574198401064673897119691940591441008011486723806693044446087709225378050534908541796496182392708480013522501001475408822055613507503984506072521242828264961235684727641854332934744798410041127986230381588424157808861218218487692073767865937462559922957444519440224569833956645968937251349241715867836892893037233460802207410806914614084939567283528605949896435102722395046658788381867343493199421238097939301001099021021773137921856844250045885927628086155862726769267603888794503192629927374480344307086051071621832809418579923044332935339717465877292944365474427845831467860111752233036242407425228735465884603855952867136410498180401826254110206672953523236247057970595608927520981577826292229569574434920780205067582237017686790976258460097180707646564123623800199288976

9999 598 的值为195787306849777760352999498423583170473922161988133393112581029081057743887632448268033136561507418729737379419777068154458413412135122044173400312917333073558050968973080879310792175636690887959321711510659590896153999067405788336411688537025981685074591099353993275372548932500672054736273057777484832450349843629922880517499308856573406078881351271897510881332815175464259408829992496988643784837937370313850372801917161617011166680654076726055665827666159471129375462975573627970225965044135098164425339180113014928053761348137980051499072186604784033413977201526971839983222112406785914258566750000390077264496649530674524554489096636702956448388817353894941614752536055808293379249215840853742368161405704316285711556757678998979295980707604075512010663184391494520237204442222256908474383005596523681046485508450155601541608918239615149544164412231582200840773604934560097627176583413039657978073135574108619124568719913409114569127013081564070218428160080542646967948712352  

9999 599的值为 3077920521228696864679846629899841781982177666973983325503970325068601756333833856467858723770453584411807866095859895852949071049301475480391532344039879974112436721264102585954443550435670631614687976440987983302247065606491331355528902903982029801649215593690452812336676445549862853805523438404907876029663678871078594891322746756934097905624721249345317383628420509263382445502942247809766256290048860067738051355891699601207321011419691139881796732256797973390064761594260328675742972207214143551442497712780022305407040859272826863115012752962499495191972694908130882411824546381595953084926449420848020674804351568346497218648825220141293597497110274191214247974233211795595415253976789073632112182901381734785910276051739580943748352227735641953866629759973979907608627025637854843758653236810901547688980376153700351325527492258565252282089697975090752682462642123410497981750937568538168314156434000326301655636347668827903117663963176896026962279486483580976832250576624

下来我们计算个100000 999 (跑了三分钟没跑完。。。。总共跑了十几分钟!!!)

167000212226765322958254627503162819293018766139480526325938047676138359859283552005248415327934428601240018273850026621609083745306771639052625162184452965050524338223023976089712509451520121922164444927397602758033260700948633502576938722373248351405146097057594527982538813597362097805803213188596397155592839345370290651606914129943099901556500101171366316015869275413301277421873373832810041063871708260075674108363636765456676862602450936536253908242371124574482482119173639560996765384849787539057470970212345640623572400115884776972385380794243324167364458628494724125732179664761396664473824089866953905038030977154884077951457139581416006041048879004559189623367950538220794744848666172210083071329965341353479091809685611182891944863252386914271765452795898254423440149876463596885680281584417953318146359422000331984403817594457215364654025734019084051865430861643262025912721679350522800447708205645844198714044316658802865819169718333264409699407505774477830564483291410119388002803613874459883377881079950168014770580778597254346986823206569083751174016144240412043456655017641240818229574975492404737316585004682725233442688197262628381280824786547755082157091084149785853851587927804649001830145520952031517213898746568690419341297166210930277742187447187577080630681499917879294218215295869594540883302376499517930407000521198784774040930476488880818618046945949200544093405841136787147617444246450470149686996110963363080823925511119003652851122646128569158138130113629518548640821881793239720356606985447416648681092827649810738202831144923649827436302930367509426503216719609566421967227208745865972934401412155717635419430755270024924876168017704941159883842006322984670687651350197977264215574502530273384119198491262205179326345423492033682276105362731530609876211202621621288579862052363545385624939503053323486815263767673286197982154656707861438947975389306199276923039777021200604828036460936760462186422453843952573130466766063054027180046170710431394202322212357579788417002150355282579675293461946131765337043156429969445746770887884972719873510884937117208494139771655811724305603986729788851833657913989218649385468583306710443496388785720008608007402546558795541183013755745802230477124683638524849070785542007871847616105886486886798601170207202124456742245230391238235030741038177219042856068855882584699083910035256987864600114194988215128172632962032965345012727947046712376874141644130016

99999 999

165333533441954356925470918483591058638867897344720563073572662306476802412313432455040534064149005149565126142555690412563334632037477365152140958223812518054850148585661823186808149563595071854880570928282183528315791584462001273898934093877678377158380271350702665674649997349497945428177520864050959657605042930739348841485775915544123774777698442145085817377044511797040368064109499923219500948653116425721775341774501779187556536310415555835815089849928336323346635589158986491647324171896857130113587971099635304026783179670523793288364134481453647895411441801214073972455839748307913390929639883608019115717858226275369539708119163948637146512163922342527068589716401876362732632673954736697071312160500592599283828520792059847773332067389169460692797443896906199923749220271400439557187907450464122605724134532719875866638289839586983652997661993556168963876933976675212590399757587349684574516980770479147006638957403509416519354829761144726545511965544447235269389838001708949384810503710808962118764123689198357819881221488833956115621581058546044064940437127330723634378866822683231654772009241579651410504897369459639404754643288604860772359552942459567604562387368092810421275823322639106949371356080758528307640008298173450936561443221952702611293655931149484680441985411587815558224557570639563687056748753245820204484279428786296777116032741356170460944662103580203832696239679180622450237248350911939072282345843273271816364658141844343249791637847272217474423074424538605044390343974633871594266187143534232298683755547857067699605180913594998515645370717800316010492554510125768111092885537785872630591092652974811764359234234367221048087135970567774475441356838248200535832840041609915570501765930909305046062308321417709926684142074132089586686033930497147590559453447241903710944058671047144038662932986747688262064599928633516404028353215219502098193867053837568721833796947618234993135735734049342718596366057190623393158824991319997317421691726382298007554316556621693783268548984363076937473712017381463723638562480922043271416474811463116473566710178298378391365196967306643231612107123973597997183811509543561790696014712306599472160581467565341375427162866747340647133066778993691603110689314942125407082639220835421562074041729098173781220956726399439227811668317222801993082714762353452159139528123293550613446196235967130227135030404484327702319211550857764598862039415249868218911358082685984

99999 998

1666678784810966032783709019571760654150868794759963252365385369661557446970119550207881263785423451674892131294336209045749113269294273900484203960640446995674189637362152902904359887925050067283873999115419229717469116486632228678004628495569974246765825706891862307888816247864152377625692324545437497987796414630941810121138214398976126778801659026280498638824763616260909357763873909590540115218591834353898766589134986269120326292035380700438818392442788251135846530014653069349441212952930408943882999112710336596789220445360983684021246312789676271953016827280650153276339916453483273544184206258934789320172750879514538243337975632778859528884956662032121033651548661858062112174711435513011759169464748754195263288893551335118612795863217453578968008898992054499690929605063157328492374133953830712422224889280456117765527754870231711656363740462915087988496884968049435512964092000838225930727435166697192075086913149386346464339957188537864187441961327242561174645289701170003192299903065497764613757390751810194889359289763298231365242148023039686233579016909688409077788194958009163457565733912753326811687635223085828688044908657767608921271844088187477594703716056975432575764605165542052458789440193503209573890448395239482779853944258227666448531516038092400188696088330063735993657725230030853826553623253697725922721092412487996924897735132710357673384842368996711397166161956164697380195895538531077404650267690091264459267369274660403059484798856351683715055689090913504250477907159368126090419841913184349997337279792743038597650231328651311790932212567193416010662209483798310874341670959993342343308759180905871060196520902803876789032047137166684442485168074784134854811308588061693713808571620968338056890169844495252642203349359944095590071432234383019316757755379717577635803381316401346962006516305635224750663839039769793953801441488359340754108335468630555089242829402965611692300726887417743590056396653329179971641774743056709758354444328133386648005655735886005148453165992205642201581444564668041698480675507926174330296076421856246306800706638738817128942804349168492693496862756190854649846404445656858689453871000110971335807318154667232580239679811454894049946976752110627366435368696399441988146321172450285542064157388713017380213480802685228930576913168436241948026275823766883716540732589034085637713799289857637465083790503887425853421411175200746150688531796844157962783561444032

可以看出都是正确的。至此问题已解决

源代码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
using namespace std;
#define n 10


//字符串乘法
string multiply(string num1, string num2) {
	string res;
	int n1 = num1.size();
	int	n2 = num2.size();
	int k = n1 + n2 - 2;
	int carry = 0;
	vector<int> v(n1 + n2, 0);
	for (int i = 0; i < n1; ++i)
	{
		for (int j = 0; j < n2; ++j)
		{
			v[k - i - j] += (num1[i] - '0') * (num2[j] - '0');
		}
	}
	for (int i = 0; i < n1 + n2; ++i)
	{
		v[i] += carry;
		carry = v[i] / 10;
		v[i] %= 10;
	}
	int i = n1 + n2 - 1;
	while (v[i] == 0)
	{
		--i;
	}
	if (i < 0)
	{
		return "0";
	}
	while (i >= 0)
	{
		res.push_back(v[i--] + '0');
	}
	return res;
}


string dezero(string a)//用来去掉正数前面的0,也就是说可以输入000001类似这样的数字
{
	unsigned int i;
	for (i = 0; i<a.length(); i++)
	{
		if (a.at(i)>48)
		{
			break;
		}
	}
	if (i == a.length())
	{
		return "0";
	}
	a.erase(0, i);
	return a;
}

int judge(string a, string b)//判断两个正数的大小
{
	if (a.length() > b.length())
	{
		return 1;
	}
	if (a.length() < b.length())
	{
		return -1;
	}

	unsigned int i;
	for (i = 0; i<a.length(); i++)
	{
		if (a.at(i)>b.at(i))
		{
			return 1;
		}
		if (a.at(i) < b.at(i))
		{
			return -1;
		}
	}

	return 0;
}

string minu(string a, string b)//自然数减法
{
	a = dezero(a);
	b = dezero(b);
	unsigned int i, j = 0;
	string c = "0";
	string c1, c2;
	string d = "-";
	if (judge(a, b) == 0)
	{
		return c;
	}
	if (judge(a, b) == 1)
	{
		c1 = a;
		c2 = b;
	}
	if (judge(a, b) == -1)
	{
		c1 = b;
		c2 = a;
		j = -1;
	}
	reverse(c1.begin(), c1.end());
	reverse(c2.begin(), c2.end());
	for (i = 0; i < c2.length(); i++)
	{
		if (c2.at(i) >= 48 && c2.at(i) <= 57)
		{
			c2.at(i) -= 48;
		}
		if (c2.at(i) >= 97 && c2.at(i) <= 122)
		{
			c2.at(i) -= 87;
		}
	}

	for (i = 0; i < c1.length(); i++)
	{
		if (c1.at(i) >= 48 && c1.at(i) <= 57)
		{
			c1.at(i) -= 48;
		}
		if (c1.at(i) >= 97 && c1.at(i) <= 122)
		{
			c1.at(i) -= 87;
		}
	}
	for (i = 0; i < c2.length(); i++)
	{
		c1.at(i) = c1.at(i) - c2.at(i);
	}
	for (i = 0; i < c1.length() - 1; i++)
	{
		if (c1.at(i)<0)
		{
			c1.at(i) += n;
			c1.at(i + 1)--;
		}
	}
	for (i = c1.length() - 1; i >= 0; i--)
	{
		if (c1.at(i)>0)
		{
			break;
		}
	}
	c1.erase(i + 1, c1.length());
	for (i = 0; i < c1.length(); i++)
	{
		if (c1.at(i) >= 10) c1.at(i) += 87;
		if (c1.at(i) < 10) c1.at(i) += 48;
	}
	reverse(c1.begin(), c1.end());
	if (j == -1)
	{
		c1.insert(0, d);
	}
	return c1;
}


string divide(string a, string b)//自然数除法
{
	if (b.length() == 1 && b.at(0) == 48)
	{
		return "error";
	}
	unsigned int i, j;
	string c1, c2, d, e;
	if (judge(a, b) == 0)
	{
		return "1";
	}
	if (judge(a, b) == -1)
	{
		return "0";
	}
	c1 = dezero(a);
	c2 = dezero(b);
	d = "";	e = "";
	for (i = 0; i < c1.length(); i++)
	{
		j = 0;
		d = d + c1.at(i);
		d = dezero(d);
		while (judge(d, b) >= 0)
		{
			d = minu(d, b);
			//调用之前的减法函数minu,在本文中也加了进来		
			d = dezero(d);
			j++;
		}
		e = e + "0";
		e.at(i) = j;
	}
	for (i = 0; i < e.length(); i++)
	{
		if (e.at(i) >= 10)
		{
			e.at(i) += 87;
		}
		if (e.at(i) < 10)
		{
			e.at(i) += 48;
		}
	}
	e = dezero(e);
	return e;
}


string  Sun(int row, int col)
{
	if (row == col || col == 1)
	{

		return "1";
	}
	else
	{
		row = row - 1;
		col = col - 1;
		char a[20] = { 0 };

		string res = "1";
		for (int i = 0; i < col; i++)
		{
			res = multiply(res, _itoa(row, a, 10));
			row = row - 1;
		}

		string res1 = "1";
		int temp = col;
		for (int i = 0; i < col; i++)
		{
			res1 = multiply(res1, _itoa(temp, a, 10));
			temp -= 1;
		}

		return divide(res, res1);

	}
}





int main()
{
	int row = 0;
	int col = 0;

	while (1)
	{
		std::cin >> row >> col;

		std::cout << Sun(row, col) << endl;
	}
	system("pause");
	return 0;
}

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值