anonymous No title
C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define GENE 20 //個数
#define BIT 8 //箱の数
#define MAX 30 //量の最大値
#define GENERATION 40 // 世代数
#define CROSSOVER 0.2 // 交伹率
#define MUTATION 0.05 // 突然変異率

//構造体の定義
struct Gene
{
	int quantitiy[BIT]; //量
	int price[BIT]; //価格
	int bit[BIT]; //入れるか入れないか
	int sum_price; //価格の合計値
	int sum_quantity; //量の合計値
}gene[GENE], gene2;

struct Elite
{
	int sum_price;
	int sum_quantity;
}elite;

int generation_count;

void input()
{
	for (int i = 0; i < GENE; i++)
	{
		FILE *file;
		errno_t error;
		error = fopen_s(&file, "data.txt", "r");

		for (int j = 0; j < BIT; j++)
		{
			fscanf_s(file, "%d,%d", &gene[i].quantitiy[j], &gene[i].price[j]);

			//遺伝子の初期化
			gene[i].bit[j] = rand() % 2;
			//printf("%2d,%2d,%2d\n", gene[i].quantitiy[j], gene[i].price[j], gene[i].bit[j]);
		}
		fclose(file);
		//printf("**************************\n");
	}
	return;
}

void calculation()
{
	for (int i = 0; i < GENE; i++)
	{
		gene[i].sum_price = 0;
		gene[i].sum_quantity = 0;

		for (int j = 0; j < BIT; j++)
		{
			if (gene[i].bit[j] == 1)
			{
				gene[i].sum_price += gene[i].price[j];
			}
			if (gene[i].bit[j] == 1)
			{
				gene[i].sum_quantity += gene[i].quantitiy[j];
			}
		}
		//printf("%2d, %2d\n",gene[i].sum_quantity, gene[i].sum_price);
	}
	//printf("**************************\n");
}

void evalution()
{
	for (int  i = 0; i < GENE; i++)
	{

		if (gene[i].sum_quantity > MAX)
		{
			gene[i].sum_price = -1 * gene[i].sum_price;
		}
		//printf("%2d, %2d\n", gene[i].sum_quantity, gene[i].sum_price);
	}
	//printf("**************************\n");
}

void sort()
{

	//printf(" 量 価格\n");
	for (int i = 0; i < GENE; ++i)
	{
		for (int j = i+1; j<GENE; ++j)
		{
			if (gene[i].sum_price < gene[j].sum_price)
			{
				gene2 = gene[i];
				gene[i] = gene[j];
				gene[j] = gene2; 
			}
		}
		//printf("%2d, %2d\n", gene[i].sum_quantity, gene[i].sum_price);
	}

	for (int i = 0; i < GENE; i++)
	{
		if (gene[i].sum_price < 0)
		{
			gene[i].sum_price = -1 * gene[i].sum_price;
		}
	}

	elite.sum_price = gene[0].sum_price;
	elite.sum_quantity = gene[0].sum_quantity;

	printf(" elite  %3d  %3d\n", elite.sum_quantity, elite.sum_price);
}

void crosscover()
{
	int point /*交叉する位置*/, random_gene1 /*1つ目の親個体*/, random_gene2 /*2つ目の親個体*/, bit2 /*入れ物*/;

	for (int i = 0; i < (int)(GENE * CROSSOVER); i++)
	{
		point = rand() % (BIT - 2) + 2; // 交叉する位置をランダムで選択
		random_gene1 = rand() % (GENE - 1) + 1; // 1つ目の親個体

		do
		{
			random_gene2 = rand() % (GENE - 1) + 1; //2つ目の親個体
		} while (random_gene2 == random_gene1); //random_gene1とrandom_gene2が同じだったらrandom_gene2を選択し直し

		// 交叉
		for (int j = point; j < BIT; j++)
		{
			bit2 = gene[random_gene1].bit[j];
			gene[random_gene1].bit[j] = gene[random_gene2].bit[j];
			gene[random_gene2].bit[j] = bit2;

		}
	}
}

