正方形個數

One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm  ×  b mm sheet of paper (a > b). Usually the first step in making an origami is making a square piece of paper from the rectangular sheet by folding the sheet along the bisector of the right angle, and cutting the excess part.
在這裏插入圖片描述

After making a paper ship from the square piece, Vasya looked on the remaining (a - b) mm  ×  b mm strip of paper. He got the idea to use this strip of paper in the same way to make an origami, and then use the remainder (if it exists) and so on. At the moment when he is left with a square piece of paper, he will make the last ship from it and stop.

Can you determine how many ships Vasya will make during the lesson?

Input
The first line of the input contains two integers a, b (1 ≤ b < a ≤ 1012) — the sizes of the original sheet of paper.

Output
Print a single integer — the number of ships that Vasya will make.

Sample test(s)
Input
2 1
Output
2
Input
10 7
Output
6
Input
1000000000000 1
Output
1000000000000
Note
Pictures to the first and second sample test.
思路:剛開始一直a-=b,結果最後一個例子一直出不來,如果a>b,則就會一直a-=b,總共進行a/b次

#include<bits/stdc++.h>
using namespace std;
int main()
{
   long long  int a,b;
    cin>>a>>b;
    long long int sum=0;
    while(b&&a)
    {
       sum+=a/b;//如果a>b,a-=b,會減少a/b次
       a%=b;
       swap(a,b);//保證a>b
    }
    cout<<sum<<endl;
    return 0;
}
發佈了58 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章