巴斯卡三角形

C语言练习实例——巴斯卡三角形 目录 题目 解法 输出结果 题目 解法 巴斯卡三角形在中国又称杨辉三角,他的具体含义如下: 前提:每行端点与结尾的数为1. 每个数等于它上方两数之和。 每行数字左右对称,由1开始逐渐变大。 第n行的数字有n项。 前n行共[(1+n)n]/2 个数。 第n行的m个数可表示为C(n-1,m-1),即为从n-1个不同元素中取m-1个元素的组合数。 第n行的第m个数和第n-m+1个数相等 ,为组合数性质之一。 每个数字等于上一行的左右两个数字之和。可用此性质写出整个杨辉三角。即第n+ 阅读详情

巴斯卡三角形

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;

博客内容:巴斯卡三角形;

博客时间:2014-4-7

编程语言:C++

编程坏境:Windows 7 专业版 x64

编程工具:vs2008 32位编译器

 

  • 引言

有些问题已经老掉牙,但是那不是对自己。各种技术横空出世,但是自己还没掌握。

  • 题目

打印 巴斯卡 三角形

  • 思路

若i代表巴斯卡三角形的行,j代表巴斯卡三角形的列。

思路如下

		// second action: get data for gossip
		data[0][0] = "1";
		for(int i=0;i < n;i++)
		{
			data[i][0] = "1" ;
			for(int j = 1; j<i; j++)
			{
				data[i][j] = String::ADD_Int(data[i-1][j-1],data[i-1][j]);
			}
			data[i][i] = "1" ;
		}

 

  • 实验

本程序已经处理掉数据溢出情况