void mutation()
{
	int random_bit/*突然変異させる遺伝子*/, random_gene/*突然変異させる個体*/, mutation_bit/*突然変異後の値*/;

	for (int i = 0;i < (int)(GENE * MUTATION);i++) 
	{
		random_bit = rand() % (BIT - 2) + 1; // 突然変異させる遺伝子をランダムで選択
		random_gene = rand() % (GENE - 1) + 1; // 突然変異させる個体をランダムで選択

		do
		{
			mutation_bit = rand() % (BIT - random_bit) + random_bit;

		} while (gene[random_gene].bit[random_bit] == mutation_bit); // 元の遺伝子とは違うものが出るまで繰り返し

		gene[random_gene].bit[random_bit] = mutation_bit;

	}
}

int main()
{
	srand((unsigned)time(NULL)); // 乱数の初期化
	input();
	calculation();
	evalution();
	printf("初期値  \n");
	sort();

	for (int i = 0; i < GENERATION; i++)
	{
		printf("第%2d世代\n", i+1);
		crosscover();
		mutation();
		calculation();
		evalution();
		sort();
	}

	return 0;
}
anonymous C++,TSP
C++
#include <stdio.h> // printf等の入出力関数を使うため
#include <stdlib.h> // randとsrandを使うため
#include <time.h> // timeを使うため
#include <math.h> // sin, cos等の数学関数を使うため

// 以下,定数の定義
#define GENE 50 // 個体数
#define BIT 20 // 遺伝子数(都市の数)
#define GENERATION 200 // 世代数
#define CROSSOVER 0.4 // 交伹率
#define MUTATION 0.4 // 突然変異率
#define INVERSE 0.4 // 逆位率
#define SELECT 0.2 // 淘汰率
#define L 4 // 表示する座標系の範囲
#define PAI 3.1415926536 // πの定義

// 以下,構造体の定義
struct Gene 
{
	char city_name[BIT]; // 都市の巡回順
	int bit[BIT]; // 遺伝子
	double distance; // 全都市を巡回し終わった時の総距離
}gene[GENE] /*個体*/, gene2 /*ソートで使うための入れ物*/;

struct Elite 
{
	char city_name[BIT];
	double distance;
}elite[GENERATION]; // 各世代の最も総距離が短い個体

// 以下,変数の定義
int generation_count = 0; // 世代数をカウントする
double city_x[BIT]; // 都市のX座標
double city_y[BIT]; // 都市のY座標

