Kasr sonlar :: Yechimlar
Masala A
Berilgan ikkita kasr sonni qo‘shadigan dastur tuzing.
Har bir qatorda a/b ko‘rinishidagi kasr son. a ning qiymat -100 dan 100 gacha, b ning qiymat 2 da 100 gacha.
Berilgan ikkita kasr sonning yig‘indisi
#include <stdio.h>
#include <itboom.h>
int main() {
int a, b, c, d;
scanf("%d/%d", &a, &b);
scanf("%d/%d", &c, &d);
int num = a * d + b * c;
int den = b * d;
//qisqarmas karga o'tkazish uchun EKUB ga bo'lamiz
int k = gcd(num, den);
printf("%d/%d", num / k, den / k);
return 0;
}
import math
from fractions import Fraction
def main():
a = Fraction(input())
b = Fraction(input())
c = a + b
# shartda 1 emas, 1/1 qabul qilinadi deyilgan
print(f"{c.numerator}/{c.denominator}")
if __name__ == '__main__':
main()
Masala B
Kiritilgan kasr sonni o‘nli kasr (\(\frac{a}{10^n}\)) ko‘rinishida ifodalash mumkin bo‘lsa, shu kasr sonni, aks holda NO
so‘zini ekranga chiqaradigan dastur tuzing.
Birinchi qatorda a/b
ko‘rinishidagi kasr son. Bu yerda a ning qiymat \(-10^5\) dan \(10^5\) gacha, b ning qiymat \(2\) dan \(10^{8}\) gacha
Masala shartidagi qiymatlar
#include <stdio.h>
#include <itboom.h>
int main() {
long a, b;
scanf("%lld/%lld", &a, &b);
long n = b;
while(n > 1) {
if (n % 2 == 0) {
a *= 5;
b *= 5;
n /= 2;
} else if (n % 5 == 0) {
a *= 2;
b *= 2;
n /= 5;
} else {
printf("NO");
return 0;
}
}
printf("%lld/%lld", a, b);
return 0;
}
import math
import itboom
def main():
a, b = map(int, input().split("/"))
n = b
while n > 1:
if n % 2 == 0:
a, b, n = a * 5, b * 5, n // 2
elif n % 5 == 0:
a, b, n = a * 2, b * 2, n // 5
else:
print("NO", end="")
return
print(f"{a}/{b}")
if __name__ == '__main__':
main()
Masala C
Kiritilgan songa teskari sonni aniqlaydigan dastur tuzing.
Birinchi qatorda a/b
ko‘rinishidagi kasr son. a
ning qiymati \(-10^8\) dan \(10^8\) gacha, b
ning qiymat \(2\) dan \(10^8\) gacha.
Kiritilgan songa teskar soni
#include <stdio.h>
#include <itboom.h>
int main() {
int a, b;
scanf("%d/%d", &a, &b);
int n = 1;
if (a < 0) {
n = -1;
a = -a;
}
printf("%d/%d", n * b, a);
return 0;
}
import math
import itboom
from fractions import Fraction
def main():
a, b = input().split("/")
if a.startswith("-"):
a = a[1:]
b = "-" + b
print(f"{b}/{a}")
if __name__ == '__main__':
main()
Masala D
Kiritilgan kasr sonni aralash son shaklida ekranga chiqaradigan dastur tuzing.
Birinchi qatorda a/b
ko‘rinishidagi kasr son. Bunda a
soni \(-10^8\) dan \(10^8\) gacha, b
soni \(2\) dan \(10^8\) gacha.
a b/c
ko‘rinishidagi aralash son
#include <stdio.h>
#include <itboom.h>
int main() {
int a, b;
scanf("%d/%d", &a, &b);
int n = a / b;
printf("%d %d/%d", n, a % b, b);
return 0;
}
import math
import itboom
def main():
a, b = map(int, input().split("/"))
n = math.fmod(a, b)
print(f"{int((a - n) / b)} {int(n)}/{b}")
if __name__ == '__main__':
main()
Masala E
Kiritilgan harfni kiritilgan qiymatcha takrorlab ekranga chiqaradigan dastur tuzing
nL
ko‘rinishidagi matn. Bu yerda n
1 dan 9 gacha bo‘lgan son, L
birorta belgi.
L
belgini n
marotaba takrorlab chiqarilgan natija
,>,>>++++++++[<++++++>-]<[<<->>-]<<[>.<-]