输出的数据保存到如下文本 gossip.txt

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 
1 11 55 165 330 462 462 330 165 55 11 1 
1 12 66 220 495 792 924 792 495 220 66 12 1 
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1 
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1 
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1 
1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1 
1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1 
1 22 231 1540 7315 26334 74613 170544 319770 497420 646646 705432 646646 497420 319770 170544 74613 26334 7315 1540 231 22 1 
1 23 253 1771 8855 33649 100947 245157 490314 817190 1144066 1352078 1352078 1144066 817190 490314 245157 100947 33649 8855 1771 253 23 1 
1 24 276 2024 10626 42504 134596 346104 735471 1307504 1961256 2496144 2704156 2496144 1961256 1307504 735471 346104 134596 42504 10626 2024 276 24 1 
1 25 300 2300 12650 53130 177100 480700 1081575 2042975 3268760 4457400 5200300 5200300 4457400 3268760 2042975 1081575 480700 177100 53130 12650 2300 300 25 1 
1 26 325 2600 14950 65780 230230 657800 1562275 3124550 5311735 7726160 9657700 10400600 9657700 7726160 5311735 3124550 1562275 657800 230230 65780 14950 2600 325 26 1 
1 27 351 2925 17550 80730 296010 888030 2220075 4686825 8436285 13037895 17383860 20058300 20058300 17383860 13037895 8436285 4686825 2220075 888030 296010 80730 17550 2925 351 27 1 
1 28 378 3276 20475 98280 376740 1184040 3108105 6906900 13123110 21474180 30421755 37442160 40116600 37442160 30421755 21474180 13123110 6906900 3108105 1184040 376740 98280 20475 3276 378 28 1 
1 29 406 3654 23751 118755 475020 1560780 4292145 10015005 20030010 34597290 51895935 67863915 77558760 77558760 67863915 51895935 34597290 20030010 10015005 4292145 1560780 475020 118755 23751 3654 406 29 1 
1 30 435 4060 27405 142506 593775 2035800 5852925 14307150 30045015 54627300 86493225 119759850 145422675 155117520 145422675 119759850 86493225 54627300 30045015 14307150 5852925 2035800 593775 142506 27405 4060 435 30 1 
1 31 465 4495 31465 169911 736281 2629575 7888725 20160075 44352165 84672315 141120525 206253075 265182525 300540195 300540195 265182525 206253075 141120525 84672315 44352165 20160075 7888725 2629575 736281 169911 31465 4495 465 31 1 
1 32 496 4960 35960 201376 906192 3365856 10518300 28048800 64512240 129024480 225792840 347373600 471435600 565722720 601080390 565722720 471435600 347373600 225792840 129024480 64512240 28048800 10518300 3365856 906192 201376 35960 4960 496 32 1 
1 33 528 5456 40920 237336 1107568 4272048 13884156 38567100 92561040 193536720 354817320 573166440 818809200 1037158320 1166803110 1166803110 1037158320 818809200 573166440 354817320 193536720 92561040 38567100 13884156 4272048 1107568 237336 40920 5456 528 33 1 
1 34 561 5984 46376 278256 1344904 5379616 18156204 52451256 131128140 286097760 548354040 927983760 1391975640 1855967520 2203961430 2333606220 2203961430 1855967520 1391975640 927983760 548354040 286097760 131128140 52451256 18156204 5379616 1344904 278256 46376 5984 561 34 1 
1 35 595 6545 52360 324632 1623160 6724520 23535820 70607460 183579396 417225900 834451800 1476337800 2319959400 3247943160 4059928950 4537567650 4537567650 4059928950 3247943160 2319959400 1476337800 834451800 417225900 183579396 70607460 23535820 6724520 1623160 324632 52360 6545 595 35 1 
1 36 630 7140 58905 376992 1947792 8347680 30260340 94143280 254186856 600805296 1251677700 2310789600 3796297200 5567902560 7307872110 8597496600 9075135300 8597496600 7307872110 5567902560 3796297200 2310789600 1251677700 600805296 254186856 94143280 30260340 8347680 1947792 376992 58905 7140 630 36 1 
1 37 666 7770 66045 435897 2324784 10295472 38608020 124403620 348330136 854992152 1852482996 3562467300 6107086800 9364199760 12875774670 15905368710 17672631900 17672631900 15905368710 12875774670 9364199760 6107086800 3562467300 1852482996 854992152 348330136 124403620 38608020 10295472 2324784 435897 66045 7770 666 37 1 
1 38 703 8436 73815 501942 2760681 12620256 48903492 163011640 472733756 1203322288 2707475148 5414950296 9669554100 15471286560 22239974430 28781143380 33578000610 35345263800 33578000610 28781143380 22239974430 15471286560 9669554100 5414950296 2707475148 1203322288 472733756 163011640 48903492 12620256 2760681 501942 73815 8436 703 38 1 
1 39 741 9139 82251 575757 3262623 15380937 61523748 211915132 635745396 1676056044 3910797436 8122425444 15084504396 25140840660 37711260990 51021117810 62359143990 68923264410 68923264410 62359143990 51021117810 37711260990 25140840660 15084504396 8122425444 3910797436 1676056044 635745396 211915132 61523748 15380937 3262623 575757 82251 9139 741 39 1 
1 40 780 9880 91390 658008 3838380 18643560 76904685 273438880 847660528 2311801440 5586853480 12033222880 23206929840 40225345056 62852101650 88732378800 113380261800 131282408400 137846528820 131282408400 113380261800 88732378800 62852101650 40225345056 23206929840 12033222880 5586853480 2311801440 847660528 273438880 76904685 18643560 3838380 658008 91390 9880 780 40 1 
1 41 820 10660 101270 749398 4496388 22481940 95548245 350343565 1121099408 3159461968 7898654920 17620076360 35240152720 63432274896 103077446706 151584480450 202112640600 244662670200 269128937220 269128937220 244662670200 202112640600 151584480450 103077446706 63432274896 35240152720 17620076360 7898654920 3159461968 1121099408 350343565 95548245 22481940 4496388 749398 101270 10660 820 41 1 
1 42 861 11480 111930 850668 5245786 26978328 118030185 445891810 1471442973 4280561376 11058116888 25518731280 52860229080 98672427616 166509721602 254661927156 353697121050 446775310800 513791607420 538257874440 513791607420 446775310800 353697121050 254661927156 166509721602 98672427616 52860229080 25518731280 11058116888 4280561376 1471442973 445891810 118030185 26978328 5245786 850668 111930 11480 861 42 1 
1 43 903 12341 123410 962598 6096454 32224114 145008513 563921995 1917334783 5752004349 15338678264 36576848168 78378960360 151532656696 265182149218 421171648758 608359048206 800472431850 960566918220 1052049481860 1052049481860 960566918220 800472431850 608359048206 421171648758 265182149218 151532656696 78378960360 36576848168 15338678264 5752004349 1917334783 563921995 145008513 32224114 6096454 962598 123410 12341 903 43 1 
1 44 946 13244 135751 1086008 7059052 38320568 177232627 708930508 2481256778 7669339132 21090682613 51915526432 114955808528 229911617056 416714805914 686353797976 1029530696964 1408831480056 1761039350070 2012616400080 2104098963720 2012616400080 1761039350070 1408831480056 1029530696964 686353797976 416714805914 229911617056 114955808528 51915526432 21090682613 7669339132 2481256778 708930508 177232627 38320568 7059052 1086008 135751 13244 946 44 1 
1 45 990 14190 148995 1221759 8145060 45379620 215553195 886163135 3190187286 10150595910 28760021745 73006209045 166871334960 344867425584 646626422970 1103068603890 1715884494940 2438362177020 3169870830126 3773655750150 4116715363800 4116715363800 3773655750150 3169870830126 2438362177020 1715884494940 1103068603890 646626422970 344867425584 166871334960 73006209045 28760021745 10150595910 3190187286 886163135 215553195 45379620 8145060 1221759 148995 14190 990 45 1 
1 46 1035 15180 163185 1370754 9366819 53524680 260932815 1101716330 4076350421 13340783196 38910617655 101766230790 239877544005 511738760544 991493848554 1749695026860 2818953098830 4154246671960 5608233007146 6943526580276 7890371113950 8233430727600 7890371113950 6943526580276 5608233007146 4154246671960 2818953098830 1749695026860 991493848554 511738760544 239877544005 101766230790 38910617655 13340783196 4076350421 1101716330 260932815 53524680 9366819 1370754 163185 15180 1035 46 1 
1 47 1081 16215 178365 1533939 10737573 62891499 314457495 1362649145 5178066751 17417133617 52251400851 140676848445 341643774795 751616304549 1503232609098 2741188875414 4568648125690 6973199770790 9762479679106 12551759587422 14833897694226 16123801841550 16123801841550 14833897694226 12551759587422 9762479679106 6973199770790 4568648125690 2741188875414 1503232609098 751616304549 341643774795 140676848445 52251400851 17417133617 5178066751 1362649145 314457495 62891499 10737573 1533939 178365 16215 1081 47 1 
1 48 1128 17296 194580 1712304 12271512 73629072 377348994 1677106640 6540715896 22595200368 69668534468 192928249296 482320623240 1093260079344 2254848913647 4244421484512 7309837001104 11541847896480 16735679449896 22314239266528 27385657281648 30957699535776 32247603683100 30957699535776 27385657281648 22314239266528 16735679449896 11541847896480 7309837001104 4244421484512 2254848913647 1093260079344 482320623240 192928249296 69668534468 22595200368 6540715896 1677106640 377348994 73629072 12271512 1712304 194580 17296 1128 48 1 
1 49 1176 18424 211876 1906884 13983816 85900584 450978066 2054455634 8217822536 29135916264 92263734836 262596783764 675248872536 1575580702584 3348108992991 6499270398159 11554258485616 18851684897584 28277527346376 39049918716424 49699896548176 58343356817424 63205303218876 63205303218876 58343356817424 49699896548176 39049918716424 28277527346376 18851684897584 11554258485616 6499270398159 3348108992991 1575580702584 675248872536 262596783764 92263734836 29135916264 8217822536 2054455634 450978066 85900584 13983816 1906884 211876 18424 1176 49 1 

 

  • 代码

