[BUC-2.21]给出一个正整数,输出它所有可能的连续自然数(两个以上)之和的算式
2009-10-27 23:42
423 查看
利用等差数列的求和公司进行数序计算,判断计算的记过是否为整数来判断,是否合理。根据公式
n=((1-2a1)+sqrt((2a1-1)^2+8Sn))/2 ,其中a1在合理的范围内进行试探,1<=a1<=Sn/2
如何sqrt和n均求出为整数, 则根据n和a1输出这个连续自然数的和。
输出:给出3到100的数据输入,得出以下结果
n=((1-2a1)+sqrt((2a1-1)^2+8Sn))/2 ,其中a1在合理的范围内进行试探,1<=a1<=Sn/2
如何sqrt和n均求出为整数, 则根据n和a1输出这个连续自然数的和。
]/** include files **/ #include <iostream> #include <iomanip> #include <math.h> using namespace std; /** * Show =a1+(a1+1)+(a1+2)+...(a1+N-1) * * @author NeeSky (2009-10-27) * * @param a1 * @param N */ void ShowExpression(int a1, int N) { cout<<"="<<setw(2)<<a1; for(int i=1;i<N;++i) { cout<<"+"<<setw(2)<<a1+i; } cout<<endl; return; } /** * Judge the t is Positive Integer ? * @author NeeSky (2009-10-27) * * @param t * * @return bool */ bool isPositiveInteger(double t) { int tl=(int)t; if(t-tl<0.0000000001) return true; else return false; } /** * Find 2+3+4=9 4+5=9 if 9 is given. * * @author NeeSky (2009-10-27) * * @param Sum */ void FindContinuNatNum(int Sum) { int halfSum=Sum/2; for(int a1=1; a1<=halfSum; ++a1) { double b=sqrt((2*a1-1)*(2*a1-1)+8*Sum);//sqrt(b^2-4ac) if(isPositiveInteger(b)) { double resN=((1-2*a1)+b)/2; if(isPositiveInteger(resN)) { int N=static_cast<int>(resN); cout<<Sum; ShowExpression(a1,N); } } } return; } /** * The Main Programming * * @author NeeSky (2009-10-27) * * @param argc * @param argv * * @return int */ int main(int argc,int *argv[]) { cout<<"/n2) --- Find Continue Nature Number Sum --- "<<endl; for(int i=3;i<=100;i++) FindContinuNatNum(i); return (0); }
输出:给出3到100的数据输入,得出以下结果
3= 1+ 2 5= 2+ 3 6= 1+ 2+ 3 7= 3+ 4 9= 2+ 3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5+ 6+ 7+ 8+ 9+10 54=12+13+14+15 54=17+18+19 55= 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10 55= 9+10+11+12+13 55=27+28 56= 5+ 6+ 7+ 8+ 9+10+11 57= 7+ 8+ 9+10+11+12 57=18+19+20 57=28+29 58=13+14+15+16 59=29+30 60= 4+ 5+ 6+ 7+ 8+ 9+10+11 60=10+11+12+13+14 60=19+20+21 61=30+31 62=14+15+16+17 63= 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11 63= 6+ 7+ 8+ 9+10+11+12 63= 8+ 9+10+11+12+13 63=20+21+22 63=31+32 65= 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11 65=11+12+13+14+15 65=32+33 66= 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11 66=15+16+17+18 66=21+22+23 67=33+34 68= 5+ 6+ 7+ 8+ 9+10+11+12 69= 9+10+11+12+13+14 69=22+23+24 69=34+35 70= 7+ 8+ 9+10+11+12+13 70=12+13+14+15+16 70=16+17+18+19 71=35+36 72= 4+ 5+ 6+ 7+ 8+ 9+10+11+12 72=23+24+25 73=36+37 74=17+18+19+20 75= 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11+12 75=10+11+12+13+14+15 75=13+14+15+16+17 75=24+25+26 75=37+38 76= 6+ 7+ 8+ 9+10+11+12+13 77= 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11+12 77= 8+ 9+10+11+12+13+14 77=38+39 78= 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11+12 78=18+19+20+21 78=25+26+27 79=39+40 80=14+15+16+17+18 81= 5+ 6+ 7+ 8+ 9+10+11+12+13 81=11+12+13+14+15+16 81=26+27+28 81=40+41 82=19+20+21+22 83=41+42 84= 7+ 8+ 9+10+11+12+13+14 84= 9+10+11+12+13+14+15 84=27+28+29 85= 4+ 5+ 6+ 7+ 8+ 9+10+11+12+13 85=15+16+17+18+19 85=42+43 86=20+21+22+23 87=12+13+14+15+16+17 87=28+29+30 87=43+44 88= 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11+12+13 89=44+45 90= 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11+12+13 90= 6+ 7+ 8+ 9+10+11+12+13+14 90=16+17+18+19+20 90=21+22+23+24 90=29+30+31 91= 1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+10+11+12+13 91=10+11+12+13+14+15+16 91=45+46 92= 8+ 9+10+11+12+13+14+15 93=13+14+15+16+17+18 93=30+31+32 93=46+47 94=22+23+24+25 95= 5+ 6+ 7+ 8+ 9+10+11+12+13+14 95=17+18+19+20+21 95=47+48 96=31+32+33 97=48+49 98=11+12+13+14+15+16+17 98=23+24+25+26 99= 4+ 5+ 6+ 7+ 8+ 9+10+11+12+13+14 99= 7+ 8+ 9+10+11+12+13+14+15 99=14+15+16+17+18+19 99=32+33+34 99=49+50 100= 9+10+11+12+13+14+15+16 100=18+19+20+21+22
相关文章推荐
- 写一个程序,对于一个正整数,输出它所有可能的连续自然数(两个以上)之和的算式
- 所有可能的连续自然数(两个以上)之和的算式
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- 编程题:输入一个正整数,若该数能用几个连续正整数之和表示,则输出所有可能的正整数序列
- 网易笔试题:输入一个正整数,若该数能用几个连续正整数之和表示,则输出所有可能的正整数序列。
- 网易笔试题:输入一个正整数,若该数能用几个连续正整数之和表示,则输出所有可能的正整数序列。
- 输入一个正整数,若该数能用几个连续正整数之和表示,则输出所有可能的正整数序列。
- 将一个正整数n,拆分成连续的自然数之和,输出所有可能的情况
- 输入一个正整数,若该数能用几个连续正整数之和表示,则输出所有可能的正整数序列
- 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。例如输入数组{32, 321},则输出这两个能排成的最小数字32132
- 如果一个正整数可以由连续正整数求和而来,输出所有可能的组合
- 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。例如输入数组{32, 321},则输出这两个能排成的最小数字32132
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- N个数组,从每个数组中取出一个数,组成一个序列,输出所有可能的情况
- 12.输入一个正整数,输出它所有的因子
- 快速查找素数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在给你一个正整数N,要你快速的找出在2.....N这些数里面所有的素数。 输入 给出一个正整数数N(N<=
- 编程题:输入一个正整数数组,将他们拼接成一个数,输出拼接出所有数字中最小的一个
- 给定一个字符串,字符串中的*可以替换成0或1,输出所有可能的结果
- 输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
- 今天开始学Java 输入一个正整数,按照从小到大的顺序输出它的所有质数的因子