// 以下,関数の定義 都市の座標,都市の名前,遺伝子の初期化
	void initial_data()
	{
		// 円用
		double angle;

		for (int i = 0;i < BIT;i++)
		{
			angle = (double)i * 2.0 * PAI / (double)BIT;

			city_x[i] = (double)L / 2 + (double)L / 2 * cos(angle);

			city_y[i] = (double)L / 2 + (double)L / 2 * sin(angle);
		}

		for (int i = 0;i < GENE;i++) 
		{
			// 遺伝子の初期化
			for (int j = 1;j < BIT;j++) 
			{

				gene[i].bit[j] = rand() % (BIT - j) + j;

			}
		}
	}

	// 遺伝子型から表現型へ
	void generate_gene(void)
	{
		char city_name2; // 右詰の作業で使うための入れもの

		for (int i = 0;i < GENE;i++) 
		{
			// ASCII文字コードを使ってアルファベットを都市数分入力する
			for (int j = 0;j < BIT;j++) {

				gene[i].city_name[j] = 65 + j;

			}
			// 右詰
			for (int j = 1;j < BIT;j++) 
			{
				city_name2 =
					gene[i].city_name[gene[i].bit[j]];

				for (int k = gene[i].bit[j]; k>j; k--) 
				{
					gene[i].city_name[k] = gene[i].city_name[k - 1];
				}

				gene[i].city_name[j] = city_name2;
			}
		}
	}

	// 距離の計算
	void distance(void)
	{
		double x, x2, y, y2;

		for (int i = 0;i < GENE;i++)
		{

			gene[i].distance = 0.0; // gene[i].distanceの初期化
			for (int j = 0;j < BIT - 1;j++)
			{
				x = city_x[gene[i].city_name[j] - 65];

				y = city_y[gene[i].city_name[j] - 65];
				x2 = city_x[gene[i].city_name[j + 1] -65];

				y2 = city_y[gene[i].city_name[j + 1] -65];

				gene[i].distance += sqrt(pow((double)x2 - (double)x, 2) + pow((double)y2 - (double)y, 2));
			}

			x = x2;
			y = y2;
			x2 = city_x[gene[i].city_name[0] - 65];
			y2 = city_y[gene[i].city_name[0] - 65];
			gene[i].distance += sqrt(pow((double)x2 -(double)x, 2) + pow((double)y2 - (double)y, 2));
		}
	}

	// ソート
	void sort(int generation)
	{
		for (int i = 0;i < GENE - 1;i++) 
		{
			for (int j = i + 1;j < GENE;j++) 
			{
				// gene[i]とgene[j]を交換
				if (gene[i].distance > gene[j].distance) 
				{
					gene2 = gene[i]; // gene[i]を一時的にgene2に退避させておく

					gene[i] = gene[j]; //gene[i]にgene[j]を移す

					gene[j] = gene2; // gene2に退避させておいたgene[i]をgene[j]に移す
				}

			}

		}
		for (int i = 0;i < BIT;i++) 
		{
			elite[generation].city_name[i] = gene[0].city_name[i];
		}

		elite[generation].distance = gene[0].distance;
	}

	// 交叉
	void crossover(void)
	{
		int point /*交差する位置*/, random_gene1 /*1つ目の親個体*/, random_gene2 /*2つ目の親個体*/, bit2 /*入れ物*/;

		for (int i = 0;i < (int)(GENE * CROSSOVER);i++) 
		{
			point = rand() % (BIT - 2) + 2; // 交伹する位置をランダムで選択
			random_gene1 = rand() % (GENE - 1) + 1; // 1つ目の親個体

				do
				{
					random_gene2 = rand() % (GENE - 1) + 1; //2つ目の親個体
				} while (random_gene2 == random_gene1); //random_gene1とrandom_gene2が同じだったらrandom_gene2を選択し直し

						// 交伹
						for (int j = point;j < BIT;j++) 
						{
							bit2 = gene[random_gene1].bit[j];
							gene[random_gene1].bit[j] =gene[random_gene2].bit[j];

							gene[random_gene2].bit[j] = bit2;
						}

		}

	}
	// 突然変異
	void mutation(void)
	{
		int random_bit/*突然変異させる遺伝子*/, random_gene/*突然変異させる個体*/, mutation_bit/*突然変異後の値*/;

		for (int i = 0;i < (int)(GENE * MUTATION);i++) {
			random_bit = rand() % (BIT - 2) + 1; // 突然変異させる遺伝子をランダムで選択
			random_gene = rand() % (GENE - 1) + 1; // 突然変異させる個体をランダムで選択

				do 
				{
					mutation_bit = rand() % (BIT - random_bit) + random_bit;

				}while (gene[random_gene].bit[random_bit] == mutation_bit); // 元の遺伝子とは違うものが出るまで繰り返し

				gene[random_gene].bit[random_bit] = mutation_bit;

		}

	}
	// 逆位
	void inverse(void)
	{
		int random_bit1/*逆位の範囲の最初の遺伝子*/, random_bit2/*逆位の範囲の最後の遺伝子*/, random_gene/*逆位させる個体*/, center, NAME[BIT];
		char city_name2;

		for (int i = 0;i < (int)(GENE * INVERSE);i++) {

			random_bit1 = rand() % (BIT - 2) + 1; // 逆位始めの遺伝子をランダムで選択

			random_bit2 = rand() % (BIT - (random_bit1 + 1)) + random_bit1 + 1; // 逆位終わりの遺伝子をランダムで選択

			center = (random_bit2 - random_bit1 + 1) / 2; // 逆位の範囲÷2(小数点以下切り捨て)

			random_gene = rand() % (GENE - 1) + 1; // 逆位させる個体をランダムに選択

				// 逆位
				for (int j = 0;j < center;j++) 
				{
					city_name2 = gene[random_gene].city_name[random_bit1 + j];

					gene[random_gene].city_name[random_bit1 + j] = gene[random_gene].city_name[random_bit2 - j];

					gene[random_gene].city_name[random_bit2 - j] = city_name2;
				}

			for (int j = 0;j < BIT;j++)
			{
				NAME[j] = 65 + j;
			}

			// 都市順から遺伝子へ変換
			for (int j = 0;j < BIT;j++) 
			{
				for (int k = j;k < BIT;k++) 
				{
					if (gene[random_gene].city_name[j] == NAME[k]) 
					{
						gene[random_gene].bit[j] = k;
						break;
					}

				}
				// 右詰
				for (int k = gene[random_gene].bit[j];k > 0;k--) 
				{
					NAME[k] = NAME[k - 1];
				}
			}
		}
	}
	// 淘汰
	void select(void)
	{
		for (int i = 0;i < (int)(GENE * SELECT);i++) 
		{

			gene[i + (int)(GENE * (1.0 - SELECT))] = gene[i];

		}
	}

	// 世代数と巡回順を表示する
	void print(void)
	{
		printf("%3d世代目:%2.7f ", generation_count + 1, elite[generation_count].distance);



		for (int i = 0;i < BIT;i++) 
		{
			printf("%c", elite[generation_count].city_name[i]);
		}

		printf("%c\n", elite[generation_count].city_name[0]);
	}
	
	/* 一定時間ごとに実行
	void timer(int value) 
	{
		if (generation_count < GENERATION - 1) 
		{
			generation_count++;
			print();
		}
	}*/

	// メイン関数
	int main(int argc, char* argv[])
	{
		srand((unsigned)time(NULL)); // 乱数の初期化
		initial_data();
		generate_gene();
		distance();
		sort(0);

		for (int i = 1;i < GENERATION;i++)
		{
			crossover();
			mutation();
			generate_gene();
			inverse();
			distance();
			sort(i);
			select();
			sort(i);

			if (generation_count < GENERATION - 1)
			{
				generation_count++;
				print();
			}

		}

		printf("  世代数  距離       巡回順\n");
		print();

		return 0;
	}
