Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Last active August 29, 2015 14:02
Show Gist options
  • Save Fiona-J-W/99c8be5461873904db6e to your computer and use it in GitHub Desktop.
Save Fiona-J-W/99c8be5461873904db6e to your computer and use it in GitHub Desktop.
inversion-calculator
#include <algorithm>
#include <string>
#include <iostream>
#include <iterator>
#include <cstdint>
int main() {
std::string line;
while(std::getline(std::cin, line)) {
std::size_t inversions = 0;
auto begin = line.begin();
auto end = line.end();
for (auto it = begin; it != end; ++it) {
inversions += std::count_if(begin, it, [&](auto val){return val > *it;});
}
std::cout << "inversions in '" << line << "': " << inversions << '\n';
}
}
# Makefile for a.out
# created with makefile-creator
####################
#Settings:
CXX ?= g++
FLAGS += -Wall -Wextra -Wpedantic -std=c++1y -O3 -mtune=native -Werror -DNDEBUG
LIBS +=
INCLUDES +=
TARGET = a.out
OBJECTS = main.o
####################
#Rules:
$(TARGET) : $(OBJECTS)
$(CXX) $(FLAGS) -o $(TARGET) $(OBJECTS) $(LIBS)
%.o:
$(CXX) $(FLAGS) $(INCLUDES) -c -o $@ $<
clean:
rm *.o
all: $(TARGET)
####################
#Dependencies:
main.o: main.cpp makefile
use=cpp
use=release
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment