|
|
楼主 |
发表于 2005-12-6 16:03
|
显示全部楼层
6、编程:读入一个正整数,把其和是该整数的正整数序列输出,如27,结果为:2--7,8--10,13--14。1 N Z, I6 d0 l0 C& u3 w+ F5 A
T& o& z$ I$ l$ g. }
源程序如下:
3 V$ v- c h, s* Q1 y+ V9 D# T! N3 A#include "stdafx.h"
0 C& ?/ I/ ~' x/ G4 b* t; O#include <iostream>& B2 M5 n6 X. x
using namespace std;- Z# v7 z; ~% k f
: _3 T$ L' Q3 @- f% |6 Lint _tmain(int argc, _TCHAR* argv[])" r6 y; G2 `. \$ `3 G( Q, F
{
# D% b! q, K4 l/ [& i int n;
2 T4 X) u+ V& ~- h' w7 f7 _0 ^ cout<<"请输入正整数:";2 O; g0 \9 N; S1 [% ~
cin>>n;: {$ x5 q7 Q p, a
if(n<= 0)exit(0);
$ l" w7 N" F& D: e% y for(int j = 1; j< n; j++){
$ l2 ^. y5 [9 z% a int i = j;
+ [- [; S8 d. c& @3 E, A. L$ Y int begin = j;
" n+ T/ x, ~, F0 S6 T! ]$ h7 b, R @ int sum = 0;$ R6 N) _) ^* g) L3 P6 [
while(sum< n){4 p% B2 _ j S5 V; U6 {
sum = sum + i;- D$ `; H" H% e' u
i++;
1 A/ M- n N& ~# f% b }
' W% |; |1 C7 \9 b8 }% {( E w! ` if(sum == n){ //符合题目要求,输出结果, |( [4 ]0 J6 I9 |+ h
for(int k = begin;k < i; k++){. }3 V! k4 J; a
cout<<k<<" ";) n- d# E% T8 M( p2 M
}
, }$ {. ?: Q* r+ E. N) l' | cout<<endl;( T2 W. q. J7 |7 \
}0 i5 b7 V" N6 ]7 v( }4 r8 w( g4 X
}1 W$ K' W# v8 V2 H
cout<<"OK";
4 h: I) q* W. l" f9 c return 0;
2 C2 W, c. r& d! ~}' X+ ~2 A9 W5 K5 N/ K% b, A
% Z i& c: v! @6 T6 {7 \/ N
输出结果:
; G/ @! X' w g; ?请输入正整数:27
1 n- W7 `- b7 y$ l2 3 4 5 6 7
+ \; Z9 U, W* l- [8 9 10- H7 k9 m6 A% @( [) d2 J/ }
13 14
. V/ K- a! F% L; q- m$ t# LOKPress any key to continue+ M \8 {6 I3 \$ A! B
! z+ w* W' E# r8 B2 x, `[ Last edited by dy.f on 2005-12-9 at 12:09 ] |
|