anonymous No title
C++
#include <stdio.h>
#include <string.h>

#define N 10

struct STUDENT
{
	int num;
	float height;
	float weight;
	char name[256];
}stdlist[N];

void file_read()
{
	FILE *file;
	errno_t error;
	error = fopen_s(&file, "meibo.txt", "r");

	printf("*学生データ*\n");
	printf(" 学生番号   身長  体重  名前\n");

	for (int i = 0; i < N; i++)
	{
		fscanf_s(file, "%d,%f,%f,%s", &stdlist[i].num, &stdlist[i].height, &stdlist[i].weight, stdlist[i].name, 256);
		printf("%d |%3.1f |%3.1f |%8s\n", stdlist[i].num, stdlist[i].height, stdlist[i].weight, stdlist[i].name);
	}

	fclose(file);
	return;
}

void sort()
{
	float tmp;
	int tmp2;
	char word[10][20];

	printf("\n*身長降順*\n");
	printf(" 学生番号   身長  体重  名前\n");

	for (int i = 0; i < N; ++i) 
	{
		for (int j = i + 1; j < N; ++j) 
		{
			if (stdlist[i].height < stdlist[j].height)
			{
				tmp = stdlist[i].height;
				stdlist[i].height = stdlist[j].height;
				stdlist[j].height = tmp;

				tmp2 = stdlist[i].num;
				stdlist[i].num = stdlist[j].num;
				stdlist[j].num = tmp2;

				tmp = stdlist[i].weight;
				stdlist[i].weight = stdlist[j].weight;
				stdlist[j].weight = tmp;

				strcpy_s(word[0], stdlist[i].name);
				strcpy_s(stdlist[i].name, stdlist[j].name);
				strcpy_s(stdlist[j].name, word[0]);
			}
		}
		printf("%d| %3.1f| %3.1f |%8s\n", stdlist[i].num, stdlist[i].height, stdlist[i].weight, stdlist[i].name);
	}

	printf("\n*体重降順*\n");
	printf(" 学生番号   身長  体重  名前\n");

	for (int i = 0; i < N; ++i)
	{
		for (int j = i + 1; j < N; ++j)
		{
			if (stdlist[i].weight < stdlist[j].weight)
			{
				tmp = stdlist[i].height;
				stdlist[i].height = stdlist[j].height;
				stdlist[j].height = tmp;

				tmp2 = stdlist[i].num;
				stdlist[i].num = stdlist[j].num;
				stdlist[j].num = tmp2;

				tmp = stdlist[i].weight;
				stdlist[i].weight = stdlist[j].weight;
				stdlist[j].weight = tmp;

				strcpy_s(word[0], stdlist[i].name);
				strcpy_s(stdlist[i].name, stdlist[j].name);
				strcpy_s(stdlist[j].name, word[0]);
			}
		}
		printf("%d |%3.1f |%3.1f |%8s\n", stdlist[i].num, stdlist[i].height, stdlist[i].weight, stdlist[i].name);
	}

	return;
}

