menu

arrow_back What would be the best way to write the code?

by
1 vote
#include <iostream>

int main() {
int a = 4668;
int b = 10415;
int c = 0;
int minValue = 0;
for(int i=a; i<b; i++) {
if(i%3==0 || i%11==0) {
if(i%2!=0 && i%13!=0 && i%22!=0 && i%33!=0) {
c++;
if(c == 1) {
minValue = i;
}
}
}
}
std::cout << c << minValue << "\n";
}
It seems to me that all these nested ifs smell like crap, can anyone give me an example of a nicer code?

6 Comments

The whole code stinks. Because it is unclear why to continue the loop, after c == 1
What is the meaning of this great code? What are we supposed to find?
Saboteur , Fedor Vlasenko Yes, it's a problem from the USE. https://inf-ege.sdamgia.ru/problem?id=27627
с==1 - code to find the minimum number to fit the requirements, the с is the number of numbers satisfying the requirements specified in the task.
So what problem should the code solve?
#include <iostream>

bool check(const int &value) {
//если число не делится на 2, то на 22 тем более
return (value % 3 == 0 || value % 11 == 0) && value % 13 != 0 && value % 33 != 0;
}

int main() {
int a = 4668;
int b = 10415;
int c = 0;
int i = a % 2 == 0 ? a + 1 : a;
int minValue = 0;

for (; i < b; i += 2) {
if (check(i)) {
c = 1;
minValue = i;
i += 2;
break;
}
}

for (; i < b; i += 2) {
if (check(i)) {
c++;
}
}
std::cout << minValue << " - " << c << std::endl; //4671 - 965
}
I'm just learning ++ I'll describe the logic by the condition of the problem our set will represent odd numbers that are divisible by 3 or 11 and are not divisible by 13, 33 I broke the code into two loops, the first finds the first minimum number, the second keeps counting the total number step 2 in the loop, since we only need the odd ones int i = a % 2 == 0 ? a + 1 : a; the ternary operator assigns i to an odd number
Fedor Vlasenko , bool check(int &value) I checked that the program works equally well with both &value as well as with value Do I understand correctly that we pass the address of the variable so as not to create a new one and take up more memory?
Yes, and probably
bool check(const int &value)
so it would be more correct to make the value of the argument a constant reference

2 Answers

by
 
Best answer
0 votes
1) It makes no sense to make nested conditional operators.
2) It makes no sense to check for a multiplicity of 22, because we are already checking for a multiplicity of 2.
3) Functions of the int type must always return a number of the int type, in the entry point (main) they use return 0.

#include <iostream>
using namespace std;

int main() {
int a = 4668;
int b = 10415;
int c = 0;
int minValue = 0;
for(int i=a; i<b; i++) {
if(i%13!=0 && i%2!=0 && i % 33!=0 && (i%3==0 || i%11==0)) {
c++;
if(c == 1) {
minValue = i;
}
}
}
cout << c << minValue << endl;

return 0;
}
by
1 vote
such nested ifs can be replaced by a single condition conjunction iff.
c, c++ and checks can be replaced by break at the end of the ifa body.

2 Comments

OCTAGRAM Right.
with conjunctive conditions