Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add ACODE - classical SPOJ #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Hacktober Fest 2020/Problems/SPOJ/ACODE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although <bits/stdc++.h> is almost perfect for Cp, but still it is supported by GCC compiler only. As our repository is a learner-friendly repo, I would like you to include all the libraries/header files used.

using namespace std;

int main() {
char a[5010];
Copy link
Member

@Abhijit2505 Abhijit2505 Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using char is a good but from 'C++' point of view, it will be better if we use the features of the language to a larger extent.
Like we have a string class inside CPP, and that can be used instead of char.

Using char is not wrong, but it is not a good practise as we are using 'modern C++'. Let's utilize the language properly.

Copy link
Member

@Abhijit2505 Abhijit2505 Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing clean code is a great part of programming, not for CP contests, but for other human beings to understand your code.

Some points to be kept in mind,

  • Use proper variable names, like we can use array[] instead of a.
  • We can use better names for i & j.

int i, j;
scanf("%s", a);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well as I have mentioned above, although scanf and printf are faster than the contemporary cin and cout, but I encourage you to use the later.

We are using C++ and let's use it's total feature. scanf and printf are also included in C programming language. Let's do something different in CPP.

I would like you to explore, getline(cin,str) function a little deeper.

while(a[0] != '0')
{
int n = strlen(a);
long long int b[n];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using long long int frequently in a bigger code, we can just use the powerful C++ feature.

I would like you to explore typedef and #define functionality of C++.

Also, can you make the code generic by using some Templates?
Hint : Use template<class T>

for(i = 0; i < n; i++)
b[i] = 0;
b[0] = 1;
for(i = 1; i < n; i++)
{
j = (a[i - 1] - '0') * 10 + (a[i] - '0');
if(a[i] - '0')
b[i] = b[i - 1];
if(j > 9 && j < 27)
{
if(i == 1)
b[i] = b[i] + 1;
else
b[i] = b[i] + b[i-2];
}
}
printf("%lld\n", b[n - 1]);
scanf("%s", a);
}
if(a[0] == '0')
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making the code modular? That will be easier for the learners to understand the I/O as well as the main algo.
Instead of including all the code inside the main function (which is a pretty bad standard in the industry), you may try to form some functions!

What about approaching the problem through the OOP functionalities of C++ ?

}