int main()
{
	file_read();
	sort();

	return 0;
}
anonymous No title
C++
// キューの大きさ
#define QUEUE_LENGTH 16

// 通常のキュー
QueueHandle_t xQueue;

TaskHandle_t taskHandle;

void task1(void *pvParameters) {
  while (1) {
    uint32_t data;
    if (xQueueReceive(xQueue, &data, 0) == pdTRUE) {
      // キューを受信するまで待つ
      Serial.printf("data = %d\n", data);
    }
    delay(1);
  }
}

void setup() {
  Serial.begin(115200);
  delay(100);

  // キュー作成
  xQueue = xQueueCreate(QUEUE_LENGTH, sizeof(uint8_t));

  // Core1の優先度1で通知受信用タスク起動
  xTaskCreateUniversal(
    task1,
    "task1",
    8192,
    NULL,
    1,
    &taskHandle,
    APP_CPU_NUM
  );
}

int i = 0;
void loop() {
  i++;
  if (i % 10 == 0) {
    uint32_t data = i;
    uint8_t ret;

    // キューを送信
    ret = xQueueSend(xQueue, &data, 0);
  }

  delay(100);
}
anonymous No title
C++
/*
 copy folder and its contents.
 dest: destination directory
 source: source directory
 hwnd: window handle of caller. Security attribute is the same of hwnd owner.
 return: 0 if succeeded. otherwise non zero value.
*/
int copy_folder(const String& path_dest, const String& path_source, HWND hwnd) {
    SHFILEOPSTRUCT tSHFile = { 0 };
    tSHFile.hwnd = (hwnd == NULL) ? ::GetDesktopWindow() : hwnd;
    tSHFile.wFunc = FO_COPY;
    tSHFile.fFlags = FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
    tSHFile.fAnyOperationsAborted = TRUE;
    tSHFile.hNameMappings = NULL;
    tSHFile.lpszProgressTitle = _T("");

    // add \0\0 to the tail of folder name.
    // std::vector is used because std::string's operand+ can not add \0 (\0 is ignored).
    std::vector<TCHAR> pfrom(path_source.size() + 2);
    _tcscpy_s(pfrom.data(), path_source.size() + 2, path_source.c_str());
    pfrom[pfrom.size() - 1] = _T('\0');
    tSHFile.pFrom = pfrom.data();

    std::vector<TCHAR> pto(path_dest.size() + 2);
    _tcscpy_s(pto.data(), path_dest.size() + 2, path_dest.c_str());
    pto[pto.size() - 1] = _T('\0');
    tSHFile.pTo = pto.data();

    return SHFileOperation(&tSHFile);
}
anonymous No title
C++
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
bool hlflag;
static int field[8][8][8];
static bool piece[512];
static int xlength,ylength,usefnum,usepnum,useppow,clistnum;
static int zlength;
static int clist[20000][4][3];
static vector<int> useclist[8][8][8];
static stack<int> puthis[4];
static int lputhis[5];
static vector<int> reachnum;
static void putpiece(int z,int y,int x,int nowhave){
    field[z][y][x] = nowhave;
    puthis[0].push(z);
    puthis[1].push(y);
    puthis[2].push(x);
    puthis[3].push(nowhave);
    piece[nowhave] = false;
}
static void deletepiece(){
    lputhis[4] = lputhis[3];
    for(int i = 0;i<4;i++){
        lputhis[i] = puthis[i].top();
        puthis[i].pop();
    }
    field[lputhis[0]][lputhis[1]][lputhis[2]] = -1;
    piece[lputhis[3]] = true;
}
static void shitazyunbi(){
    for(int i = 0;i<512;i++){
        piece[i] = true;
    }
    for(int i = 0;i<5;i++){
        lputhis[i] = -1;
    }
    usefnum = xlength*ylength*zlength;
    usepnum = 1;
    useppow = 0;
    while(usepnum<usefnum){
        usepnum += usepnum;
        useppow++;
    }
    for(int i = 0;i<useppow*2;i++){
        reachnum.push_back(0);
    }
}
static void makeclist(){
    int nowlistnum = 0;
    for(int i1 = 0;i1<zlength;i1++){
        for(int i2 = 0;i2<ylength;i2++){
            for(int i3 = 0;i3<xlength;i3++){
                if(i3<xlength-3){
                    for(int i4 = 0;i4<4;i4++){
                        clist[nowlistnum][i4][0] = i1;
                        clist[nowlistnum][i4][1] = i2;
                        clist[nowlistnum][i4][2] = i3+i4;
                        useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                    }
                    nowlistnum++;
                    if(i2<ylength-3){
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1;
                            clist[nowlistnum][i4][1] = i2+i4;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1;
                            clist[nowlistnum][i4][1] = i2+3-i4;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                        if(i1<zlength-3){
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+i4;
                                clist[nowlistnum][i4][1] = i2+i4;
                                clist[nowlistnum][i4][2] = i3+i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+3-i4;
                                clist[nowlistnum][i4][1] = i2+i4;
                                clist[nowlistnum][i4][2] = i3+i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+i4;
                                clist[nowlistnum][i4][1] = i2+3-i4;
                                clist[nowlistnum][i4][2] = i3+i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+i4;
                                clist[nowlistnum][i4][1] = i2+i4;
                                clist[nowlistnum][i4][2] = i3+3-i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                        }
                    }
                }
                if(i2<ylength-3){
                    for(int i4 = 0;i4<4;i4++){
                        clist[nowlistnum][i4][0] = i1;
                        clist[nowlistnum][i4][1] = i2+i4;
                        clist[nowlistnum][i4][2] = i3;
                        useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                    }
                    nowlistnum++;
                    if(i1<zlength-3){
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+i4;
                            clist[nowlistnum][i4][1] = i2+i4;
                            clist[nowlistnum][i4][2] = i3;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;   
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+3-i4;
                            clist[nowlistnum][i4][1] = i2+i4;
                            clist[nowlistnum][i4][2] = i3;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;          
                    }
                }
                if(i1<zlength-3){
                    for(int i4 = 0;i4<4;i4++){
                        clist[nowlistnum][i4][0] = i1+i4;
                        clist[nowlistnum][i4][1] = i2;
                        clist[nowlistnum][i4][2] = i3;
                        useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                    }
                    nowlistnum++;
                    if(i3<xlength-3){
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+i4;
                            clist[nowlistnum][i4][1] = i2;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+3-i4;
                            clist[nowlistnum][i4][1] = i2;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                    }
                }
            }
        }
    }
    if(hlflag){
        for(int i1 = 0;i1<3;i1++){
            for(int i2 = 0;i2<3;i2++){
                for(int i4 = 0;i4<4;i4++){
                    clist[nowlistnum][i4][0] = 0;
                    clist[nowlistnum][i4][1] = i1+(i4%2);
                    clist[nowlistnum][i4][2] = i2+(i4/2);
                    useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                }
                nowlistnum++;
            }
        }
    }
    clistnum = nowlistnum;
}
static vector<int> binkai(int a){
    vector<int> ret; 
    for(int i = 0;i<useppow;i++){
        ret.push_back(-1);
    }
    int cnt = 0;
    while(a>0){
        ret[cnt]=a%2+a%2-1;
        a/=2;
        cnt++;
    }
    return ret;
}
static int firstcheck(int nowhave){
    int wlist = -1;
    for(int i1 = clistnum-1;i1>-1;i1--){
        int nowlist = i1;
        vector<int> binnhave = binkai(nowhave);
        vector<int> sum(useppow);
        for(int i = sum.size()-1;i>-1;i--){
            sum[i] = 0;
        }
        bool spaceflag = false;
        for(int i2 = 0;i2<4;i2++){
            int cpiece = field[clist[nowlist][i2][0]][clist[nowlist][i2][1]][clist[nowlist][i2][2]];
            if(cpiece != -1){
                vector<int> tmp = binkai(cpiece);
                for(int i3 = 0;i3<useppow;i3++){
                    sum[i3] += tmp[i3];
                }
            }else{
                if(spaceflag){
                    break;
                }
                spaceflag = true;
            }
        }
        if(!spaceflag){
            continue;
        }
        for(int i2 = 0;i2<useppow;i2++){
            if(sum[i2] == 3){
                reachnum[i2+i2+1]++;
                if(binnhave[i2] == 1){
                    wlist = nowlist;
                }
            }
            if(sum[i2] == -3){
                reachnum[i2+i2]++;
                if(binnhave[i2] == -1){
                    wlist = nowlist;
                }
            }
        }
    }
    return wlist;
}
static vector<int> recheck(int z,int y,int x){
    vector<int> kaeriti(useppow*2);
    for(int i1 = useclist[z][y][x].size()-1;i1>-1;i1--){
        int nowlist = useclist[z][y][x][i1];
        vector<int> 
anonymous タッチとGPIOを同時に使う方法
C++
volatile uint32_t touchedTime = 0;

void IRAM_ATTR gotTouch() {
  touchedTime = millis();
}

void setup() {
  Serial.begin(115200);
  delay(50);

  touchAttachInterrupt(33, gotTouch, 10);
  pinMode(33, OUTPUT);
}

void loop() {
  if (millis() < touchedTime + 50) {
    digitalWrite(33, HIGH);
  } else {
    digitalWrite(33, LOW);
  }
  delay(1);
}
anonymous No title
C++
#include <M5StickC.h>

void setup() {
  M5.begin();
  M5.Lcd.fillScreen(BLACK);

  Wire1.beginTransmission(0x34);
  Wire1.write(0x33);
  Wire1.write(0x00);
  Wire1.endTransmission();
}

void loop() {
  M5.Lcd.setCursor(0, 4, 1);

  M5.Lcd.printf("AXP192 Test\n");
  M5.Lcd.printf("\n");

  M5.Lcd.printf("Battery\n");
  M5.Lcd.printf(" State:%6d\n"  , M5.Axp.GetBatState());      // バッテリーが接続されているか(常に1のはず)
  M5.Lcd.printf(" Warn :%6d\n"  , M5.Axp.GetWarningLevel());  // バッテリー残量警告 0:残あり, 1:残なし
  M5.Lcd.printf(" Temp :%6.1f\n", M5.Axp.GetTempInAXP192());  // AXP192の内部温度
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetBatVoltage());    // バッテリー電圧(3.0V-4.2V程度)
  M5.Lcd.printf(" I(mA):%6.1f\n", M5.Axp.GetBatCurrent());    // バッテリー電流(プラスが充電、マイナスが放電)
  M5.Lcd.printf(" W(mW):%6.1f\n", M5.Axp.GetBatPower());      // バッテリー電力(W=V*abs(I))

  M5.Lcd.printf("ASP\n");
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetAPSVoltage());    // ESP32に供給されている電圧

  M5.Lcd.printf("VBus(USB)\n");
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetVBusVoltage());   // USB電源からの電圧
  M5.Lcd.printf(" I(mA):%6.1f\n", M5.Axp.GetVBusCurrent());   // USB電源からの電流

  M5.Lcd.printf("VIN(5V-In)\n");
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetVinVoltage());    // 5V IN端子からの電圧
  M5.Lcd.printf(" I(mA):%6.1f\n", M5.Axp.GetVinCurrent());    // 5V IN端子からの電流

