|
|
楼主 |
发表于 2005-12-6 16:03
|
显示全部楼层
6、编程:读入一个正整数,把其和是该整数的正整数序列输出,如27,结果为:2--7,8--10,13--14。
5 |" E; e( ]( p2 J8 t8 B U& F( {. B, O! w; @" M; s; V8 ]
源程序如下:
" Z3 `; J) B. D+ J+ o! h' E#include "stdafx.h"# N( ^5 t0 ]. h' w8 T$ S
#include <iostream>* v+ l5 t" l; V% R
using namespace std;- c: a9 c* E* }% H2 |
) F6 e* W7 T/ G7 @* C
int _tmain(int argc, _TCHAR* argv[])
# R* J0 P& J; B9 m{8 \7 J. a0 W2 M3 `) g. o" v2 m! C' j
int n;8 G% j/ ~6 n6 R
cout<<"请输入正整数:";" A+ M9 G! k, Q7 W6 {/ w
cin>>n;
* o% X" T: Y2 y& ? b0 Z if(n<= 0)exit(0);; q, U! _3 p- g; e
for(int j = 1; j< n; j++){' s5 ^* m9 [" a* n2 X; F/ [7 P4 W
int i = j;
9 M6 N- _- w- |& w; D9 Q int begin = j;
/ r- ?0 G: ]4 p r int sum = 0;
7 p& l% ^& d9 {4 c while(sum< n){
- y9 x, Q7 j# U2 x W* G sum = sum + i;
# L& U) i7 l' u) @% e i++; . p4 i+ D$ h/ c: Y
}
* l4 Y7 J5 {) Q$ s5 C+ a if(sum == n){ //符合题目要求,输出结果8 M6 A! _4 D# y3 h
for(int k = begin;k < i; k++){
. V1 z4 g3 F6 W! _1 {3 ]5 x* o0 r cout<<k<<" ";4 A. ?, A* i' u: P# |) ]) K
}! |: ?2 N, Y1 c' k. S
cout<<endl;
- ?* n( f: f" r3 D' X }
) C& B' ~& G( |2 y }9 T W% g+ a% }1 L
cout<<"OK";
" V9 N8 G4 V9 j+ C3 ] return 0;
2 q' X! s8 s* Q6 f! u' K}
! ]& g/ J* [9 E1 [3 A$ |+ u6 J5 N l6 y3 h0 |
输出结果:0 H( w: O. I, x4 E; j
请输入正整数:27
( V/ A$ q; K4 c5 w6 ? m2 3 4 5 6 7
6 S. Q8 j. S" }) f, ~8 9 105 u$ `/ R' t2 ^' U
13 142 O7 `4 i* s: z
OKPress any key to continue
6 _2 o& A$ f4 P8 U6 N6 X
5 d7 c* y4 p0 q0 Q4 E[ Last edited by dy.f on 2005-12-9 at 12:09 ] |
|