1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

- 学習記録用 [AtCoder5日目]【APG4b】ABC038/ABC029/ABC010/ABC015/ABC075 

Last updated at Posted at 2024-03-07

学習記録用 A問題を4つ+B1つやりました!

AtCoder始めて5日目です!IT就職を目指す、よわよわ学生のFUKIです。Contenstまだ一回も出たことないから、出てみたいけど、beginner contest思ったより数ない、、!最初にぴったりなコンテストあるかな?



2024/03/07 12:30 現在

ABC038, ABC029, ABC010, ABC015, ABC075

#include <bits/stdc++.h>
using namespace std;

int main(){
  string s;
  int last;
  char ans;
  
  cin >> s;
  last = s.size();
  last--;
  ans = s.at(last);
  
  if(ans=='T'){
    cout << "YES" << endl;
  }else{
    cout << "NO" << endl;
  }
}

// string s;
//   cin >> s;
//   if(s.back()=='T'){
//     cout << "YES" << endl;
//   }else{
//     cout << "NO" << endl;
//   }

解き方2通り!1つ目は、APG4bの今までの知識でできるやり方で、2つ目はGPTに他のいいやり方教えてもらった方法。別の問題でback()また思い出したい。

#include <bits/stdc++.h>
using namespace std;

int main(){
  string s;
  cin >> s;
  
  cout << s+'s' << endl;
}

特にいうことなし(笑)

#include <bits/stdc++.h>
using namespace std;

int main(){
  string A,B;
  cin >> A >> B;
  
  int A_size = A.size();
  int B_size = B.size();
  
  if(A_size > B_size){
    cout << A << endl;
  }else{
    cout << B << endl;
  }
}

問題文がおもしろかった。for文でcountするやり方もありそう?

#include <bits/stdc++.h>
using namespace std;

int main(){
  string s;
  cin >> s;
  
  cout << s + "pp" << endl;
}
#include <bits/stdc++.h>
using namespace std;

int main (){
  int h,w;
  cin >> h >> w;
  string panel[50];
  //panel行
  for(int i=0; i<h; i++) cin >> panel[i];
  //上,下,右,左 ,右上,右下,左上,左下
  const int dx[8] = {0,0,1,-1,1,1,-1,-1};
  const int dy[8] = {1,-1,0,0, 1,-1,1,-1};
  
  for(int i=0; i<h; i++){
    for(int j=0; j<w; j++){
      if(panel[i][j] == '#') continue;
      //for文を使って、dx/dyの配列にアクセスしたい
      int count = 0;
      //8方向チェックper 1panel。
      for(int d=0;d<8;d++){
        //ex.最初はi=0,j=0の左上のpanel。そのpanelから8方向の#をチェック。panel番号を隣接した8つのpanel番号に繰り返し
        //変えてアクセス。
        int ni = i + dy[d];
        int nj = j + dx[d];
        //panel[nj] < wは、単位的に比較がおかしいから×。
        //この条件式の書き方はNG。条件式は左から行われるため、out of rangeにアクセスしようとして、REエラーになる。
        // if(panel[ni][nj]=='#' && 0 <= nj && nj < w && 0 <= ni && ni < h ){
        //   count++;
        // }↓この前にifで(0 > nj or nj >= w) continue;で飛ばしてもOK。
        if(0 <= nj && nj <w && 0 <= ni && ni < h && panel[ni][nj] == '#'){
          count++;
        }
      }
      //1パネル(panel[i][j])に表されている整数を一つずつ文字列にしている。これがないとwrongエラー。
      panel[i][j] = count + '0';
    }
    //1パネル行ずつ出力している。panel[i][j]みたいに、配列内の配列iのj番目って指定しなくてもOK。
    cout << panel[i] << endl;
  }
}

B問題解いてみたけど、やっぱりまだ難しくて解けませんでした。模範解答みて写経して、1時間後にもう一度やってみました!(答えチラ見はした^_~)

  • 二次元配列を用いて周りにアクセスする方法を知れてよかった
  • 文字リテラルで'0'を足すことで優勢(顕性)のstring型にできる
  • if(...)continue;のような、一行でfor/if文を書いたことなかったのと、continueを使ったことなかったので助かる
  • int型のint 配列名[要素数]={...}書いたことなかったから、次どっかで使いたい!

最初解いてみたときの回答がどっかいったのことが残念!提出したら回答消えないで

また明日も続けたい



追記:LINEヤフーのYoutubeとサイトに載せられているアルゴリズムの問題が、今日やったマインスイーパーとちょっと似ていて、嬉しくなりました。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?