- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
#define as ;while
int main(int argc, char* argv[])
{
int n = atoi(argv[1]);
do printf("n is %d\n", n) as ( n --> 0);
return 0;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
#define as ;while
int main(int argc, char* argv[])
{
int n = atoi(argv[1]);
do printf("n is %d\n", n) as ( n --> 0);
return 0;
}
+1
#include <iostream>
using namespace std;
void Brezenhem(char **z, int x0, int y0, int x1, int y1)
{
int A, B, sign;
A = y1 - y0;
B = x0 - x1;
if (abs(A) > abs(B)) sign = 1;
else sign = -1;
int signa, signb;
if (A < 0) signa = -1;
else signa = 1;
if (B < 0) signb = -1;
else signb = 1;
int f = 0;
z[y0][x0] = '*';
int x = x0, y = y0;
if (sign == -1)
{
do {
f += A*signa;
if (f > 0)
{
f -= B*signb;
y += signa;
}
x -= signb;
z[y][x] = '*';
} while (x != x1 || y != y1);
}
else
{
do {
f += B*signb;
if (f > 0) {
f -= A*signa;
x -= signb;
}
y += signa;
z[y][x] = '*';
} while (x != x1 || y != y1);
}
}
int main()
{
const int SIZE = 25; // размер поля
int x1, x2, y1, y2;
char **z;
z = new char*[SIZE];
for (int i = 0; i < SIZE; i++)
{
z[i] = new char[SIZE];
for (int j = 0; j < SIZE; j++)
z[i][j] = '-';
}
cout << "x1 = "; cin >> x1;
cout << "y1 = "; cin >> y1;
cout << "x2 = "; cin >> x2;
cout << "y2 = "; cin >> y2;
Brezenhem(z, x1, y1, x2, y2);
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
cout << z[i][j];
cout << endl;
}
cin.get(); cin.get();
return 0;
}
https://prog-cpp.ru/brezenham/
спойлер: автор тян
0
class Solution
{
public:
ListNode *reverseList1(ListNode* head){
for (int i=1;i<6;i++) {
head = new ListNode(1);
head->val=i;
head->next=last;
printf("%d->",head->val);
}
printf("NULL");
}
ListNode *reverseList2(ListNode* head){
head = nullptr;
head = new ListNode(5);
head->val = 5;
printf("%d->",head->val);
head->next = new ListNode(4);
head->next->val=4;
printf("%d->",head->next->val);
head->next->next = new ListNode(3);
head->next->next->val = 3;
printf("%d->",head->next->next->val);
head->next->next->next = new ListNode(2);
head->next->next->next->val=2;
printf("%d->",head->next->next->next->val);
head->next->next->next->next = new ListNode(1);
head->next->next->next->next->val=1;
printf("%d->",head->next->next->next->next->val);
head->next->next->next->next->next = nullptr;
printf("NULL");
}
};
От "разработчика" требовалось реализовать функции обращения связанного списка двумя способами.
Занавес
0
constexpr std::size_t find(string_view str, char c) noexcept {
#if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
// https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc
// https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
constexpr bool workaround = true;
#else
constexpr bool workaround = false;
#endif
if constexpr (workaround) {
for (std::size_t i = 0; i < str.size(); ++i) {
if (str[i] == c) {
return i;
}
}
return string_view::npos;
} else {
return str.find_first_of(c);
}
}
какой constexpr)))
+2
https://github.com/golded-plus/golded-plus/blob/master/golded3/gccfgg0.cpp#L162
0
class Foo {
std::atomic<bool> a{false};
std::atomic<bool> b{false};
public:
Foo()
{
}
void first(function<void()> printFirst) {
printFirst();
a.store(true, std::memory_order_release);
}
void second(function<void()> printSecond) {
while (!a.load(std::memory_order_acquire))
;
printSecond();
b.store(true, std::memory_order_release);
}
void third(function<void()> printThird) {
while (!b.load(std::memory_order_acquire))
;
printThird();
}
};
https://leetcode.com/problems/print-in-order/
36 / 36 test cases passed.
Runtime: 1697 ms
Memory Usage: 7 MB
Хочу, чтобы начался срач по поводу того, как достичь рантайма в 200мс
+3
rule_t block_r = ch_p('{') >> *(~ch_p('}') | (~ch_p('{') >> block_r));
rule_t bin_r = str_p("0b") >> bin_p[assign(m)];
rule_t hex_r = str_p("0x") >> hex_p[assign(m)];
rule_t dec_r = uint_p[assign(m)];
rule_t id_r = alpha_p >> *(alnum_p | ch_p('_') | ch_p('.'));
rule_t string_r = ch_p('"') >> (*(~ch_p('"')))[assign(s)] >> ch_p('"');
rule_t channel_r = str_p("channel") >> *(space_p) >> id_r[assign(s)][assign_a(ch.name,s)][push_back_a(lstJSVars_,s)] >> *(space_p) >> ch_p('{') >>
*(space_p
| (str_p("mode") >> *(space_p) >> ch_p('=') >> *(space_p) >>
(str_p("in")[assign_a(ch.mode,(int)channel::IN)] | str_p("out")[assign_a(ch.mode,(int)channel::OUT)]) >> *(space_p) >> ch_p(';'))
| (str_p("number") >> *(space_p) >> ch_p('=') >> *(space_p) >> int_p[assign(ch.number)] >> *(space_p) >> ch_p(';'))
| (str_p("channel") >> *(space_p) >> ch_p('=') >> *(space_p) >> int_p[assign(ch.ch)] >> *(space_p) >> ch_p(';'))
| (str_p("gain") >> *(space_p) >> ch_p('=') >> *(space_p) >> int_p[assign(ch.gain)] >> *(space_p) >> ch_p(';'))
| (str_p("type") >> *(space_p) >> ch_p('=') >> *(space_p) >>
(str_p("analog")[assign_a(ch.type,(int)channel::ANALOG)] | str_p("discrete")[assign_a(ch.type,(int)channel::DISCRETE)]) >> *(space_p) >> ch_p(';'))
| (str_p("mask") >> *(space_p) >> ch_p('=') >> *(space_p) >>
(bin_r[assign_a(ch.mask,m)] | hex_r[assign_a(ch.mask,m)] | dec_r[assign_a(ch.mask,m)]) >> *(space_p) >> ch_p(';'))
) >>
ch_p('}') >> *(space_p) >> ch_p(';');
rule_t device_r = str_p("device_info") >> *(space_p) >> id_r[assign(dev_info_.name)] >> *(space_p) >> ch_p('{') >>
*(space_p |
(str_p("device") >> *(space_p) >> ch_p('=') >> *(space_p) >> string_r[assign_a(dev_info_.dev,s)] >> *(space_p) >> ch_p(';')) |
(str_p("blocking") >> *(space_p) >> ch_p('=') >> *(space_p) >>
(str_p("true")[assign_a(dev_info_.blocking,true)] | str_p("false")[assign_a(dev_info_.blocking,false)]) >> *(space_p) >> ch_p(';')) |
(str_p("freq_ch") >> *(space_p) >> ch_p('=') >> *(space_p) >> int_p[assign(dev_info_.freq_ch)] >> *(space_p) >> ch_p(';')) |
(str_p("freq_sel_ch") >> *(space_p) >> ch_p('=') >> *(space_p) >> int_p[assign(dev_info_.freq_sel_ch)] >> *(space_p) >> ch_p(';')) |
(str_p("speed") >> *(space_p) >> ch_p('=') >> *(space_p) >> int_p[assign(dev_info_.speed)] >> *(space_p) >> ch_p(';')) |
channel_r[assign_a(v,ch)][push_back_a(lstChannels_,v)]
) >>
ch_p('}') >> *(space_p) >> ch_p(';');
rule_t var_r = (str_p("var") >> *(space_p) >> id_r[assign(s)][push_back_a(lstJSVars_,s)] >>*(~ch_p(';')) >> ch_p(';'))[assign(s)][push_back_a(lstVars,s)];
rule_t function_r = str_p("function") >> *(~ch_p('`'));
//rule_t function_r = str_p("function") >> *(~ch_p('{')) >> block_r;
rule_t script_r = str_p("script") >> *(space_p) >> ch_p('{') >>
*(space_p |
var_r[assign(s)][push_back_a(lstJSVars_,s)] |
(function_r[assign(s)][push_back_a(lstFunc,s)] >> *(space_p) >> ch_p('`'))
) >>
ch_p('}') >> *(space_p) >> ch_p(';');
Из системы эмуляции полёта на тренажере.
+2
int f6(int* a,int k, double& min, int& n, int& count) {
int i;
min = a[0];//8
n = 0;
count = 0;
for (i = 0; i < k; i++) {
if (a[i] == 0) {
count += 1;
}
if (abs(10-abs(a[i])) < abs(min)) {
min = a[i];
n = i;
}
}
return 0;
}
Помогите!!Пожалуйста!!Надо найти ближайший элемент к 10ти и его номер, почему не работает??
−3
https://t.me/stdvector
го в вектор
+2
struct S { struct Inner { }; };
template<int N> struct X;
auto refl = ˆS;
auto tmpl = ˆX;
void f() {
typename [:refl:] * x; // OK: declares x to be a pointer-to-S
[:refl:] * x; // error: attempt to multiply int by x
[:refl:]::Inner i; // OK: splice as part of a nested-name-specifier
typename [:refl:]{}; // OK: default-constructs an S temporary
using T = [:refl:]; // OK: operand must be a type
struct C : [:refl:] {}; // OK: base classes are types
template [:tmpl:]<0>; // OK: names the specialization
[:tmpl:] < 0 > x; // error: attempt to compare X with 0
}
и да, это приняли.
https://lists.isocpp.org/sg7/2021/04/0226.php
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2320r0.pdf