Click here to Skip to main content
15,911,139 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: consecutive sums Pin
minikg18-Sep-01 16:04
minikg18-Sep-01 16:04 
GeneralRe: consecutive sums Pin
minikg19-Sep-01 12:28
minikg19-Sep-01 12:28 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 12:47
protectorChristian Graus19-Sep-01 12:47 
GeneralRe: consecutive sums Pin
minikg19-Sep-01 13:22
minikg19-Sep-01 13:22 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:23
protectorChristian Graus19-Sep-01 14:23 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:40
protectorChristian Graus19-Sep-01 14:40 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:42
protectorChristian Graus19-Sep-01 14:42 
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 14:58
protectorChristian Graus19-Sep-01 14:58 
This will work but has IMHO ceased to be elegant.

#include "stdafx.h"

#include <iostream>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;

int main(int argc, char* argv[])
{
	
	// It's a good idea to declare variables as you need them, not all at the top like this
	int input;

	cout <<"please enter a number ";
	cin >> input;

	for (int i = 2; i < 10; ++i)
	{
		if ((input%i) == (i-1)) break;
	}

	if (i == 10)
	{
		cout << "No solution found\n";
		return 0;
	}

	int lowest = floor(input/i);
// Added to make sure we never provide a solution of 0 + 1 + ....
	if (lowest < 1) lowest = 1;

// Keep track of the sequence because sequences starting from 1 seem to overrun right now, this fixes that.
	int sequence = 0;

	for (;i>0;--i)
	{
// Make sure we never overrun our value
		sequence += lowest;
		if (sequence > input) break;
		
		cout << lowest++;
		if (i > 1 && sequence + lowest <= input) cout << " + ";
	}
	cout << " = " << input << endl;

	return 0;
}


I'm at work so I can't devote any more time to it, but hopefully you can see what direction I'm heading. Right now I am managing cases my algorithm won't catch after the fact, which is *bad*. Maybe you can see where I have gone wrong. Try putting 6 into the first code I posted and you'll see it does not work, and it's pretty clear why. I just can't quickly wrap my head around itwhile other people are waiting for me to do some work so they can also continue with theirs.

If you don't get it sorted today I'll be happy to play with it more tonight.



Christian

As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.

Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
GeneralRe: consecutive sums Pin
Christian Graus19-Sep-01 16:32
protectorChristian Graus19-Sep-01 16:32 
QuestionHow do you remove help from a project? Pin
Cathy18-Sep-01 14:21
Cathy18-Sep-01 14:21 
AnswerRe: How do you remove help from a project? Pin
Tomasz Sowinski19-Sep-01 2:02
Tomasz Sowinski19-Sep-01 2:02 
GeneralRe: How do you remove help from a project? Pin
Cathy19-Sep-01 10:52
Cathy19-Sep-01 10:52 
Questionhelp, Implement Flashwindow like MNS Msg? Pin
OGIR18-Sep-01 12:30
OGIR18-Sep-01 12:30 
AnswerRe: help, Implement Flashwindow like MNS Msg? Pin
Michael P Butler18-Sep-01 22:21
Michael P Butler18-Sep-01 22:21 
Generalchar* as template parameter Pin
Aaron Schaefer18-Sep-01 12:22
Aaron Schaefer18-Sep-01 12:22 
GeneralRe: char* as template parameter Pin
Michael Dunn18-Sep-01 21:16
sitebuilderMichael Dunn18-Sep-01 21:16 
GeneralRe: char* as template parameter Pin
Aaron Schaefer19-Sep-01 3:48
Aaron Schaefer19-Sep-01 3:48 
GeneralDebug vs Release Pin
18-Sep-01 11:58
suss18-Sep-01 11:58 
GeneralRe: Debug vs Release Pin
Tomasz Sowinski19-Sep-01 1:06
Tomasz Sowinski19-Sep-01 1:06 
GeneralWin32 Printing Problems - 32character device names. Pin
Mike Doner18-Sep-01 11:19
Mike Doner18-Sep-01 11:19 
GeneralWin2000/XP Job Kernel Object Pin
18-Sep-01 11:12
suss18-Sep-01 11:12 
GeneralMFC Extension DLL Pin
sfanjoy18-Sep-01 10:56
sfanjoy18-Sep-01 10:56 
GeneralRe: MFC Extension DLL Pin
Tomasz Sowinski18-Sep-01 11:10
Tomasz Sowinski18-Sep-01 11:10 
GeneralExecuting a database macro from VC++ Pin
duggie18-Sep-01 10:55
duggie18-Sep-01 10:55 
GeneralRe: Executing a database macro from VC++ Pin
Carlos Antollini18-Sep-01 11:01
Carlos Antollini18-Sep-01 11:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.