检查我的代码,我认为它很复杂。请提出一些建议使其变得简单

时间:2018-11-22 12:13:39

标签: c++

//编写一个程序,该程序将10个学生的id放在他们坐在椅子上的数组中。用户将告诉您他/她想移动多少个学生,但最后每个学生都必须以圆周运动坐在各自的椅子上。     样本输入:1 2 3 4 5 6 7 8 9 10     迁移学生数:4     样本输出:7 8 9 10 1 2 3 4 5 6

    #include <iostream>
using namespace std;
#define SIZE 10
int main()
{
    int id[SIZE], move = 0, result[SIZE]; 
    for(int index = 0; index < SIZE; index++){
        id[index] = 0;
    }
    for(int index = 0; index < SIZE; index++){
        cout << "Enter the id of student at index " << index << ": " ;
        cin >> id[index];
    }
    cout << "Enter number of students to move: ";
    cin >> move;
    while(move > 9){
        cout << "Enter number of students to move between 0 to 10: ";
        cin >> move;
    }
    switch(move){
        case 1:
        result[0] = id[9];
        for(int index = 1; index < SIZE; index++){
            result[index] = id[index-1];
        }
        break;
        case 2:
        for(int index = 0; index < 2; index++){
            result[index] = id[index+8];
        }
        for(int index = 2; index < SIZE; index++){
            result[index] = id[index-2];
        }
        break;
        case 3:
        for(int index = 0; index < 3; index++){
            result[index] = id[index+7];
        }
        for(int index = 3; index < SIZE; index++){
            result[index] = id[index-3];
        }
        break;
        case 4:
        for(int index = 0; index < 4; index++){
            result[index] = id[index+6];
        }
        for(int index = 4; index < SIZE; index++){
            result[index] = id[index-4];
        }
        break;
        case 5:
        for(int index = 0; index < 5; index++){
            result[index] = id[index+5];
        }
        for(int index = 5; index < SIZE; index++){
            result[index] = id[index-5];
        }
        break;
        case 6:
        for(int index = 0; index < 6; index++){
            result[index] = id[index+4];
        }
        for(int index = 6; index < SIZE; index++){
            result[index] = id[index-6];
        }
        break;
        case 7:
        for(int index = 0; index < 7; index++){
            result[index] = id[index+3];
        }
        for(int index = 7; index < SIZE; index++){
            result[index] = id[index-7];
        }
        break;
        case 8:
        for(int index = 0; index < 8; index++){
            result[index] = id[index+2];
        }
        for(int index = 8; index < SIZE; index++){
            result[index] = id[index-8];
        }
        break;
        case 9:
        for(int index = 0; index < 9; index++){
            result[index] = id[index+1];
        }
        result[9] = id[0];
        break;
        default:
        for(int index = 0; index < SIZE; index++){
            result[index] = id[index];
        }
    }
    for(int index = 0; index < SIZE; index++){
        cout << result[index] << " "; 
    }
}

4 个答案:

答案 0 :(得分:1)

您可以看到,在switch(move)中,您基本上有:

case N:
    for(int index = 0; index < N; index++){
        result[index] = id[index + SIZE - N];
    }
    for(int index = N; index < SIZE; index++){
        result[index] = id[index - N];
    }

您可以将整个switch (move) { /* all the cases */ }替换为:

for(int index = 0; index < move; index++){
    result[index] = id[index + SIZE - move];
}
for(int index = move; index < SIZE; index++){
    result[index] = id[index - move];
}

通常,您应该始终尝试在重复的代码中识别出这种模式。

答案 1 :(得分:0)

我建议使用双端队列,最好将程序划分为一些功能:

#include <deque>
#include <iostream>
using namespace std;
const int SIZE = 10;

std::deque<int> input_students()
{
    deque<int> students;
    for(int index = 0; index < SIZE; index++)
    {
        cout << "Enter the id of student at index " << index << ": ";
        int v = 0;
        cin >> v;
        students.push_back(v);
    }
    return students;
}

unsigned int get_move()
{
    cout << "Enter number of students to move: ";
    unsigned int move = 0;
    do
    {
        cout << "Enter number of students to move between 0 to 10: ";
        cin >> move;
    }while(move > 9);
    return move;
}

void offset_students(deque<int> &students, unsigned int move)
{
    for(auto i = 0U;i < move;i++)
    {
        students.push_front(students.back());
        students.pop_back();
    }
}

int main()
{
    deque<int> students = input_students();
    auto move = get_move();
    offset_students(students, move);

    for(auto s : students)
        cout << s;
}

答案 2 :(得分:0)

使用c样式数组的另一种方法是使用std::vector(或类似的容器)并使用STL算法std::rotate

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> ids;

    int index=0;
    do {
        int id;
        std::cout << "Enter the id of a student at index " << index++ << " (0 to quit):  ";
        std::cin >> id;
        if(id<1) break;
        ids.push_back(id);
    } while( true );

    size_t move;
    do {
        std::cout << "Enter number of students to move [0," << ids.size() << "]: ";
        std::cin >> move;
    } while( move>ids.size() );

    std::rotate(ids.begin(), ids.begin()+move, ids.end());

    for(int id : ids) {
        std::cout << id << " ";
    }
    std::cout << "\n";
}

答案 3 :(得分:0)

写下两次输入数组索引。

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

从重复的视图看,结果会产生一些变化(|之间的位是结果)

    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1:                   |<-               ->|
2:                 |<-               ->|
3:               |<-               ->|

在结果中写下相应的索引。
转换3个位置的示例:

    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
3:               |<-               ->|
Index in result:  0 1 2 3 4 5 6 7 8 9

一段时间后,您可能会注意到输入中的每个元素i对应于结果中的元素(i + move) % 10

因此,您可以使用以下循环替换整个switch

for (int i = 0; i < SIZE; i++) {
    result[(i + move) % SIZE] = id[i];
}