头文件 mine_string.h

#include <iostream>
#include <string>
#include <limits>
using namespace std;

// extra the class of string
class String:public string
{
public:

	// function 1: mode the add of int( (-3) + (-3) ) = - 6
	// input: 两个字符串 a 和 b,里面放的都是整数;
	// output: 返回一个字符串,字符串里面是整数;
	// 功能: 实现参数两个整数的相加操作,结果存在返回的字符串里
	static string ADD_Int(string a,string b);



	// function 2: make a-b mode int a - b; 7 - (-3) = 10
	// input: 两个字符串 a 和 b,里面放的都是整数;
	// output: 返回一个字符串,字符串里面是整数;
	// 功能: 实现参数两个整数的相减操作,结果存在返回的字符串里
	static string MINUS_Int(string a,string b);

	// function 3: make a*b mode int a * b;
	// input: 两个字符串 a 和 b,里面放的都是整数;
	// output: 返回一个字符串,字符串里面是整数;
	// 功能: 实现参数两个整数的相乘操作,结果存在返回的字符串里
	static string MULT_Int(string a,string b);

	// function 4: mode the division a/b
	// input: 两个字符串 a 和 b,里面放的都是整数;
	// output: 返回一个字符串,字符串里面是整数;
	// 功能: 实现参数两个整数的相除操作,结果存在返回的字符串里
	static string DIV_Int(string a,string b);

	// function 5: pow number a^b
	// input: 两个字符串 a 和 b,里面放的都是整数;
	// output: 返回一个字符串,字符串里面是整数;
	// 功能: 实现参数两个整数的a^b操作,结果存在返回的字符串里
	static string Pow_Int(string a,string b);

	// function 6: int To string :"123" = 123
	// input: 一个int数 a;
	// output: 返回一个字符串,字符串里面是整数;
	// 功能: 将整数a转换成对应的字符串格式
	static string Int_To_String(int x);

	// function 7: static char division a/b : 4 / 3
	static string Division(string a,string b);

	// function 8: make a-b mode int a - b; 4 - 3
	static string MinusInt(string a,string b);

	// function 9: mode the add of int :3 + 4
	static string AddInt(string a,string b);

	// function 10: make char to the int number :'9' = 9
	static int CharToNumber(char c);

	// function 11: make int to the model char : 7 = '7'
	static string IntToChar(int i);

	// function 12: check whether the string is legal 
	static bool Check_all_number(string a);

	// function 13: compare string a and b
	// input: 两个字符串 a 和 b,里面放的都是整数;
	// output: 返回一个字符,字符里是a和b的大小关系;
	// 功能: 实现参数两个整数的a和b比较操作,结果< or = or >存在返回的字符里
	static char Compare(string a,string b);

	// function 14: make string into standard string number
	static bool Standardization(string &a);

	// function 15: make string(>0) into standard int number
	// input: 一个字符串 a,里面放的是一个整数;
	// output: 返回一个字符串,字符串里是a对应的整形数据;
	// 功能: 将存在字符串里的整数取出来,放在整形容器里,然后返回,根据返回的结果可以判定是否转换成功
	static std::pair<bool,int> String_into_intNumber(string &a);


	// function 16: get data   
	// input: a int number data with string   
	// output: a string number b   
	// 功能: 实现 sqrt 函数   
	static string Sqrt_data( string data ,int weishu);  

	// function 17: go larger   
	// input: a int keyword with string , and the int keyword key   
	// output: a double number    
	// 功能: 尝试找到一个区间使得 a^2 <= key <= b^2   
	static string _make_qujian(string key,int weishu);  

	// function 18: binary search   
	// input: two int numbers s and b, and the int keyword key and weishu   
	// output: a double number with string   
	// 功能: 实现sqrt函数,找到 r^2 == key   
	static string _binary_search(string s,string b,string key,int weishu);  

	// function 19: get perfect double   
	// input: two strings with data and key ,weishu    
	// output: a string with result(data < result < data+1) ,and result^2 <= key   
	// 功能: 实现整数开方精确到小数部分   
	static string _get_perfect(string data,string key,int weishu);  

	// function 19: get more number   
	// input: two string data and key ,(bigger)   
	// output: a pair with <bool,string>,if(true) perfect string,or bad string   
	// 功能: 进一步精确小数的位数   
	static std::pair<bool,string> _get_more_number(string data,string key);


	// function 20: 求余操作
	// input: two int number within string
	// output: a int number within string
	// 功能: 求出a%b
	static string Qiuyu(string a,string b);

	// function 21: Qiu fanshu
	// input: a int number within string
	// output: a int number within string
	// 功能: 实现取反操作
	static string Qiu_fanshu(string a);


	// function 22: data to binary system
	// input: a int number within string
	// output: a fuhao wei within bool and a binary_system result with string
	// 功能 : 把一个十进制数转换成二进制数
	static std::pair<bool,string> Data_to_Binary_system(string data);
};



// mode the add of int
string String::ADD_Int(string a,string b)
{
	// exception of input
	if( a.empty() )
		return b;
	else if( b.empty() )
		return "0";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		return "exception of input ADD_Int";
	}
	Standardization(a);
	Standardization(b);	

	if(a[0] != '-' && b[0] != '-')
		return AddInt(a,b);
	else if(a[0] != '-'&& b[0] == '-')		
		return MinusInt( a,b.substr( 1,b.size() ) );
	else if(a[0] == '-'&& b[0] != '-')
		return MinusInt(b,a.substr(1,a.size()));
	else return '-'+AddInt(a.substr(1,a.size()),b.substr( 1,b.size() ));
};







