Happy 51 knots, everyone! LABOR is glorious!
Early in the morning to brush algorithm problems, mental labor, the results found such a problem ~
With this problem, you can learn to:
- Use your own language to implement the quick tools available in high-level languages
Title: Seeking 1+2+...+n,
Requirements cannot use multiplication, for, while, if, else, switch, case, and conditional judgment statements (A?). B:C)
I got a go. Cannot use the keyword, that is not the self-waste martial arts??
Calm down ~ ~ Think about what the high-level languages do when we use for and while, and the world there is no for and while.
So, the For And while loop statements are just the loops that the high-level language provides to the user, and we can make a loop for ourselves!
Ideas
Create a struct (class), add one to the number in its constructor,
Then create an array of this struct type of size n,
Will naturally invoke the N-th constructor, and it will implement a 1 plus to n
Here is the code for the constructor of our own design Circulator
Temp () {++n; Sum + = N;}
A very simple code implementation that executes a statement within a block of code each time it is called.
In fact, the equivalent of the for (int i = 1;i<n;i++) {xxx}, xxx and i++ placed in our own design of the Loop constructor.
Source
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include < Vector>using namespace std;/** title: Ask for 1+2+...+n, the request cannot use multiplication method, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C). The idea for And while Loop statement is just a loop that the high-level language provides to the user, and we can make a loop of our own. Method: Create a struct (class), add one to the number in its constructor, and then create an array of the struct type of size n, naturally call the N-th constructor, and implement the 1 plus to N*/class temp{public:temp () {++n; Sum + = N;} static void Reset () {N = 0; Sum = 0; }static int Getsum () {return Sum;} private:static int n;static int sum;};/ /static variable initialization int temp::n = 0;int Temp::sum = 0;int solution1_sum (int N) {temp::reset (); Temp *a = new Temp[n]; New out n arrays. Destroys the object, frees the memory delete []a;a = 0;return temp::getsum ();} void Main () {cout<<solution1_sum (<<endl;system) ("Pause");}
Run Diagram
How, is not this god problem brainwashed, happy Labor Day!
Summarize:
Sometimes we can write our own code to implement high-level language to our packaged tools.
C Language Enhancement (10) Seeking 1+2+...+n | Requirements: Do not use keywords