Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. algorithm fun
Forum Updated to NodeBB v4.3 + New Features

algorithm fun

Scheduled Pinned Locked Moved Solved Brainstorm
1 Posts 1 Posters 502 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    Imagine an array of n items. Given an index v, your task is to "normalize" v so that 0 <= v < n. Two catches:

    1. When the indices extend beyond 0-9, they don't merely repeat; they "mirror." So, if n = 3,
    v  v'
    0 0
    1 1
    2 2
    3 2
    4 1
    5 0
    
    1. must work for negative values of v.

    This didn't seem that complicated at first glance, but I had a heck of a time with it. My solution works, but I'm wondering whether any of you math wizards can simplify it. Here's my solution:

    int getReal(int v, int n) {
        int x;
        int y;
        int z;
    
        x = abs(v);
    
        y = x % (2*n);
    
        if (y >= n) {
            z = n - (x % n) - 1;
        } else {
            z = x % n;
        }
    
        printf("%3d %3d\n", v, z);
    
        return z;
    }
    

    Thanks...

    1 Reply Last reply
    0
    • mzimmersM mzimmers has marked this topic as solved on

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved