Переделал 25ю Задачу с файлом через битовые операции

This commit is contained in:
AZEN-SGG 2024-11-08 10:14:58 +03:00
parent 060d43dbcf
commit a1a35972d4
4 changed files with 17 additions and 16 deletions

View file

@ -1,37 +1,38 @@
#include "count_elements.h"
double detectNumber(double number, double count) {
int detectNumber(double number, int count) {
if ((1 - number > INACCURACY) || (number - 5 > INACCURACY)) {
return 0;
}
for (double i = 1.; i < 6; i++) {
// printf("%lf ? \n", abs(number - i));
if (fabs(number - i) < INACCURACY) return (1 - (int)(fmod(count, pow(10., i)) / pow(10., i - 1))) * pow(10., i - 1);
for (int i = 1; i < 6; i++) {
if (fabs(number - i) < INACCURACY) return 1 << (i - 1);
}
return 0;
}
double summa(double info_count) {
int summa(int info_count) {
int count = 0;
for (int i = 0; i < 5; i++) {
count += fmod(info_count, 10.);
info_count /= 10;
for (;info_count;info_count >>= 1) {
count += info_count & 1;
}
return count;
}
double countElements(FILE * file) {
int countElements(FILE * file) {
double current;
double count = 100000.;
int count;
if (fscanf(file, "%lf", &current) != 1)
{
printf("File is empty!\n");
return -1;
}
count = 0;
do {
count += detectNumber(current, count);
count |= detectNumber(current, count);
} while (fscanf(file, "%lf", &current) == 1);
return summa(count);