//  delay(1000);
}
anonymous No title
C++
int main() {
    init();
    ll N;
    cin >> N;
    string S;
    cin >> S;
    ll Q;
    cin >> Q;
    vc<set<ll>> ss(26);
    rep(i,N){
        ss[S[i] - 'a'].emplace(i);
    }

    while(Q--){
        ll q;
        cin >> q;
        if(q==1){
            ll x;
            char c;
            cin >> x >> c;
            x--;
            if (S[x] == c) continue;

            ss[S[x] - 'a'].erase(x);
            ss[c - 'a'].emplace(x);
        }else{
            ll l, r;
            cin >> l >> r;
            l--, r--;
            ll ans = 0;
            rep(i,26){
                auto t = ss[i].lower_bound(l);
                if(t==ss[i].end()){
                    continue;
                }
                else if(*t<=r)
                    ans++;
            }
            cout << ans << endl;
        }
    }
}
anonymous No title
C++
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <boost/format.hpp>
#include <random>
#include <algorithm>
using namespace std;

const int N = 4;
const int NN = N * N;
const int MaxPlace = 9;
const int Trial = 10000000;

using conf_t = vector<int>;

inline size_t pos(int i, int j){
  return i + N * j;
}

// 配置の出力用関数
inline string conf2str(const conf_t &xs){
  string ret = "";
  for(int i=0; i<N; ++i){
    ret += (boost::format("%2d %2d %2d %2d\n")
      % xs[pos(0,i)] % xs[pos(1,i)] % xs[pos(2,i)] % xs[pos(3,i)]).str();
  }
  return ret;
}

