Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [c++]

This challenge is related to the C++ language. Note that challenges that require the answers to be in a specific language are generally discouraged.

Filter by
Sorted by
Tagged with
13
votes
7answers
2k views

Generate ladder of integers using the least number of unique characters (in C++)

I am new to the sport of code golf. I am trying to generate a ladder of integers using the least number of unique characters in C++. Let's say we are given an integer 4. We will generate the ...
-2
votes
2answers
155 views

find the value at kth position when numbers are sorted lexicographically till n [closed]

Ex :- Input: n = 12, k = 5 Output: ans = 2 Sorted list S: ["1", "10", "11", "12", "2", "3", "4", "5", ...., "9"] ans = 2
1
vote
1answer
839 views

A+B using C++ language

I'm golfing a program to sum two numbers in C++. Example: 10 3 13 I found out that this problem can be solved in 54 symbols (without spaces and so on) using C++....
7
votes
4answers
627 views

Let's play POKE!

Poke is a really simple incomplete-information game. It's like "Poker", but simpler. At the start of every round, each player is dealt a hand. To simplify things, a hand is a uniformly distributed ...
73
votes
18answers
7k views

The Futuristic Gun Duel

The Background Future In the year 2017, you and your opponent will face off in a futuristic gun battle where only one may survive. Are you experienced enough to defeat your opponent? Now is the time ...
12
votes
5answers
1k views

Add comments to a Python script and make it a bilingual Python/C++ “program”

Given the following Python 3 script: def greet(): print("Hello, world!") greet() Prepend some lines to this text file so that it can be both executed as a ...
2
votes
0answers
377 views

Useful C++ template metaprograms [closed]

C++ implements a Turing-complete functional programming language which is evaluated at compile time. Hence, we can use a C++ compiler as an interpreter to run our metaprogram without ever generating ...
12
votes
5answers
2k views

Risk, the Warlight Way

Introduction In this game, players use their armies to fight other players' armies, capture territories, and become the last man standing. Each turn, players receive a base number of armies to use at ...
0
votes
1answer
307 views

Cycles and stuff - What does this program do? [closed]

I just found one of my first C++ programs in some deep caves on my disk... ...
27
votes
6answers
2k views

Write a C / C++ Polyglot

This challenge's concept is pretty simple. All you have to do is write a program that will compile as both valid C and valid C++! Well, there are some catches. The program must behave differently when ...
12
votes
15answers
10k views

Compare two integers in C or C++ without comparison operators

Produce the shortest program which takes two signed integers as input (through stdin or as arguments) and displays 3 different outputs depending upon whether the first number is (1) greater than, (2) ...
11
votes
2answers
894 views

X greater than 3 with at least 2 difference between X and Y

I am trying to golf down some C++. Is it possible to make this condition shorter? X > 3 & X - Y > 1 (Apart from removing whitespace, of course.) So, <...
6
votes
9answers
2k views

Shortest C \ C++ function to generate -1, 0 ,1 [closed]

Let a function receive two integers i and j between 0 and ...
5
votes
2answers
539 views

Generate a self-extractor application

Create a program in any language, that takes an arbitrary binary input, and generates a valid C++11 program, which when compiled and run will print this binary input. So you could do this (on linux ...
5
votes
6answers
1k views

Make FizzBuzz using only preprocessor directives

I have a source file here: #include <FizzBuzz.h> fizzbuzz And I want to make "fizzbuzz" turn into a FizzBuzz program ...
3
votes
1answer
812 views

Flip the commented-out code [closed]

This puzzle works for any programming language that has the same // single-line and /* ... */ multi-line comments that C++, C# ...
1
vote
1answer
458 views

Generate numerical permutations with conditions and template metaprogramming

This, but via template metaprogramming: template <int n> struct insanity { int[][n] permutations; } Generate all numbers with n digits (0-9) such that each ...
12
votes
1answer
722 views

Implement an IEEE 754 64-bit binary floating point number through integer manipulation

(I've tagged the question "C" for the time being, but if you're aware of another language that supports unions, you can also use that.) Your task is to build the four standard mathematical operators <...
5
votes
6answers
2k views

RPN calculator without pointers

Me and some other students were at a bar discussing programming languages, in particular the C programming language. At some point a first year student said he didn't need no damn pointers. Another ...
4
votes
4answers
3k views

The shortest code to tell if a number is even or odd

One of my colleagues proposed us a challenge: to write the shortest C/C++ program to determine if a number is even or odd. These are the requirements: Get a number from keyboard, at run-time (not ...
2
votes
3answers
786 views

m3ph1st0s's programming puzzle 4 (C/C++): “3-way swap” [closed]

My last puzzle has generated some confusion and controversy so I decided to give up, for now, on those "replace one character"-type puzzles. Hope this 4th puzzle will redeem myself after all the ...
11
votes
5answers
2k views

m3ph1st0s's programming puzzle 3 (C): “Easy bug” [closed]

This is the 3rd of my series of C/C++ puzzles; in case you missed the first 2 they are here: (1) m3ph1st0s's programming puzzle 1 (C++) (2) m3ph1st0s's programming puzzle 2 (C++): "Call ...
8
votes
6answers
1k views

Shortest undefined behavior sample in C++ [closed]

What's the shortest, well-formed C++ code that exhibits undefined behavior?
-2
votes
12answers
839 views

Code-golf: square of the number of ones

I'm solving problem: in file "a.in" given the number N - length of the number consists of ones. Need to gets square of it, and put this in file "a.out". This is my shortest solution(150 bytes): ...
-2
votes
2answers
3k views

Conversion to palindrome with minimal points used

Input: a word (2-100 characters) Convert this word to a palindrome: delete character - 13 points add character - 12 points increase character - 5 points ('d' > 'e') decrease character - 4 points ('n'...
7
votes
7answers
2k views

Shortest code that return SIGSEGV among the given languages [duplicate]

I come across a question in a coding competion "write a code that returns SIGSEGV(Segmentation fault ) " . Points were given on the basis of length of code. The prgramming languages available ...
1
vote
2answers
641 views

A small puzzle. Why does this code work the way it does? [closed]

This was given to me by a friend of mine, it prints out 0 through 9. We have been quite puzzled over this. Does anyone know why this works? ...
2
votes
2answers
860 views

Fastest C/C++ comparison function for an opaque field containing doubles

Description: Write the fastest comparison function for an opaque field containing a double. Here is my example: ...
16
votes
8answers
9k views

Compute the CRC32 table at compile-time [closed]

The reference implementation of CRC32 computes a lookup table at runtime: ...
89
votes
15answers
58k views

Generate the longest error message in C++

Write a short program, that would generate the longest possible error message, in a standard C++ compiler (gcc, cl.exe, ...
25
votes
17answers
8k views

Print 1 to 1000 in C++ without semi-colons

Following this popular question, present your solution which prints the numbers 1 to 1000 (all of them, not the string "1 to 1000" verbatim or something funny) in C++ without using any semi-colons. ...
48
votes
25answers
12k views

Tips for golfing in C++

What general tips do you have for golfing in C++? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C++ (e.g. "remove comments" is not ...
10
votes
4answers
1k views

fizz buzz in TMP

The Fizz Buzz problem is a very basic problem to solve that is used by some to weed out interviewees that don't know how to program. The problem is: ...