// make a-b mode int a - b;
string String::MINUS_Int(string a,string b)
{
	// exception of input
	if( a.empty() )
		return b;
	else if( b.empty() )
		return "0";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		return "exception of input Multiplies_Int";
	}
	Standardization(a);
	Standardization(b);	
	if(a[0] != '-' && b[0] != '-')
		return MinusInt(a,b);
	else if(a[0] != '-' && b[0] == '-')
		return AddInt(a,b.substr(1,b.size()));
	else if(a[0] == '-' && b[0] != '-')
		return "-"+AddInt(a.substr(1,a.size()),b);
	else return MinusInt( b.substr(1,b.size()) , a.substr(1,a.size()) );
};






// make a*b mode int a * b;
string String::MULT_Int(string a,string b)
{
	// exception of input
	if( a.empty() )
		return b;
	else if( b.empty() )
		return "0";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		return "exception of input Multiplies_Int";
	}
	Standardization(a);
	Standardization(b);	
	string::size_type i = a.size(),j = b.size();
	string c = "0",d = "";
	bool fushu = (a[0] == '-' && b[0] != '-')||(a[0] != '-' && b[0] == '-');
	if(a[0] == '-')	
		a = a.substr(1,a.size());		
	if(b[0] == '-')	
		b = b.substr(1,b.size());

	int jinwei = 0;
	for( j = b.size()-1 ; j < b.size() ;j--)
	{
		// each number of b to * a 
		jinwei = 0;
		for( i = a.size()-1 ; i < a.size() ;i-- )
		{
			d = IntToChar(   ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) % 10 )+ d ;
			jinwei = ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) / 10 ;
		}
		if(jinwei)
			d = IntToChar(jinwei) +d;
		// add all number result
		c = ADD_Int(c,d);
		d = "";
		unsigned int zero = 0 ;
		while( zero < b.size() - j )
		{
			d = d + '0';
			zero ++;
		}

	}

	Standardization(c);
	if( fushu && c != "0" )
		return '-'+c;
	else return c;
};




// mode the division a/b
string String::DIV_Int(string a,string b)
{
	// exception of input
	if( a.empty() )
		return "0";
	else if( b.empty() )
		return "e";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		return "exception of input DIV_Int";
	}
	Standardization(a);
	Standardization(b);	
	if(b == "0")
		return "e";
	bool fushu =  (a[0] == '-' && b[0] != '-')||(a[0] != '-' && b[0] == '-');
	if( a[0] == '-' )	
		a = a.substr(1,a.size());		
	if( b[0] == '-' )	
		b = b.substr(1,b.size());
	if( Compare(a,b) == '<' )
		return "0";


	string yushu = "";

	string beichushu = a.substr(0,b.size());	
	string shang = Division( beichushu , b);
	yushu =  MinusInt( beichushu ,MULT_Int( shang, b) );
	string c = shang;

	for(string::size_type i = b.size(); i<a.size(); i++)
	{	
		beichushu =   yushu+ a[i]     ;
		shang = Division( beichushu , b);
		c = c + shang;			
		yushu =  MinusInt( beichushu ,MULT_Int( shang, b) );
	}
	Standardization(c);
	return fushu?('-'+c):c;
};





// function: pow number x,y
string String::Pow_Int(string a,string b)
{
	// exception of input
	if( a.empty() )
		return "0";
	else if( b.empty() )
		return "e";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		return "exception of input DIV_Int";
	}
	Standardization(a);
	Standardization(b);	
	string result = "1" ;
	if(Compare(b,"0") != '<')
		for(string i ="0" ;Compare(i,b) == '<' ;i = AddInt(i,"1"))
		{
			result = MULT_Int(result,a);
		}
	else 
		for(string i ="0" ;Compare(i,b) == '>' ;i = MINUS_Int(i,"1"))
		{
			result = DIV_Int(result,a);
		}
		return result ;
};






// function : int To string 
string String::Int_To_String(int x)
{
	bool fushu = false;
	string result="";
	if(x < 0 )
	{
		fushu = true ;
		x = -x;
	}
	else if( x == 0 )
		return "0";
	while(x)
	{
		result = IntToChar(x % 10) + result;
		x = x/10;
	}
	if(fushu)
		result = "-"+result;
	return result;
};





// static char division a/b
string String::Division(string a,string b)
{
	// exception of input
	if( a.empty() )
		return "0";
	else if( b.empty() )
		return "e";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		cout<<"exception of input Division"<<endl;
		return "e";
	}
	Standardization(a);
	Standardization(b);	
	int i = 0;
	while( i<=9 )
	{
		// if a - b*i < b
		if(  Compare(   MINUS_Int(   a  ,   MULT_Int(IntToChar(i),b)    ) , b ) == '<'    )
			break;
		i++;
	}
	if( i>9 )
		return "e";
	return ""+IntToChar(i);
};






// make a-b mode int a - b;
string String::MinusInt(string a,string b)
{
	// exception of input
	if(!Check_all_number(a) || !Check_all_number(b))
		return "exception of input MinusInt";
	Standardization(a);
	Standardization(b);
	// particular string of input
	if(a.empty())
	{
		if(b.empty())
			return "0";
		else
			return "-"+b;
	}
	else if(b.empty())
	{
		return a;
	}

	// normal number a < b
	string c = "";
	bool check = true ;
	if(Compare(a,b) == '=')
		return "0";
	else if(Compare(a,b) == '<')
	{
		c = a ;
		a = b ;
		b = c ;
		c = "";
		check = false ;
	}
	// normal number a >= b
	string::size_type i = a.size()-1, j = b.size()-1;
	int jiewei = 0,now;

	while(i < a.size() && j < b.size())
	{
		now = CharToNumber(a[i]) - CharToNumber(b[j]) - jiewei ;

		if( now < 0 )
		{
			jiewei = 1;
			now = 10 + now ;
		}
		else jiewei = 0;
		c = IntToChar(now)  + c ;
		i--;j--;
	}
	while(i < a.size())
	{
		now = CharToNumber(a[i]) - jiewei ;
		if( now < 0 )
		{
			jiewei = 1;
			now = 10 + now ;
		}
		else jiewei = 0;
		c = IntToChar( now )  + c ;
		i--;
	}
	Standardization(c);
	if(!check)
		c = '-' + c;		
	return c; 
};







// mode the add of int
string String::AddInt(string a,string b)
{
	// exception of input
	if( a.empty() )
		return b;
	else if( b.empty() )
		return "0";
	if(!Check_all_number(a) || !Check_all_number(b))
	{
		return "exception of input AddInt";
	}
	Standardization(a);
	Standardization(b);
	string::size_type i = a.size()-1 ,j = b.size()-1 , k = 0 ;
	string c = "";
	int jinwei = 0;
	while( i < a.size() && j < b.size() )
	{
		c = IntToChar( ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) % 10 ) + c;
		jinwei = ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) / 10;
		j--;i--;
	}
	while( j < b.size()  )
	{
		c =  IntToChar( ( CharToNumber(b[j]) + jinwei ) % 10 ) + c;
		jinwei = ( jinwei + CharToNumber(b[j]) ) / 10;	
		j--;
	}
	while( i < a.size() )
	{
		c =  IntToChar( ( CharToNumber(a[i]) + jinwei ) % 10 ) + c;
		jinwei = ( jinwei + CharToNumber(a[i]) ) / 10;	
		i--;
	}
	if( jinwei )
		c = IntToChar(  jinwei  ) + c;
	Standardization(c);
	return c;
};







// make char to the int number
int String::CharToNumber(char c)
{
	if( c >= '0' && c <= '9' )
		return int(c - '0');
	else 
	{
		cout<<c<<" exception of input CharToNumber "<<endl;
		system("pause");
		return 0;
	}
};







// make int to the model char
string String::IntToChar(int i)
{
	if( i >= 0 && i <= 9 )
	{
		string c = "";
		return c+char(i+48);
	}
	else
	{
		cout<<i<<" exception of input IntToChar"<<endl;
		system("pause");
		return "e";
	}
};






// check whether the string is legal 
bool String::Check_all_number(string a)
{
	if(a.empty())
		return true ;
	string::size_type L = a.size(),i = 0;
	if(a[0] == '-')
		i++;
	while( i < L )
	{
		if( a[i] < '0' || a[i] > '9')
			return false;
		i++; 
	}
	return true ;
};







// compare string a and b
char String::Compare(string a,string b)
{
	if(a.empty() || b.empty())
	{
		cout<<"error of input compare";
		return 'e';
	}
	else
	{
		if(!Check_all_number(a) || !Check_all_number(b))
		{
			return 'e';
		}
		Standardization(a);
		Standardization(b);
		if(a[0] == '-' && b[0] != '-')
			return '<';
		else if( a[0] != '-' && b[0] == '-')
			return '>';
		bool fushu = (a[0] == '-');

		if(a.size() > b.size() )
			return fushu?'<':'>';
		else if(a.size() == b.size())
		{
			for(string::size_type i = 0;i < a.size(); i++)
			{
				if(a[i] > b[i])
					return fushu?'<':'>';
				if(a[i] < b[i])
					return fushu?'>':'<';
			}
			return '=';
		}			
		return fushu?'>':'<';
	}
};







// make string into standard string number
bool String::Standardization(string &a)
{
	if(!Check_all_number(a))
	{
		cout<<a<<" exception of input Standardization"<<endl;
		return false;
	}
	string::size_type i = 0 ;
	bool fushu = false ;
	if( a[0] == '-' )
	{
		fushu = true ;
		i = 1 ;
	}
	while(i < a.size())
	{
		if(a[i] != '0')
			break;
		i++;
	}
	if(i == a.size())
		i--;
	a = a.substr(i,a.size()) ;
	if( fushu && a != "0")
		a = '-' + a ;
	return true ;
};







// make string(>0) into standard int number
std::pair<bool,int> String::String_into_intNumber(string &a)
{
	if(Standardization(a))
	{
		int max_int = numeric_limits<int>::max()-1 ;
		string max = Int_To_String(max_int);
		bool fushu = false;
		if(a[0] == '-')
		{
			fushu = true ;
			a = a.substr(1,a.length());
		}
		if(Compare(a,max) != '<')
		{
			cout<<"溢出 exception"<<endl;
			return std::make_pair(false,0);
		}
		int result = 0 ;
		for(size_t i =0;i<a.length();i++)
		{
			result = result * 10 + CharToNumber(a[i]);
		}
		if(fushu)
			result = - result;
		return std::make_pair(true,result);
	}
	else
	{
		cout<<"exception of function String_into_intNumber input"<<endl;
		return std::make_pair(false,0);
	}
};



// function: get data
// input: a int number data with string
// output: a double number b with string
// 功能: 实现 sqrt 函数
string String::Sqrt_data( string data ,int weishu)
{
	if(weishu < 0)
		weishu = 0;
	if(! String::Check_all_number(data))
	{
		cout<<"exception of function _sqrt_data input"<<endl;
		return "NULL";
	}
	String::Standardization(data);
	char r = String::Compare(data,"0");
	if(r == '=')
		return "0";
	else if(r == '<')
	{
		cout<<"exception of function _sqrt_data input"<<endl;
	}
	return _make_qujian(data,weishu);
}

// function: go larger
// input: two int number a and b, and the int keyword key
// output: a double number with string
// 功能: 尝试找到一个区间使得 a^2 <= key <= b^2
string String::_make_qujian(string key,int weishu)
{

	if( String::Compare(key,"0") == '>' && weishu >=0 )
	{
		string s,b;
		int length = key.length()/2;
		// 偶数
		if(key.length() % 2 == 0)
		{
			s = String::Pow_Int("10",String::Int_To_String(length-1));
			b = String::Pow_Int("10",String::Int_To_String(length));
		}
		// 奇数
		else
		{
			s = String::Pow_Int("10",String::Int_To_String(length));
			b = String::Pow_Int("10",String::Int_To_String(length+1));
		}
		return _binary_search(s,b,key,weishu);// binary search
	}
	else
	{
		cout<<"exception of function _go_larger input"<<endl;
		return "NULL";
	}
}


// function: binary search
// input: two int numbers s and b, and the int keyword key
// output: a double number with string
// 功能: 实现sqrt函数,找到 r^2 == key
string String::_binary_search(string s,string b,string key,int weishu)
{
	if( String::Compare(s,"1")!= '<' && String::Compare(b,s) != '<' && weishu >= 0 )
	{
		string s_2 = String::MULT_Int(s,s);
		string b_2 = String::MULT_Int(b,b);
		if(String::Compare(key,s_2) == '=')
			return s;
		if(String::Compare(key,b_2) == '=')
			return b;
		string zhong ,zhong_2,zhong2_2;
		char r ;
		while(String::Compare(s,b) != '>')
		{
			zhong = String::DIV_Int(String::ADD_Int(s,b) ,"2");
			//zhong = s + (b-s)/2;
			zhong_2 = String::MULT_Int(zhong,zhong);
			r = String::Compare(zhong_2,key);
			if(r == '>')
				b = String::MINUS_Int(zhong,"1");
			else if( r == '=' )
				return zhong;
			else if(r == '<')
			{
				zhong2_2 = String::MULT_Int(String::ADD_Int(zhong,"1"),String::ADD_Int(zhong,"1"));
				r = String::Compare(zhong2_2,key);
				if(r == '>')
					return _get_perfect(zhong,key,weishu);
				else s = String::ADD_Int(zhong,"1");
			}
		}
	}
	else
	{
		cout<<"exception of function _binary_search input"<<endl;

	}
}

// function: get perfect double
// input: two strings with data and key
// output: a string with result(data < result < data+1) ,and result^2 <= key
// 功能: 实现整数开方精确到小数部分
string String::_get_perfect(string data,string key,int weishu)
{
	int i = weishu;
	string d = data;
	string k = key;
	string result;
	std::pair<bool,string> r ;


	while(i > 0)
	{
		r = _get_more_number(d,k);
		if(r.first == true)
			break;
		else 
		{
			d = r.second;
			k += "00";
		}
		i--;
	}
	if(weishu != 0)
		result = data+"."+d.substr(data.length(),d.length()-data.length());
	else result = data;
	return result;
}

// function: get more number
// input: two string data and key ,(bigger)
// output: a pair with <bool,string>,if(true) perfect string,or bad string
// 功能: 进一步精确小数的位数
std::pair<bool,string> String::_get_more_number(string data,string key)
{
	char s = '1',e ='9',zhong,guanxi1,guanxi2 ;
	key += "00" ;

	string r1,r2 ;
	r1 = String::MULT_Int((data+'9'),(data+'9')) ;
	guanxi1 = String::Compare(r1,key) ;
	if(guanxi1 == '<')
		return std::make_pair(false,data+'9');
	else if(guanxi1 == '=')
		return std::make_pair(true,data+'9');
	r1 =  String::MULT_Int((data+'1'),(data+'1'));
	guanxi1 = String::Compare(r1,key);
	if(guanxi1 == '=')
		return std::make_pair(true,data+'1');
	else if(guanxi1 == '>')
		return std::make_pair(false,data+'0');
	while(s <= e)
	{
		zhong = (s+e)/2;

		r1 = String::MULT_Int((data+zhong),(data+zhong));
		guanxi1 = String::Compare(r1,key);
		if(guanxi1 == '=')
		{
			return std::make_pair(true,data+zhong);
		}
		else if(guanxi1 == '>')
			e = zhong -1;
		else if(guanxi1 == '<')
		{
			r2 = String::MULT_Int((data+char(zhong+1)),(data+char(zhong+1)));
			guanxi2 = String::Compare(r2,key); 
			if(guanxi2 == '>')
				return std::make_pair(false,data+zhong);
			else s = zhong+1;
		}
	}
}


// function: 求余操作
// input: two int number within string
// output: a int number within string
// 功能: 求出a%b
string String::Qiuyu(string a,string b)
{
	if(String::Compare(a,"0") != '>' || String::Compare(b,"0") != '>')
		return "error of function Qiuyu input";
	string shang = String::DIV_Int(a,b);
	string chengji = String::MULT_Int(shang,b);
	return String::MINUS_Int(a,chengji);
}


// function: Qiu fanshu
// input: a int number within string
// output: a int number within string
// 功能: 实现取反操作
string String::Qiu_fanshu(string a)
{
	if(String::Check_all_number(a))
	{
		String::Standardization(a);
		if(a[0] == '-')
			return a.substr(1,a.length()-1);
		else return ("-"+a) ;
	}
	else 
	{
		cout<<"exception of function Qiu_fanshu input"<<endl;
		return "error";
	}
}

