Блог пользователя HitmanBBa

Автор HitmanBBa, 9 лет назад, По-английски

Hi everybody...

How are you ??

I have learnt a new move in c++ while reading an array, and I would like to share it with you my friends :).

Most of us while reading an array in contest he/she use this move:

int x[100005];
int i;
for(i=0;i<50;i++)
    scanf("%d", &x[i]);

We use &x[i] for point to the ith element in the array... look look look we just press shift + 7 and then write x[i]. oh that's make me so tired.

But look at this move...

int x[100005];
int i;
for(i=0;i<50;i++)
    scanf("%d", x+i); // I just press x + i only and for the same reason, that's make me comfortable. ^_^

If you got any lazy move like this please share it with us :).

Теги c++
  • Проголосовать: нравится
  • +13
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +20 Проголосовать: не нравится

If we speak about c++ and laziness cin >> x[i] is the answer

  • »
    »
    9 лет назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    But I heard a lot about scanf faster than cin, right?

    • »
      »
      »
      9 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +9 Проголосовать: не нравится

      Well, it depends.

      It we use cin.tie(0) and ios_base::sync_with_stdio(false) then on real g++ it's as fast as scanf, but not in MSVC, and not always in MinGW.

      Currently on CF its slower but is usually fast enough

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      You can also use my 11158242 template. It uses streams, is big enough, but is very convenient(at least for me) and can be used for user-defined types easily
      About speed you should understand, when it is critical and when it is better to switch to stdio and even use arrays instead of stl. But I don't remember last time when it was such critical to io

»
9 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

well i find this more lazy for(int i = 1 ; i <= n ; scanf("%d",arr + (i++)));

»
9 лет назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

In C++ in library <cmath> you can use function hypot(double x, double y) to compute distence between 2-points instead of this code:

#include <cmath>

using namespace std;

double distence (double x, double y)

{

    return sqrt(x * x + y * y);

}
»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You know when you have to print an M x N grid that is stored in an array?

for(int i = 0 ; i < m ; i++)
   for(int j = 0 ; j < n ; j++)
      cout << arr[i][j] << "\n"[j!=n-1];

It prints the elements with a space between them, and with an endline when j reaches n-1.

But sometimes the OJ doesn't recognize the space, so be careful.