简体   繁体   中英

C++ long long int

So I have the following problem:

There will be a big conference next month in the city. Friends usually tend to arrive and register together, so they also end up sitting next to each other and chatting with people that they already know. In order to spice things up a bit, the conference organisers came up with a system to “shuffle” the order of the attendants and, thus, get them to meet new people.

The system works as follows: the first person arriving at the conference registration gets a ticket with a number a1 randomly chosen by the organisers. Each of the following persons arriving get a new ticket with the number ai = (ai – 1 × 31334) mod 31337, and finds its corresponding position at the queue, just behind the last person which has a number smaller or equal than ai. This means that ticket numbers in the queue should always be in order and, if there already are many persons with the same number, the latest arriving should be the last in that group.

Your task is to write a computer program that will help the attendants in finding their correct position at the queue.

Example Given the initial ticket number a1 = 7546, find the position in the queue of the 6th person arriving. So the input here is [7546, 6].

Discussion: the first person arriving gets the ticket a1 = 7546 and stands at the front of the queue. The second person gets the ticket a2 = (7546 × 31334) mod 31337 = 8699, and so stands at the second position in the queue. The third person arriving then gets the ticket with number a3 = (8699 × 31334) mod 31337 = 5240 and, therefore, gets to jump the queue and stand at the position 1, moving other people in the queue one position to the back. That is, the queue now looks like:

1: 5240 (3), 2: 7546 (1), 3: 8699 (2) where the ticket numbers are in increasing order and the number in parentesis is the original order in which the participants arrived to the conference.

Continuing this sequence, the fourth, fifth, and sixth participants will get the ticket numbers a4= 15617, a5 = 15823, and a6 = 15205; so that the queue would then look like:

1: 5240 (3), 2: 7546 (1), 3: 8699 (2), 4: 15205 (6), 5: 15617 (4), 6: 15823 (5) that is, the 6th person arriving gets to stand at the position 4 in the queue.

Answer: 4

And the following c++ code:

#include <iostream>
using namespace std;
int main ()
{
    int n, i, poz, ok;
    long long int a, v[100], aux;
    cout << "v[1]= "; cin >> v[1];
    cout << "n= "; cin >> n;
    for (i=2; i<=n; i++)
        v[i]=(v[i-1]*31334)%31337;
    a=v[n]; 
    do
    {
        ok=0;
        for (i=1; i<n; i++)
            if (v[i]>v[i+1])
            {
                aux=v[i];
                v[i]=v[i+1];
                v[i+1]=aux;
                ok=1;
            }
    }while (ok==1);
    for (i=1; i<=n; i++)
        if (v[i]==a)
            poz=i;
    cout << poz;
    return 0;
}

It displays the right things for small numbers, but when I enter larger ones here comes the problem because it simply breaks. For example, it displays 4 for [7253,10] and 1 for [24284,10] but it breaks when I input [12879,505]. Any idea?

Even without going over your code I can say that integer overflowing shouldn't be a problem even for signed int s:

31337² = 982,007,569 which is 111010100010000011111100010001 , a 30 bit number.

Therefore, at no point should a calculation here exceed the MAX INT which is (2^31)-1 = 2,147,483,647 . Just as a piece of trivia, long long int would support numbers up to (2^63)-1 = 9,223,372,036,854,775,807 .

Now what needs to be done is to debug the code...

As per your bug - I believe the problem is that you define an arrav v the size of 100, and attempt to find the 505th element - which is in the essence an illegal memory access and thus unpredictable. You're doing a buffer overflow on your own code :)

Apart from all that, I'd recommend you spending some time organizing your code - #define or add const values for your constants (array size, 31337, 31334 etc.) Remember that arrays in C++ begin with v[0] , not v[1] - No matter what is described textually. This is a mistake. Encapsulate your for and if statements with blocks { } even if you only have a single expression following them - or when you add code in the future you'll forget them and have a "fun" time debugging it.

As a general recommendation - save yourself some grief and make a habit of using Yoda case: 1 == ok instead of ok == 1 since a typo turning it into ok = 1 will cause you a VERY nasty bug.

You should consider using a vector and pushing back values.

But you are going out of bounds for your array. You made an array of size 100 and you are trying to work with 505 spaces on your last test run.

So either use a vector and push_back your values or increase the size of your array. For ease of use I would recommend the vector.

Here is something I threw together to solve your problem using containers and STL functions to do most of the work for you. Hope this helps you. =)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using LONG64 = long long;

const int MULTIPLIER = 31334;
const int MOD = 31337;

int main()
{
    vector<LONG64> ticket;

    cout << "v[1]= ";
    LONG64 seed;
    cin >> seed;
    ticket.emplace_back(seed);

    cout << "n= ";
    int queue_spot;
    cin >> queue_spot;

    for (auto i = 1; i <= queue_spot; ++i) {
        auto seed_number = (ticket[i - 1] * MULTIPLIER) % MOD;
        //cout << "\nseed_number " << seed_number;
        ticket.emplace_back(seed_number);
    }

    LONG64 person_ticket_number = ticket[queue_spot - 1];

    cout << "\nPerson's ticket number " << person_ticket_number;

    sort(ticket.begin(), ticket.end());

    auto spot = find(ticket.begin(), ticket.end(), person_ticket_number);

    cout << "\nPerson is in spot " << (spot - ticket.begin()) + 1 << endl;

    system("PAUSE");
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM