李俊亨_2404020110_网络24_1 level
获赞
3
粉丝
1
关注
1
看过 TA
28
哈尔滨理工大学
2028
C++
IP属地:广东
暂未填写个人简介
私信
关注
2024-12-30 14:42
哈尔滨理工大学 C++
#includeusing namespace std;vector> shun(const vector>&a,int x,int y, int r){    int n = a.size();    vector> result = a;    for(int i = -r;i        for(int j = -r;j            result[x+j][y-i] = a[x+i][y+j];        }    }    return result;}vector> ni(const vector>&a,int x,int y, int r){    int n = a.size();    vector> result = a;    for(int i = -r;i        for(int j = -r;j            result[x-j][y+i] = a[x+i][y+j];        }    }    return result;}int main(){    int n,m,num=1; cin>>n>>m;    vector>a(n,vector(n));    for(int i = 0;i        for(int j = 0;j            a[i][j]=num++;        }    }    for(int i = 0;i        int x, y, r, z; cin>>x>>y>>r>>z;        x--;y--;        if(z == 0){            a = shun(a,x,y,r);        }else{            a = ni(a,x,y,r);        }    }    for(int i = 0;i        for(int j = 0;j            cout        }        cout    }    return 0;}
0 点赞 评论 收藏
分享
2024-12-30 14:39
哈尔滨理工大学 C++
#include #include #include   // 用于排序using namespace std;// 定义学生结构体struct Student {    string name;       // 姓名    int programming;   // 编程成绩    int math;          // 数学成绩    int english;       // 英语成绩    // 计算总分    int totalScore() const {        return programming + math + english;    }};// 自定义排序规则bool compareStudents(const Student &a, const Student &b) {    if (a.totalScore() != b.totalScore()) { return a.totalScore() > b.totalScore();  // 按总分从高到低排序    } else {        return a.name     }}int main() {    int n; cin >> n;  // 读取学生数量    // 定义学生数组    Student students[n];    // 读取每个学生的信息    for (int i = 0; i  cin >> students[i].name >> students[i].programming >> students[i].math >> students[i].english;    }    // 对学生数组进行排序    sort(students, students + n, compareStudents);    // 输出排序后的学生姓名和总分    for (int i = 0; i         cout     }    return 0;}
0 点赞 评论 收藏
分享
2024-12-30 14:38
哈尔滨理工大学 C++
#include using namespace std;// 定义链表结点结构体struct ListNode {    int data;           // 数据    ListNode* next;     // 指向下一个结点的指针};int main() {    int n; cin >> n;  // 读取数据个数    // 创建头结点    ListNode* head = new ListNode(); head->next = nullptr;    // 当前结点指针,用于插入新结点    ListNode* current = head;    // 建立链表    for (int i = 0; i         int data; cin >> data;  // 读取数据        // 创建新结点        ListNode* newNode = new ListNode(); newNode->data = data; newNode->next = nullptr;        // 将新结点插入到链表尾部 current->next = newNode;        current = newNode;    }    int m; cin >> m;  // 读取欲删除的数据    // 删除值为 m 的结点    ListNode* prev = head;  // 前驱结点 ListNode* curr = head->next;  // 当前结点    while (curr != nullptr) { if (curr->data == m) {            // 删除当前结点 prev->next = curr->next;            delete curr;  // 释放内存 curr = prev->next;  // 继续检查下一个结点        } else {            // 移动到下一个结点            prev = curr; curr = curr->next;        }    }    // 计算删除后的链表长度    int length = 0; curr = head->next;    while (curr != nullptr) {        length++; curr = curr->next;    }    // 输出删除后的链表长度    cout     // 输出删除后的链表数据 curr = head->next;    while (curr != nullptr) {        cout data; if (curr->next != nullptr) {            cout         } curr = curr->next;    }    cout     // 释放链表内存    curr = head;    while (curr != nullptr) {        ListNode* temp = curr; curr = curr->next;        delete temp;    }    return 0;}
0 点赞 评论 收藏
分享
2024-12-30 14:37
哈尔滨理工大学 C++
#include #include using namespace std;struct Student {    int id;                 string name;            int score;              Student* next;     };int main() {    Student* head = nullptr;      Student* tail = nullptr;     while (true) {        int id;        string name;        int score; cin >> id;        if (id == 0) {            break;        } cin >> name >> score;          Student* newStudent = new Student{id, name, score, nullptr};        if (head == nullptr) {            head = newStudent;            tail = newStudent;        } else { tail->next = newStudent;            tail = newStudent;        }    }    Student* current = head;    while (current != nullptr) {        cout id name score current = current->next;    }    current = head;    while (current != nullptr) {        Student* temp = current; current = current->next;        delete temp;    }    return 0;}
0 点赞 评论 收藏
分享
2024-12-30 14:36
哈尔滨理工大学 C++
#include "bits/stdc++.h"using namespace std;int win[5][5] = {        {0, -1, 1, 1, -1},        {1, 0, -1, 1, -1},        {-1, 1, 0, -1, 1},        {-1, -1, 1, 0, 1},        {1, 1, -1, -1, 0}};int main(){    int n,a,b; cin>>n>>a>>b;    vector A(a);    vector B(b);    for(int i = 0;i cin>>A[i];    }    for(int i = 0;i cin>>B[i];    }    int countA = 0,countB = 0;    for(int i = 0;i        int a1 = A[i%a] ,b1 = B[i%b];        if (win[a1][b1] == 1) {            countA++;        } else if (win[a1][b1] == -1) {            countB++;        }    }    cout    return 0;}
0 点赞 评论 收藏
分享
2024-12-30 14:35
哈尔滨理工大学 C++
#include using namespace std;char mp[11][11];int xa,ya,xb,yb;int lxa,lya,lxb,lyb;int fa = 1,fb = 1;int ans = 0;void yda(){    if(fa==1){//北        lxa = xa - 1;        lya = ya;    }    if(fa==2){//东        lxa = xa;        lya = ya + 1;    }    if(fa==3){//南        lxa = xa+1;        lya = ya ;    }    if(fa==4){//西        lxa = xa;        lya = ya - 1;    } if(lxa>=1 && lya >=1 && lya        xa = lxa;        ya = lya;    }else{        fa++;//方向改变 if(fa>4){            fa = 1;        }    }}void ydb(){    //平移区    if ( fb == 1 ){        lxb = xb - 1 ;        lyb = yb ;    }    if ( fb == 2 ){        lxb = xb ;        lyb = yb + 1 ;    }    if ( fb == 3 ){        lxb = xb + 1 ;        lyb = yb ;    }    if ( fb == 4 ){        lxb = xb ;        lyb = yb - 1 ;    }    //判定区: if ( lxb >= 1 && lyb >= 1 && lyb         xb = lxb ;        yb = lyb ;    }    else{        fb++ ; if ( fb > 4 ){            fb = 1 ;        }    }}int main(){    for(int i = 1;i        for(int j = 1;j cin >> mp[i][j];            if(mp[i][j]=='F'){                xa=i;                ya=j;                mp[i][j] = '.';            }            if(mp[i][j]=='C'){                xb=i;                yb=j;                mp[i][j] = '.';            }        }    }    while(1){        yda();        ydb();        ans++; if(ans>1000){            cout            return 0;        }        if(xa==xb&&ya==yb){            cout            return 0;        }    }}
0 点赞 评论 收藏
分享
2024-12-30 14:34
哈尔滨理工大学 C++
#includeusing namespace std;class shape{protected:    int x,y;    shape(int x = 0,int y = 0):x(x),y(y){}    virtual ~shape(){}    virtual double GetArea() = 0;// 纯虚函数,用于计算面积};class Rectangle : public shape{protected:    int Length,Width;public:    Rectangle(int Length,int Width):Length(Length),Width(Width){}    double GetArea() override{//表示该函数是重写基类的虚函数        return Length*Width;    }};class Circle : public shape{protected:    int radius;public:    Circle(int radius):radius(radius){}    double GetArea() override{        return 3.14*radius*radius;    }};class Square : public Rectangle{public:    Square(int side) : Rectangle(side,side){}};int main(){    int Length,Width,radius,side; cin>>Length>>Width;    Rectangle a(Length,Width);    cout cin>>radius;    Circle b(radius);    cout cin>>side;    Square c(side);    cout    return 0;}
0 点赞 评论 收藏
分享
2024-12-30 14:34
哈尔滨理工大学 C++
#include "bits/stdc++.h"using namespace std;enum CPU_Rank{P1 = 1,P2,P3,P4,P5,P6,P7};class CPU {public:    CPU(CPU_Rank rank,int frequency,float voltnumber)         : rank(rank),frequency(frequency),voltnumber(voltnumber){//初始化列表        cout    }    ~CPU() {//析构函数 清理对象资源,销毁对象        cout    }    void run(){        cout                    }    void stop(){        cout     }private://私有成员    CPU_Rank rank;    int frequency;    float voltnumber;};int main(){    int rank, frequency;    float voltnumber; cin >> rank >> frequency >> voltnumber;    CPU cpu((CPU_Rank)rank,frequency,voltnumber);// 将整数转换为 CPU_Rank 类型    cpu.run();    cpu.stop();    // 对象销毁时,析构函数自动调用    return 0;}
0 点赞 评论 收藏
分享
2024-12-09 17:21
哈尔滨理工大学 C++
#includeusing namespace std;vector> shun(const vector>&a,int x,int y, int r){    int n = a.size();    vector> result = a;    for(int i = -r;i        for(int j = -r;j            result[x+j][y-i] = a[x+i][y+j];        }    }    return result;}vector> ni(const vector>&a,int x,int y, int r){    int n = a.size();    vector> result = a;    for(int i = -r;i        for(int j = -r;j            result[x-j][y+i] = a[x+i][y+j];        }    }    return result;}int main(){    int n,m,num=1; cin>>n>>m;    vector>a(n,vector(n));    for(int i = 0;i        for(int j = 0;j            a[i][j]=num++;        }    }    for(int i = 0;i        int x, y, r, z; cin>>x>>y>>r>>z;        x--;y--;        if(z == 0){            a = shun(a,x,y,r);        }else{            a = ni(a,x,y,r);        }    }    for(int i = 0;i        for(int j = 0;j            cout        }        cout    }    return 0;}二维数组中,顺时针旋转(x,y)=(y,-x),逆时针旋转(x,y)=(-y,x)
0 点赞 评论 收藏
分享
2024-12-05 18:36
哈尔滨理工大学 C++
在C++中,如果你需要快速获取矩阵中一个位置(如(i, j))四周的值,可以使用一个方向数组(或方向向量)来简化代码。方向数组定义了目标点的上下左右相对偏移量,配合循环可以避免重复代码。以下是一个示例代码:#include #include using namespace std;void getNeighbors(const vector>& matrix, int x, int y) {    // 定义方向数组:上、下、左、右    int dx[4] = {-1, 1, 0, 0};    int dy[4] = {0, 0, -1, 1};    int rows = matrix.size();    int cols = matrix[0].size();    cout     for (int k = 0; k         int nx = x + dx[k];        int ny = y + dy[k];        // 检查边界条件 if (nx >= 0 && nx = 0 && ny             cout         }    }    cout }int main() {    vector> matrix = {        {1, 2, 3},        {4, 5, 6},        {7, 8, 9}    };    int x = 1, y = 1; // 中心位置    getNeighbors(matrix, x, y);    return 0;}运行结果对于matrix矩阵:1 2 34 5 67 8 9如果查询(1, 1)位置的四周,输出结果为:Position (1, 1) neighbors: 2 8 4 6代码说明方向数组:dx和dy表示相对偏移量,dx表示行的变化,dy表示列的变化。组合后可以生成上下左右的位置:上:(x - 1, y)下:(x + 1, y)左:(x, y - 1)右:(x, y + 1)边界检查:通过nx >= 0 && nx = 0 && ny 代码通用性:这种方法适用于任意大小的矩阵。如果需要扩展到八个方向(包括对角线),可以扩展方向数组为:这种方式不仅代码简洁,而且易于扩展。作者:徐子昂_2404020114_网络24_1链接:https://www.nowcoder.com/discuss/694188703584968704?sourceSSR=users来源:牛客网
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客企业服务