# Logic Tests Series (2) - DECR 逻辑测试系列之二 - DECR

in cn •  last year

@justyy 's series of Logits Tests:

What you can do with this tiny programming language with only 4 instructions? Today, we are going to implement the DECR function which decrements the variable by one, for example, DECR(X) where X=5 will make X=4.

DECR(X) {

}

The only 4 instructions are: INCR, LOOP, ZERO and ASGN. And all variables hold non-negative integers. To make X decremented by 1, we can increment a temp variable by (X-1) times..

DECR(X) {
ZERO(V1)
LOOP(X) {
ASGN(X, V1)
INCR(V1)
}
}

We translate this to C++, which might be a bit easy to understand.

void decr(unsigned int &x) {
int v1 = 0;
int xx = x;
for (; x > 0; -- x) {
xx = v1;
v1 ++;
}
x = xx;
}

If you change the value of the loop variable x, the loop iteration count may be altered. Therefore, we declare a temp variable xx and assign the xx to X after loop when it gets incremented (x-1) times.

Image Credit: pixabay.com

@justyy 的逻辑测试系列:

DECR(X) {

}

DECR(X) {
ZERO(V1)
LOOP(X) {
ASGN(X, V1)
INCR(V1)
}
}

void decr(unsigned int &x) {
int v1 = 0;
int xx = x;
for (; x > 0; -- x) {
xx = v1;
v1 ++;
}
x = xx;
}

Chrome 插件:

Steem API:

Chrome Extensions:

Steem API:

Recent Posts:

// Later, it may be reposted to my blogs: justyy.com, helloacm.com and codingforspeed.com 稍后同步到我的中文博客和英文计算机博客

Originally published at https://steemit.com Thank you for reading my post, feel free to Follow, Upvote, Reply, ReSteem (repost) @justyy which motivates me to create more quality posts.

Sort Order:
·  last year

Thanks for the information. I'm eager to learn from this programming.

·  last year

This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond

·  last year

This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond