寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #C - Buy an Integer

AtCoder Beginner Contestの過去問から、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#C - Buy an Integer

atcoder.jp

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#C - Buy an Integer
https://atcoder.jp/contests/abc146/tasks/abc146_c
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll a,b,x,m=1e9,y,ans,w;
ll f(ll num){
  ll cnt=0;
  while(num){
    cnt++;
    num/=10;
  }
  return cnt;
}
int main(void){
  cin>>a>>b>>x;
  if((x/a)*a==x){
    w=f(x/a)-1;
  }else{
    w=f(x/a);
  }
  y=x-b*w;
  if(y>0){
    ans=y/a;
  }else{
    while(y<=0){
      w--;
      y=x-b*w;
    }
    ans=y/a;
  }
  cout<<min(m,ans)<<endl;
  return 0;
}

AtCoder Beginner Contestは、オンラインジャッジによるプログラミングコンテストです。
日本語と英語に対応していて、週末ごとに実施されているみたいです。
https://practice.contest.atcoder.jp/tutorial
アカウントを登録すれば、誰でも参加できます。