2016华为上机题一(循环移位)

1.循环数组移位
输入10个整整数的数组,在输入一个正整数m,将数组的后m位进行循环移位。计算移位后的前m个数和后m个数的和。
1 2 3 4 5 6 7 8 9 10
3
8 9 10 1 2 3 4 5 6 7
27 18
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cmath>
 4 #include <string>
 5 #include <algorithm>
 6 #include <memory.h>
 7 
 8 using namespace std;
 9 
10 const double PI = acos(-1.0);
11 const double eps = 1e-6;
12 const double INF = 1e9 + 7;
13 
14 
15 
16 int a[10];
17 void exchange(int a[], int begin, int end)
18 {
19     int temp;
20     while (begin<end)
21     {
22         temp = a[begin];
23         a[begin] = a[end];
24         a[end] = temp;
25         begin++;
26         end--;
27     }
28 
29 }
30 
31 int main()
32 {
33     freopen("data.in", "r", stdin);
34     
35     int m;
36     memset(a, 0, 10);
37     for (int i = 0; i < 10; i++)
38     {
39         cin >> a[i];
40     }
41     
42     cin >> m;
43     exchange(a,0,10-m-1);
44     exchange(a, 10-m, 9);
45     exchange(a, 0,9);
46 
47     int beginSum = 0;
48     int endSum = 0;
49     for (int i = 0; i < m; i++)
50     {
51         beginSum += a[i];
52 
53     }
54 
55     for (int j = 9; j >10 - m -1; j--)
56     {
57         endSum += a[j];
58     }
59 
60     for (int i = 0; i < 9; i++)
61     {
62         cout << a[i] << " ";
63     }
64     cout <<a[9] << endl;
65     cout << beginSum << " "<< endSum << endl;
66 
67     system("pause");
68     return 0;
69 }

 

posted @ 2015-09-29 19:56  柯南333  阅读(231)  评论(0编辑  收藏  举报