You are not logged in. Please login at www.codechef.com to post your questions!

×

[C++] Am I using for loops correctly?

0
1

My solution for CLEANUP goes as follows:

#include <iostream>
#include <set>
#include <vector>
#define lo unsigned long long
using namespace std;
void solve(lo M, lo N,lo a[]){
    set<lo> s;
    vector<lo> v;
    vector<lo> j;
    vector<lo> k;
    bool polar = true;
    //creates list of 1,2..M
    for (lo x  = 1; x<=M;x++){
        s.insert(x);
    }
    //deletes terms
    for (lo x  = 0; x<N;x++){
        s.erase(a[x]);
    }
    for (auto x : s) {
        v.push_back(x);
    }
    for (lo x = 0; x<v.size();x+=2){
            j.push_back(v[x]);
    }
    for (lo x = 1; x<v.size();x+=2){
        k.push_back(v[x]);

    }
    for (auto x : j) {
        cout << x << " ";
    }
    cout << endl;
    for (auto x : k) {
        cout << x << " ";
    }
    cout << endl;

}
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    lo T;
    cin >> T;
    lo M,N = 0;
    lo a[N];
    for (lo x = 0; x<T;x++){
        cin >> M >> N;
        for (lo y = 0; y<N;y++){
            cin >> a[y];
        }
        solve(M,N,a);
    }

    return 0;
}

However I seem to be getting wrong answers. I suspect this is because of how I used for loops. I tried to use increments of 2, however I'm not sure if that causes overflow or not(goes past the set limit). Can anyone confirm? Thanks

asked 21 Jul '17, 03:44

jaycram's gravatar image

3★jaycram
1
accept rate: 0%


hey everything is correct but you are doing a small mistake.

Initially N is 0 and you are declaring an array a[N] then you can store only one element in the array.

Declare array after scaning the value of N then you will get AC.I just changed that got AC.here is the link

link

answered 21 Jul '17, 04:26

hruday968's gravatar image

4★hruday968
1.7k210
accept rate: 14%

ty so much, this explains why my last 3 solutions have been wrong lol

(21 Jul '17, 20:11) jaycram3★

Another thing I want to add with @hruday, it's always good practice to declare an array globally. There's always chance of stack overflow if you declare an array locally.

link

answered 21 Jul '17, 08:06

sudip_95's gravatar image

4★sudip_95
7556
accept rate: 10%

The best way to understand loops is to understand the concept behind them. Loops are used in programming when you need to perform a task more than once. While loops and For loops exist to this end. the main difference in the two, besides the code you use to write them, is that while loops check the condition at the end of the loop, and Do My Essays will repeat the loop until the while condition is met.

link

answered 21 Jul '17, 15:47

jenniferjudy22's gravatar image

0★jenniferjudy22
-1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,706
×2,698
×1,901
×179

question asked: 21 Jul '17, 03:44

question was seen: 391 times

last updated: 21 Jul '17, 20:11