Haqiqiy sonlar :: Yechimlar
Masala A
Kiritilgan haqiqiy songa eng yaqin butun sonni topadigan dastur tuzing.
\(-100.000\) dan \(100.000\) gacha oraliqdagi har qanday son
Kiritilgan songa eng yaqin butun son.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <itboom.h>
float roundHalfToEven(float f)
{
const float r = round(f);
const float d = r - f;
if ((d != 0.5f) && (d != -0.5f)) {
return r;
}
if (fmod(r, 2.0f) == 0.0f) {
return r;
}
return f - d;
}
int main() {
float f;
scanf("%f", &f);
printf("%d", (int)roundHalfToEven(f));
return 0;
}
import math
import itboom
def main():
n = float(input())
print(round(n))
if __name__ == '__main__':
main()
Masala B
1 sonini \(10^n\) soniga bo‘lgandagi qiymatini hisoblaydigan dastur tuzing.
\(n\) ning qiymati 0 dan 100 gacha.
\(\frac{1}{10^n}\) ifodaning qiymati
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <itboom.h>
int main() {
int n;
scanf("%d", &n);
if (n == 0) {
printf("1");
return 0;
}
printf("0.");
for(int i = 0; i < n - 1; i++) {
printf("0");
}
printf("1");
return 0;
}
import math
import itboom
def main():
n = int(input())
if n == 0:
print(1)
else:
print("0." + "0" * (n - 1) + "1")
if __name__ == '__main__':
main()
Masala C
Kiritilgan son haqiqiy son ekanligini tekshiradigan dastur tuzing.
Birinchi qatorda belgilar soni (1 dan 100 gacha), ikkinchi qatorda raqamlar va nuqtadan iborat, uzunligi berilgan belgilar soniga teng matn
Agar haqiqiy son bo’lsa YES
, aks holda NO
so’zi
#include <stdio.h>
#include <itboom.h>
int main() {
int n, k = 0, m = 0;
scanf("%d\n", &n);
for(int i = 0; i < n; i++) {
char c;
scanf("%c", &c);
if (c == '.') {
k ++;
} else {
m ++;
}
}
if (k == 1 && m > 0) {
printf("YES");
} else {
printf("NO");
}
return 0;
}
import math
import itboom
def main():
# raqamlar soni bizga kerak emas
input()
n = input()
if len(n) > 1 and n.count(".") == 1:
print("YES")
else:
print("NO")
if __name__ == '__main__':
main()
Masala D
Kiritilgan haqiqiy soning butun va kasr qismini alohida ajratib, qatorma-qator ekranga chiqaradigan dastur tuzing.
\(-100.000\) dan \(100.000\) gacha oraliqdagi har qanday son.
Sonning butun va kasr qismi
#include <stdio.h>
#include <itboom.h>
int main() {
double f, a, b;
scanf("%lf", &f);
a = modf(f, &b);
if (b == 0 && a < 0) {
//Shartda ishorasi bir xil bo'lsin deyilgan
//Hatto 0 bo'lsa ham
printf("-");
}
printf("%d\n%g", (int)b, a);
if (a == 0.0f) {
//Shartda 0 emas, 0.0 chiqarish kerak
//C da %g 0.0 da faqat 0 chiqaradi
printf(".0");
}
return 0;
}
import math
import itboom
def main():
n = str(float(input())) # 0 larni olib tashlaymiz
if "." not in n:
n += ".0"
a, b = n.split(".")
print(a)
if a.startswith("-"):
print("-", end="")
print("0." + b)
if __name__ == '__main__':
main()
Masala E
Ketma-ket ikkita belgini kiritib olamiz va teskari tartibda ekranga chiqaramiz
Kiritilgan ikki xonali sonni teskarisini chiqaradigan dastur tuzing.
Ikki xonali son
Kiritilgan sonning teskarisi
,>,.<.