Acwing C++

756. 蛇形矩阵

Alt

题解:
蛇形矩阵走法:右 -> 下 ->左 ->上
坐标变化:(x2,y2) = (x1,y1) + (dx[d] + dy[d])
d步数变化:d = (d + 1)%4
dx[4],dy[4] 分别用来存放xy偏移量,d初始值为0,在两种情况下会+1:
1)撞墙
2)走过走过的点

Alt

#include "iostream"
using namespace std;
int arr[101][101];
int main(){
    int n,m;
    cin >> n >> m; // n x m

    int dx[4] = {0,1,0,-1} , dy[4] = {1,0,-1,0}; // x,y 按顺序的偏移量
    int x = 0 , y = 0 , d = 0;

    for(int num = 1 ; num <=m*n ; num ++ ){
        arr[x][y] = num;

        int x_next = x + dx[d] , y_next = y + dy[d];
        // 判断是否撞墙? 走过走过的点
        if (x_next<0 || x_next >=n || y_next<0 || y_next>=m || arr[x_next][y_next]){
            d = (d+1)%4;
            x_next = x + dx[d];
            y_next = y + dy[d];
        }
        x = x_next;
        y = y_next;
    }


    for (int i = 0 ; i < n; i++){
        for (int j = 0 ; j < m; j++){
            cout << arr[i][j] << ' ';
        }
        cout << '\n';
    }

    return 0 ;
}

772. 只出现一次的字符

Alt

题解:用字符转int存在数组里面记录出现次数

#include <iostream>
#include <string.h>
using namespace std;

int alp[200];

int main(){
    
    string s;
    cin >> s;
    
    int len = s.size();
    for (int i = 0; i<len; i++){
        alp[(int)(s[i] - 'a')] ++ ;

    }

    for (int i = 0; i<len; i++){
        if(alp[s[i]-'a']==1){
            cout << s[i];
            return 0;
        }
        
    }
    
    cout << "no";
    
}

770. 单词替换

Alt

sstream用法:对定义的字符串进行读入 ssin可以理解为cin

#include<iostream>
#include<sstream>
using namespace std;
int main(){
    string s,a,b;
    getline(cin,s);
    cin >> a >> b;
    
    stringstream ssin(s); 
    string str;
    while(ssin>>str){
        if(str == a) cout << b << ' ';
        else cout << str << ' ';
    }
    
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值