Table of Contents
Example
The method used in the program below is as follows
Output
Home Backend Development C++ In C++, translate the following into Chinese: Find the rectangle with the smallest difference between length and width

In C++, translate the following into Chinese: Find the rectangle with the smallest difference between length and width

Sep 16, 2023 pm 01:21 PM
programming length Look for width

In C++, translate the following into Chinese: Find the rectangle with the smallest difference between length and width

Given a rectangular area as input. The goal is to find the sides of the rectangle that minimize the difference between length and width.

The area of ​​the rectangle = length * width.

Example

Input− Area = 100

Output− The rectangular side with the smallest difference:

Length = 10, width = 10

Explanation− Side of area = 100.

2-50, 4-25, 5-20, 10-10. The side with the smallest difference is 10-10, Difference = 0. As we all know, a square is a rectangle with all sides equal in length.

Input− Area = 254

Output− Sides of rectangle with minimum difference:

Length = 127, Width = 2

Explanation - The only possible minimum difference of sides to make a rectangle with area 254 is 127 and 2.

The method used in the program below is as follows

Here we will find the square root value of the area and traverse from there to 1 in order to find the value with the smallest difference, and area=input area.

  • Take the integer variable Area as input.

    li>
  • The function fragmentSides(int area1) accepts area1 and prints the side lengths of the rectangle, with the difference between length and width as small as possible.

  • Get integer length, width, tmp1.

  • Set tmp1=ceil(sqrt(area1))

  • Use for loop to traverse (int i = tmp1;i> 0; i --).

  • If (area1 % i == 0) then set length=area/i and width=i.

  • Use the break statement to stop iteration.

  • Print side length and width.

Example

#include <bits/stdc++.h>
using namespace std;
void rectangleSides(int area1){
   int length, breadth;
   int tmp1 = ceil(sqrt(area1));
   for (int i = tmp1; i > 0; i--) {
      if (area1 % i == 0) {

         length = ceil(area1 / i);
         breadth = i;
         break;
      }
   }
   cout<<"Sides of Rectangle with minimum difference :"<<endl;
   cout << "Length = " << length << ", Breadth = "   << breadth << endl;
}
int main(){
   int Area = 140;
   rectangleSides(Area);
   return 0;
}
Copy after login

Output

If we run the above code it will generate the following output

Sides of Rectangle with minimum difference :
Length = 14, Breadth = 10
Copy after login

The above is the detailed content of In C++, translate the following into Chinese: Find the rectangle with the smallest difference between length and width. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
Remove duplicate values ​​from PHP array using regular expressions Remove duplicate values ​​from PHP array using regular expressions Apr 26, 2024 pm 04:33 PM

How to remove duplicate values ​​from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

Build browser-based applications with Golang Build browser-based applications with Golang Apr 08, 2024 am 09:24 AM

Build browser-based applications with Golang Golang combines with JavaScript to build dynamic front-end experiences. Install Golang: Visit https://golang.org/doc/install. Set up a Golang project: Create a file called main.go. Using GorillaWebToolkit: Add GorillaWebToolkit code to handle HTTP requests. Create HTML template: Create index.html in the templates subdirectory, which is the main template.

Get Go modules quickly and easily with Go Get Get Go modules quickly and easily with Go Get Apr 07, 2024 pm 09:48 PM

Through GoGet, you can quickly and easily obtain Go modules. The steps are as follows: Run in the terminal: goget[module-path], where module-path is the module path. GoGet automatically downloads the module and its dependencies. The location of the installation is specified by the GOPATH environment variable.

Collection of C++ programming puzzles: stimulate thinking and improve programming skills Collection of C++ programming puzzles: stimulate thinking and improve programming skills Jun 01, 2024 pm 10:26 PM

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

See all articles