- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
int abs(int x)
{
int a = x;
if(x >= 0)
{
return a;
}
else if(x < 0)
{
a = a^2;
a = sqrt(a);
return a;
}
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
−2
int abs(int x)
{
int a = x;
if(x >= 0)
{
return a;
}
else if(x < 0)
{
a = a^2;
a = sqrt(a);
return a;
}
}
Поиск абсолютного значения числа.
0
class Program
{
static void Main(string[] args)
{
Cell[] cells = new Cell[15];
cells[1] = new Cell(); //и ещё 14 подобных строк
cells[1].AddAdjacentCell(cells[2], 1);
cells[1].AddAdjacentCell(cells[5], 2); //и так для всех 15 ячеек
Spore spore01 = new Spore(true, false, true, true, false, true);
Spore spore02 = new Spore(true, true, true, true, true, true);
for(Int16 i = 1; i <= 14; i++)
for (Int16 k = 1; k <= 14; k++)
{
if (i != k)
{
Console.Write("Trying " + i + " " + k + "... ");
cells[i].AddSpore(spore01);
cells[k].AddSpore(spore02);
bool badAttempt = false;
for(Int16 c = 1; c <= 14; c++)
{
if (cells[c].state == CellState.Empty)
{
badAttempt = true;
break;
}
}
Console.WriteLine(badAttempt.ToString());
}
}
Console.ReadLine();
}
}
class Cell
{
public CellState state;
private Cell[] adjacentCells = new Cell[6];
private Spore currentSpore = null;
public Cell()
{
this.state = CellState.Empty;
for (Int16 i = 0; i <= 5; i++)
{
this.adjacentCells[i] = null;
}
}
public void AddAdjacentCell(Cell cell, Int16 direction)
{
if (direction >= 6)
return;
this.adjacentCells[direction] = cell;
}
public void Ray(Int16 direction)
{
if (this.adjacentCells[direction] == null)
return;
if (this.adjacentCells[direction].state == CellState.Spore)
return;
this.state = CellState.Light;
this.adjacentCells[direction].Ray(direction);
}
public void AddSpore(Spore spore)
{
this.state = CellState.Spore;
this.currentSpore = spore;
for (Int16 i = 0; i <= 5; i++)
{
if (this.currentSpore.directions[i] == true)
this.Ray(i);
}
}
public void Reset()
{
this.state = CellState.Empty;
this.currentSpore = null;
for (Int16 i = 0; i <= 5; i++)
{
this.adjacentCells[i] = null;
}
}
}
enum CellState
{
Empty,
Light,
Spore
}
class Spore
{
public bool[] directions = new bool[6];
public Spore(params bool[] rays)
{
for (Int16 i = 0; i <= 5; i++)
this.directions[i] = rays[i];
}
}
}
(обсуждение программы для поиска решений для одной головоломки под Андроид)
- Да щас напишем, хуль там делать то?
(через 5 минут)
- Ой, переполнение стека...
- ...
−2
type Speaker interface {
SayHello()
}
type Human struct {
Greeting string
}
func (Human) SayHello() {
fmt.Println("Hello")
}
...
var s Speaker
s = Human{Greeting: "Hello"}
s.SayHello()
Отсюда: https://habrahabr.ru/post/276981/
+2
setInterval(function() {
jQuery('#thsp-sticky-header').find('.ya-site-form__submit').removeClass('ya-site-form__submit').addClass('ya-site-form__submit2');
}, 500)
0
class ConnectDB {
protected static $_instance;
private function __construct() {
$this->DB = new DBConnector();
}
private function __clone() {}
private function __wakeup() {}
public static function getInstance() {
if (empty(self::$instance)) {
self::$_instance = new self;
}
return self::$_instance;
}
}
Синглтон, который каждый рас создает новый объект
+5
var pts = new Vector3[vCount];
var r = new Random();
for (var i = 0; i < pts.Length; i++)
pts[i] = new Vector3(r.Next(-10000, 10000), r.Next(-600, 600), r.Next(-10000, 10000)) * 0.05f;
/*for (var i = 0; i < pts.Length; i++)
for (var j = 0; j < pts.Length; j++)
if (pts[i].X > pts[j].X)
{
var tmp = pts[i];
pts[i] = pts[j];
pts[j] = tmp;
}*/
var vertices = new VertexPositionColor[vCount];
var indices = new int[vCount * 6];
//*
for (var i = 0; i < vCount; i++)
{
vertices[i] = new VertexPositionColor(pts[i], new Color(new Vector3(r.Next(-100000, 100000), r.Next(-100000, 100000), r.Next(-100000, 100000)) * 0.00001f));
indices[i * 6] = i;
indices[i * 6 + 3] = i;
var minDist = new float[] { 100000000, 100000000, 100000000 };
var minId = new int[] { 0, 0, 0 };
for (var j = 0; j < vCount; j++)
{
if (j == i) continue;
var dist = Vector3.DistanceSquared(pts[i], pts[j]);
if (dist < minDist[0])
{
minDist[2] = minDist[1]; minId[2] = minId[1];
minDist[1] = minDist[0]; minId[1] = minId[0];
minDist[0] = dist; minId[0] = j;
}
else if (dist < minDist[1])
{
minDist[2] = minDist[1]; minId[2] = minId[1];
minDist[1] = dist; minId[1] = j;
}
else if (dist < minDist[2])
{
minDist[2] = dist;
minId[2] = j;
}
}
indices[i * 6 + 1] = minId[0];
indices[i * 6 + 2] = minId[1];
indices[i * 6 + 4] = minId[1];
indices[i * 6 + 5] = minId[2];
}//*/
Антон, 20 лет.
Особенно вставило
indices[i * 6 + 1] = minId[0];
indices[i * 6 + 2] = minId[1];
indices[i * 6 + 4] = minId[1];
indices[i * 6 + 5] = minId[2];
−1
public void SaveModels(IEnumerable<Activity> models)
{
if (models == null && models.Count() == 0) return;
// step 1/3: remove empty models
var empty = models.Where(m => !m.ForecastedValue.HasValue && !m.ActualValue.HasValue).ToList();
if (empty != null)
{
models = models.Except(empty);
}
.....
}
Зачем такая конструкция, если можно просто
models = models.Where(m => m.ForecastedValue.HasValue && m.ActualValue.HasValue).ToList()
К тому же проверка на null бесполезна - ни Where, ни ToList не могут вернуть null. Даже если в коллекции ничего не останется.
+4
if(($ID+0)<1)
Оригинальное приведение типов
−4
var up = document.getElementById('upload'),
text1 = document.getElementById('text1'),
text2 = document.getElementById('text2'),
sliderSize = document.getElementById('sliderSize'),
sliderImage = document.getElementById('sliderImage'),
file = document.getElementById('image'),
canvas = document.getElementById('canvas'),
uploaded = document.getElementById('uploaded'),
placeholder_image = document.getElementById('placeholder_image');
up.addEventListener('click', uploadToImgur);
text1.addEventListener('keyup', updateImage);
text2.addEventListener('keyup', updateImage);
sliderSize.addEventListener('change', updateImage);
sliderImage.addEventListener('change', updateImage);
file.addEventListener('change', changeAndUpdateImage);
function readFile(fileInput, callback) {
var f = fileInput.files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
callback(e.target.result);
};
})(f);
reader.readAsDataURL(f);
}
var curImg = null;
placeholder_image.onload = function() {
curImg = placeholder_image;
updateImage();
}
function changeAndUpdateImage() {
var img = new Image();
readFile(file, function(dataURL) {
img.onload = function() {
curImg = img;
sliderImage.value = Math.max(img.width, img.height);
canvas.width = img.width;
canvas.height = img.height;
//canvas.style.height = img.height + 'px';
//canvas.style.width = img.width + 'px';
updateImage();
}
img.src = dataURL;
});
}
function drawLines(ctx, lines, x, y, yStep) {
lines = lines.split('\n');
if (yStep < 0) lines = lines.reverse();
lines.forEach(function(l, k) {
ctx.strokeText(l, x, y + yStep * k);
ctx.fillText(l, x, y + yStep * k);
});
}
function updateImage() {
var LINE_HEIGHT = 1.1;
var PARAGRAPH_HEIGHT = 1.5;
var imgSizeLimit = parseFloat(sliderImage.value);
var canvasSize = autoScale({
w: curImg.width,
h: curImg.height
}, imgSizeLimit);
canvas.width = canvasSize.w;
canvas.height = canvasSize.h;
var ctx = canvas.getContext("2d");
var txtSize = parseFloat(sliderSize.value) || 24;
if (!curImg) return;
ctx.strokeStyle = '#000000';
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(curImg, 0, 0, canvas.width, canvas.height);
ctx.font = txtSize + "px Impact";
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.lineWidth = Math.round(Math.max(1, txtSize / 12));
drawLines(ctx, text1.value, canvas.width / 2,
txtSize * PARAGRAPH_HEIGHT / 2, LINE_HEIGHT * txtSize);
drawLines(ctx, text2.value, canvas.width / 2,
canvas.height - txtSize * PARAGRAPH_HEIGHT / 2, -1 * LINE_HEIGHT * txtSize );
−1
http://i.imgur.com/xzte9cX.png
Я.. я не могу просто взять и скопировать этот код сюда. Посмотрите сами...