An warware: yadda za a ce idan lamba ta kasance cikakkiyar murabba'i

Cikakkun murabba'ai suna riƙe ƙima mai mahimmanci a cikin warware matsalar lissafi da algorithms. Suna da mahimmanci, har ma a cikin lissafin yau da kullum da tsarin yanke shawara. A cikin shirye-shirye, sau da yawa buƙatu na tasowa don sanin ko takamaiman lamba daidai ne ko a'a. Ƙayyade wannan da kyau na iya yin ko karya aikin algorithm. A cikin sassan da ke gaba, za mu bincika dabara don tantance ko lamba ta zama cikakkiyar murabba'i a cikin yaren shirye-shiryen C++.

Cikakken Square: Ma'anarsa

Cikakken murabba'i shine sakamakon karkatar da lamba. A wasu kalmomi, idan lamba "n" shine samfurin wani lamba tare da kanta, "n" ana ɗaukarsa cikakkiyar murabba'i. Misali, lambobi 1, 4, 9, 16 cikakkun murabba'ai ne tunda sune murabba'ai na 1, 2, 3, da 4 bi da bi. A cikin sharuddan aiki, idan zaku iya shirya abubuwa daidai a cikin grid murabba'i, to jimlar adadin waɗannan abubuwa cikakke murabba'i ne.

Ƙaddamar da Cikakken Square a C++

Yanzu, bari mu magance yadda za mu iya tantance ko lamba cikakke ce ta amfani da C++. Akwai hanyoyi da yawa don cimma wannan, amma za mu mai da hankali kan hanya madaidaiciya, mai sauƙi kuma mai inganci ta amfani da C++ Standard Template Library (STL).

Ga lambar mafita tsakanin

[lamba]
#include
bool isPerfectSquare (int lamba) {
tushen int = sqrt (lambar);
koma lamba = tushen * tushen;
}

A cikin snippet lambar da ke sama, muna amfani da ɗakin karatu na cmath don amfani da aikin sqrt, wanda ke ba da tushen murabba'in lamba. Mun ayyana aikin mai suna "isPerfectSquare," wanda zai ɗauki lamba a matsayin shigarwa kuma ya dawo da ƙimar boolean mai nuna ko lambar cikakkiyar murabba'i ce.

Bayanin Code

Bari mu zurfafa cikin wannan snippet mai sauƙi amma mai ƙarfi, mataki-mataki.

Da farko, mun haɗa da ɗakin karatu na cmath, don haka za mu iya amfani da aikin sqrt. Ayyukan sqrt ɗaya ne daga cikin ayyukan ginannen C++, kuma yana dawo da tushen murabba'in lambar da aka bayar.

  • An ayyana aikin "isPerfectSquare", wanda ke ɗaukar lamba azaman shigarwa.
  • Sai mu lissafta tushen murabba'in na lambar shigarwa ta amfani da aikin sqrt kuma mu adana sakamakon a cikin ma'auni mai suna "tushen".
  • Muhimmin mataki anan shine kwatanta murabba'in "tushen" tare da lambar shigar farko. Idan sun kasance iri ɗaya, to tabbas lambar ta zama cikakkiyar murabba'i, don haka aikin zai dawo gaskiya, in ba haka ba ƙarya.

Laburaren gama-gari don Ayyukan Lissafi

C++ yana ba da ɗimbin ɗakunan karatu masu amfani waɗanda ke taimakawa wajen lissafin lissafi. Laburaren da aka saba amfani da shi shine cmat, wanda muka yi amfani da shi a cikin wannan matsala don kiran aikin sqrt. Bugu da ƙari, sauran ɗakunan karatu na lissafi a cikin C++ sun haɗa da algorithm (an yi amfani da shi don ayyuka kamar rarrabawa da bincike), da numeric (yana ba da ayyuka don ayyukan lambobi akan ƙima a cikin kwantena).

Duniyarmu tana cike da murabba'ai, ko ta hanyar nau'in pixels na dijital ko akasin haka. Tare da wannan taƙaitaccen jagorar, yanzu kuna da ikon gano waɗannan cikakkun murabba'ai ta amfani da madaidaicin yare C++. Tare da hannu-kan sarrafa murabba'ai da tushen tushe, kuna shirye don magance ƙarin rikitattun matsalolin lissafi. Murnar coding!

Shafi posts:

Leave a Comment