// function: data to binary system
// input: a int number within string
// output: a fuhao wei within bool and a binary_system result with string
// 功能 : 把一个十进制整数转换成二进制数
std::pair<bool,string> String::Data_to_Binary_system(string data)
{
	if(String::Check_all_number(data))
	{
		String::Standardization(data);
		string result = "";
		if(String::Compare(data,"0") == '=')
			return std::make_pair(false,"0");
		bool fushu = false;
		if(String::Compare(data,"0") == '<')
		{
			data = String::Qiu_fanshu(data);
			fushu = true;
		}
		while(String::Compare(data,"0") == '>')
		{
			result = String::Qiuyu(data,"2") + result ;
			data = String::DIV_Int(data,"2");
		}
		return std::make_pair(fushu,result);
	}
	else
	{
		cout<<"exception of function Data_to_Binary_system input"<<endl;
		return make_pair(true,"error");
	}
}


test.cpp

#include<iostream>
#include<fstream>
#include"mine_string.h"
using namespace std;

// function: _gossip
// input: a int number with gossip's rows
// output: void
// 功能: 构造一个row = n 的gossip,并输出
void _Gossip(int n);


// function: main
int main()
{
	int n ;
	cout<<"please input the number of gossip rows"<<endl;
	while(cin>>n)
	{
		_Gossip(n);
		cout<<"please input the number of gossip rows"<<endl;
	}
	system("pause");
	return 0;
}

// function: _gossip
// input: a int number with gossip's rows
// output: void
// 功能: 构造一个row = n 的gossip,并输出
void _Gossip(int n)
{
	if(n > 0)
	{
		// first action: get space
		string ** data = new string*[n];
		for(int i = 0;i<n;i++)
			data[i] = new string[i+1];
		
		// second action: get data for gossip
		data[0][0] = "1";
		for(int i=0;i < n;i++)
		{
			data[i][0] = "1" ;
			for(int j = 1; j<i; j++)
			{
				data[i][j] = String::ADD_Int(data[i-1][j-1],data[i-1][j]);
			}
			data[i][i] = "1" ;
		}

		// third action: output the data into a file
		ofstream writer;
		writer.open("gosssip.txt");
		writer.clear();
		cout<<"gossip follows"<<endl;
		for(int i =0;i<n;i++)
		{
			for(int j =0;j<i+1;j++)
				writer<<data[i][j]<<' ';
			writer<<"\n";
		}
		cout<<"data output over"<<endl;
		writer.close();

	}
	else
	{
		cout<<"exception of function _gossip input"<<endl;
	}
}



 

 

巴斯卡(Pascal)三角形 (C语言实现) 以前读书的时候没有用C语言编写什么东西,感觉C语言的功能有限,现在工作后觉的C语言的功能真的很强大,可以做很多事情。所以现在重新把C语言好好学习一下。在博客上记录自己的一点一滴,看着自己成长进步,为了梦想而奋斗,技术改变未来。 阅读详情

相关推荐

Tecplot进阶:巧用公式与多Frame对比,实现CFD多工况数据差异的可视化分析

本文详细介绍了如何利用Tecplot进行CFD多工况数据差异的可视化分析,通过公式计算和多Frame对比,实现变量差值自动计算、多视图同步展示和差异量化统计。文章还提供了数据准备、基础操作、实战技巧和常见问题解决方案,帮助工程师高效完成CFD后处理工作。

weixin_29227425的博客 531

C++经典算法题-巴斯卡三角形

3. 巴斯卡三角形 代码示例 #include <stdio.h> #define N 12 long combi(int n, int r){ int i; long p = 1; for(i = 1; i <= r; i++) p = p * (n-i+1) / i; return p; } ...

逍遥云恋 1124

逆向拼多多长连接:Frida+Python协议还原实战

