A. Subtractions

本文介绍了一种算法,用于计算给定数对经过特定运算达到特定条件所需的步骤数。输入包含多个正整数对,输出为每对数字完成运算所需的总操作次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You’ve got two numbers. As long as they are both larger than zero, they go through the same operation: subtract the lesser number from the larger one. If they equal substract one number from the another. For example, one operation transforms pair (4,17) to pair (4,13), it transforms (5,5) to (0,5).

You’ve got some number of pairs (ai, bi). How many operations will be performed for each of them?

Input
The first line contains the number of pairs n (1  ≤  n  ≤  1000). Then follow n lines, each line contains a pair of positive integers ai, bi (1  ≤  ai,  bi  ≤  109).

Output
Print the sought number of operations for each pair on a single line.

Examples
input
2
4 17
7 987654321

output
8
141093479

My Answer Code:

/*
	Author:Albert Tesla Wizard
	Time:2021/3/12 18:00
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b,ans=0;
        cin>>a>>b;
        int Min=min(a,b);
        int Max=max(a,b);
        while(Max%Min!=0)
        {
            ans+=Max/Min;
            int temp=Min;
            Min=Max%Min;
            Max=temp;
        }
        ans+=Max/Min;
        cout<<ans<<'\n';
    }
    return 0;
}

Now, we have a way to perceive that the user is pressing a key. We know when we want to move up, down, left, and right. We know at each iteration of the main loop exactly, what the user wants; we just have to update the circle with a new position depending on this input. This method gives us a great advantage. So finally we can write something in our update() function, namely, the movement of our player. We check which of the four Boolean member variables is true, and determine the movement accordingly. By using += (instead of =) and if (instead of else if), we implicitly handle the case where two opposite keys, such as right and left are pressed at the same time&mdash;the movement stays zero. The update() function is shown in the following code snippet: void Game::update() { sf::Vector2f movement(0.f, 0.f); if (mIsMovingUp) movement.y -= 1.f; if (mIsMovingDown) movement.y += 1.f; if (mIsMovingLeft) movement.x -= 1.f; if (mIsMovingRight) movement.x += 1.f; mPlayer.move(movement); } We introduce two new things here: a vector and the move() function on the circle shape. The move() function does what its name says, it moves the shape by the amount we provide it. Vector algebra Vectors are an important part of algebraic mathematics. They imply lots of rules and definitions, which go beyond the scope of our book. However, SFML's sf::Vector2 class template is way more practical, both in concept and functionality. To be as simple as we could possibly be, we know that a coordinate in a two-dimensional Cartesian system would need two components: x and y. Because in graphics all coordinates are expressed with the decimal float data type, sf::Vector2 is instantiated as sf::Vector2<float>, which conveniently has a typedef named sf::Vector2f. Such an object is made to contain two member variables, x and y. This makes our life simpler, because now we don't need to pass two variables to functions, as we can fit both in a single sf::Vector2f object. sf::Vector2f also defines common vector operations, such as additions and subtractions with other vectors, or multiplications and divisions with scalars (single values), effectively shortening our code.翻译,要求每行中英文对照
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值