// 空の場所のリスト生成
vector<size_t> empty_place(const conf_t &xs){
  vector<size_t> ret;
  for(size_t i=0; i<xs.size(); ++i){
    if(!xs[i]) ret.push_back(i);
  }
  return ret;
}

// 空の場所をランダムに埋める
conf_t place_rnd(const conf_t &xs, vector<size_t> &empty, mt19937 &engine){
  conf_t ret(xs);
  // 空の場所のリストをシャッフル
  shuffle(empty.begin(), empty.end(), engine);
  for(size_t i=0; i<empty.size() - (NN - MaxPlace); ++i){
    ret[empty[i]] = 1;
  }
  return ret;
}

// ビンゴの数をカウント
int hantei(const conf_t &xs){
  int ret = 0;
  vector<int> sums(2*N + 2, 0);
  for(int i=0; i<N; ++i){
    for(int j=0; j<N; ++j){
      sums[j] += xs[pos(i, j)];
      sums[j + N] += xs[pos(j, i)];
    }
    sums[2*N] += xs[pos(i,i)];
    sums[2*N + 1] += xs[pos(N-1-i, i)];
  }
  for(auto x : sums){
    if(x == N) ++ret;
  }
  return ret;
}

conf_t configuration(NN), rev(NN);

int main(){
  // 乱数初期化
  std::random_device seed_gen;
  std::mt19937 engine(1983);

  // 入力を受け取る
  int nonzero = 0;
  for(int j=0; j<N; ++j){
    for(int i=0; i<N; ++i){
      size_t n;
      cin >> n;
      configuration[pos(i,j)] = n;
      if(n != 0) ++nonzero;
    }
  }
  cout << conf2str(configuration);
  cout << nonzero << endl;

  // 空の場所のリストを生成
  auto empty = empty_place(configuration);
  // 結果のヒストグラム
  vector<int> histogram(4,0);
  for(int i=0; i<Trial; ++i){
    auto placed = place_rnd(configuration, empty, engine);
    histogram[hantei(placed)] += 1;
  }
  // 結果を出力
  for(size_t i=0; i < histogram.size(); ++i){
    cout << (boost::format("%d\t%8d\t%0.4e\n") % i % histogram[i] % (histogram[i] / double(Trial))).str();
  }
  return 0;
}
Don't you submit code?
Submit