Data Structure and algorithm 1

Source: Internet
Author: User

Question 1: 1! + 2! + ..... + N! The last 6 digits of the sum (note the range of N)

#include <iostream>using namespace std;const int MAX = 1000000;int getResu(int n){int sum=0;int temp= 1;for(int i=1; i <= n; i++){temp *= i;temp %= MAX;sum += temp;sum %= MAX;}return sum ;}int main(){int n;while(cin>>n){if(n > 0)cout << getResu(n) << endl;elsecout << "Please input a integer greater than zero !" <<endl;}return 0;}

Question 2: In a sequence with a length of N, calculate the maximum and minimum values and output them. Finally, analyze possible optimizations.

# Include <iostream> # include <cstdlib> // set the random function # include <ctime> using namespace STD; const int max_count = 10000; const int max_value = 1000; int number [max_count]; void Init () {// 0-1000for (INT I = 1; I <= max_count; I ++) number [I] = rand () % max_value; for (INT I = 1; I <= max_count; I ++) cout <number [I] <""; cout <Endl;} void solve () {int Max, Min, flag; If (flag = max_count % 2) // assuming true, 1max = min = number [1]; if else {// is an even number, if (number [1]> Number [2]) {max = number [1]; min = number [2];} else {max = number [2]; min = number [1] ;}} if (FLAG) Flag = 2; else flag = 3; for (INT I = flag; I <max_count; I + = 2) {If (number [I]> Number [I + 1]) {If (number [I]> MAX) max = number [I]; If (number [I + 1] <min) min = number [I + 1];} else {If (number [I + 1]> MAX) max = number [I + 1]; If (number [I] <min) min = number [I];} cout <"max =" <max <Endl; cout <"min =" <min <Endl;} int main () {// seed srand (Time (null) of the random function; Init (); // initialize solve (); // return 0 ;}


The method for finding the maximum and minimum values in N numbers is obvious. We only need to find the minimum and maximum values independently. The n-1 ratio is required for each of them. Assuming that the first maximum and minimum values must be 2N-2 ratio. In fact, it only requires a maximum of 3 (n/2) times than the limit. The detailed method is to record the known maximum and minimum values, but not compare each input element with the current maximum and minimum values ----- because the cost of doing so is that each element needs to be compared twice. Each element and maximum ratio is one time. Minimum value once. Instead, compare the input elements. First, compare the input elements with each other, then compare the smaller value with the current minimum value, and compare the larger value with the current maximum value. In this way, three percentages are required for each two elements, which is less than the four percentages described above. How to set the known minimum and maximum values depends on whether N is an odd or even number. If n is an odd number, we will calculate the minimum and maximum values.
Set as the first number. If n is an even number, compare the first number with the second number. A smaller value is that the current minimum value is the current maximum value, the following number is a paired ratio, which is smaller than the current minimum ratio, larger than the current maximum ratio. Finally, we will analyze the total number of times of ratio, assuming N is an odd number, then a total of 3 (n/2) times of ratio, assuming N is an even number, it is 3 (n-2) /2 + 1 ratio, up to 3 (n/2 ).

Question 3: Sorting


I also had a blog: http://blog.csdn.net/china_zoujinyong/article/details/17588897.


Question 4: Steps

Problem description: The Stairs from the first floor to the second floor of the dining room in North District of our school have 17 steps. If you are a normal person, you can choose one step and two steps at a time, or three steps: How many steps do you have to go from the bottom to the bottom?


Solution 1:

The method of n steps is equal to the sum of n-1 steps, N-2 steps and n-3 steps.

Steps

  1. 11

  2. 22 1 + 1

  3. 43 1 + 2 1 + 1 + 1 2 + 1

  4. 71 + 3 1 + 2 + 1 + 1 + 1 + 1 1 + 1 + 1 + 2 2 + 1 2 + 2 3 + 1

  5. 131 + 1 + 1 + 1 + 1 + 2 + 2 2 + 1 + 2 1 + 2 + 1 + 1 + 1 + 1 + 3 3 + 1 + 1 1 + 3 + 1

1 + 2 + 1 + 1 + 1 + 2 + 1 2 + 1 + 1 + 1 + 1 + 1 + 1 + 2 2 + 3 + 2

6 24 1 + 1 + 1 + 1 + 1 + 1 2 + 1 + 1 + 1 + 1 + 1 (5) 3 + 1 + 1 + 1 (4)

2 + 2 + 1 + 1 (6) 2 + 3 + 1 (6) 3 + 3 2 + 2 + 2




#include <iostream>using namespace std;int recu(int n){  if(n==1) return 1;  if(n==2) return 2;  if(n==3) return 4;  return recu(n-1)+recu(n-2)+recu(n-3);}int main(){  int n;  cin >> n;  cout << recu(n) << endl;  return 0;}


Method 2: Set a container and place something (, 3,) in it. If the number value in the container is equal to the value of the given n, the condition is met, save as a result.


#include <iostream>using namespace std;int n, resu=0;int number[3]={1,2,3};void recu(int count){  if(count==n)    {      resu++;      return ;    }  if(count > n)    return ;  for(int i=0; i < 3; i++)    {      count += number[i];      recu(count);      count -= number[i];    }}int main(){  cin >> n;  recu(0);  cout << resu << endl;  return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.