长连接是现代移动App实现低延迟、高并发通信的核心技术,其原理在于复用TCP通道避免HTTP频繁建连开销,并通过自定义二进制协议提升传输效率与安全性。在电商场景中,长连接不仅承载业务数据,更集成了动态密钥协商、设备指纹绑定、应用层加密等多重防护机制,显著提升数据窃取与自动化采集难度。本文聚焦真实商业级长连接逆向工程,以拼多多(PDD)v12.86.0为案例,详解如何利用Frida在JNI层精准Hook encrypt/decrypt函数捕获明文,结合Python构建协议解析器与会话管理器,实现对商品详情(I

weixin_30843121的博客 1120

巴斯卡三角形 C++版

/* 帕斯卡三角形,是一个三角形矩阵,其顶端是 1,视为(row0). 第1行(row1)(1&1)两个1,这两个1是由他们上头左右两数之和  (不在三角形内的数视为0).依此类推产生第2行(row2):0+1=1; 1+1=2;1+0=1.第3行(row3):0+1=1;1+2=3; 2+1=3;1+0=1. 循此 法可以产生以下诸行。 */ #include #include

sanjinjin的博客 1861

SD--根据订单创建出库单(相关的函数列表的介绍系列篇(2))

创建订单样例代码参见:http://blog.csdn.net/CompassButton/archive/2007/05/09/1601712.aspx 可以根据一个订单或者部分订单项来创建出库单,代码如下:* Populating the item data lx_sales_item-ref_doc = l_vbeln. lx_sales_item-ref_item = c_000010

SAP BLOG--Jack Wu 6226

ubuntu安装eigen3.3.7

查看已安装版本: gedit /usr/include/eigen3/Eigen/src/Core/util/Macros.h 或 gedit /usr/local/include/eigen3/Eigen/src/Core/util/Macros.h 卸载先前版本: sudo updatedb locate eigen3 得到eigen库的位置 卸载: sudo rm -rf /usr/include/eigen3 /usr/lib/cmake/eigen3 ...

reason的博客 1万+

巴斯卡(Pascal)三角形

背景知识                 巴斯卡是十七世纪的一位法国数学家,也是历史上第一位发明了加法计算机的人!他造出“巴斯卡三角形”的方法是这样的:先在纸上写出一行和一列的“ 1 “ ,然后在各个位置中填入数字,每一个位置上的数字都是它上面一个数和左边一个数的和。接下来,把这个表右转45 °,放正就得到上面的数字三角形了。        现在的数学书里,都把这个三角形称为“巴斯卡三

杰哥的专栏 2441

Temporal Noise Reduction(TNR)时序去噪技术解析:原理、平台实践与工程调优路径

随着高画质夜景拍照和高清视频拍摄需求的持续增长,图像传感器在低照度环境下输出信号的噪声问题日益突出。相比空间域降噪(SNR),时序去噪(Temporal Noise Reduction, TNR)利用帧间信息进行融合处理,具备更高的信噪比提升效率及更强的边缘保护能力,已成为移动影像ISP链路中的核心环节。本文系统剖析TNR的算法原理、平台实现机制与工程调试路径,并基于实际开发案例分析其在不同场景下的效果差异及调优方法,为工程师提供从理论到实操的完整路径参考。

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 3206

C++代码示例1~巴斯卡三角形

巴斯卡三角形是两个边全输出1,三角形的内部用上行相邻两个数之和,各相加后的和数等分地放在三角形的各行内。 实现代码如下所示: #include <iomanip> #include <iostream> using namespace std; long combi(int n, int r) { int i; long p = 1; for (i...

shouwangyunkai666的博客 942

基于深度学习的推荐系统设计与实现

基于深度学习的推荐系统设计与实现 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming / TextGenWebUILLM 基于深度学习的推荐系统设计与实现 关键词:深度学习, 推荐系统, 用

AI天才研究院 1079

YOLOv11改进策略【Head】| AFPN渐进式自适应特征金字塔,增加针对小目标的检测头(附模块详解和完整配置步骤)

一、本文介绍 本文利用AFPN对YOLOv11的网络模型进行优化提升。AFPN采用渐近式融合及自适应空间融合操作。通过渐近式架构,逐步整合不同层级特征,有效避免非相邻层级间因语义差距过大导致的信息丢失或降级问题,确保在多尺度特征融合过程中既能保留高层语义信息,又能融入低层细节特征。本文将AFPN应用于YOLOv11中,并配置了针对小目标的检测头,能显著增强模型对不同尺度目标的检测能力,使其在复杂场景下更聚焦于目标物体特征,减少背景等无关信息的影响。 专栏目录:YOLOv11改进目录一览 | 涉及卷积层、

Limiiiing的博客 2718

巴斯卡三角形算法的C语言实现

巴斯卡(Pascal)三角形基本上就是在解 nCr ,因为三角形上的每一个数字各对应一个nCr,其中 n 为 row,而 r 为 column,如下:    0C0   1C0 1C1  2C0 2C1 2C2 3C0 3C1 3C2 3C34C0 4C1 4C2 4C3 4C4   对应的数据如下图所示:    解法 巴斯卡三角形中的 nCr 可以使用以下这个公式来计算,以避免阶乘运算...

从零开始 704

Agentic AI为教育行业赋能,提示工程架构师解析应用潜力

然而,即便是最先进的 LLMs,在面对复杂、动态、需要持续规划和调整的教育场景时,也常常显得力不从心,它们更像是强大的“问答机器”,而非真正意义上的“学习伙伴”或“教学助手”。在教育场景中,传统的 AI 工具可能只能回答学生提出的特定问题,或者根据预设的规则推送一些学习材料。无论您是教育技术领域的开发者、产品经理,还是一线的教育工作者、教育管理者,抑或是对 AI 教育应用充满好奇的研究者,本文都将为您提供宝贵的 insights,助您把握 Agentic AI 时代教育创新的脉搏。

架构师的AI之路,分享AI应用开发架构的学习与实践。 467

一种排列好看巴斯卡三角形

在经典算法50上看到的算法。算法精妙浓缩到p = p * (n-i+1) / i;上。 但是这个打印出来的金字塔不好排列,我写了两个方法把他改好看一点,使用最大数的长度+2来做为标准的间隔。 高度 n=6 高度 n=12 代码如下: public static void main(String[] args) { int N=6; //三角形的行高。 in...

小jinx的博客 392

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

巴斯卡(Pascal)三角形Java算法

用Java写了一个巴斯卡(Pascal)三角形的小程序,代码如下:import java.util.Scanner;/** * 巴斯卡(Pascal)三角形算法解析 * @author qjx * */public class PascalTest { public static void main(String[] args){ PascalTest p

JavaSky的专栏 8864

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

2-巴斯卡三角形

巴斯卡三角形,和杨辉三角一样 #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #define N 12 long combi(int n, int r) { int i; long p = 1; for (i = 1; i <= r; i++) { p = p * (n - i + 1) / i; } return p; } void paint(void) { int n, r, t; for (n

南山府的博客 739

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

巴斯卡三角形c语言,巴斯卡(Pascal)三角形 (C语言实现)

以前读书的时候没有用C语言编写什么东西,感觉C语言的功能有限,现在工作后觉的C语言的功能真的很强大,可以做很多事情。所以现在重新把C语言好好学习一下。在博客上记录自己的一点一滴,看着自己成长进步,为了梦想而奋斗,技术改变未来。巴斯卡(Pascal)三角形基本上就是在解 nCr ,因为三角形上的每一个数字各对应一个nCr,其中 n 为 row,而 r 为 column,如下:0C01C0 1C12C...

weixin_42116847的博客 469
上一篇: 汉诺塔问题
下一篇: 算法之旅 快速排序 速度超过库函数,挑战 stl
p11323
博客等级 码龄14年 79粉丝 197原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值