Harigami
ログイン
Harigamiは、ログイン不要のコード共有サービスです。コードの投稿後に発行されるURLでコードを共有できます。
PythonやRubyなど一部の言語は、投稿後にオンラインで実行することもできます。
誰でも無料で使えて、広告もありません。コードレビューやスニペットの代わりに使ってみてください。


詳細設定
+
まんだげり タイトルなし
C
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
void  gravity( double  (*x)[3],  double *m, double  (*a)[3],double *p, const int n){
    int i,j;
    for(i=0; i<n; i++){
        a[i][0]=a[i][1]=a[i][2]=p[i]=0.0;
        for(j=0;j<n;j++){
            if(i==j) continue;
            double dx = x[i][0] - x[j][0];
            double dy = x[i][1] - x[j][1];
            double dz = x[i][2] - x[j][2];
            double r2 = dx*dx + dy*dy +dz*dz;
            double rinv = 1.0 / sqrt(r2);
            double mjr3inv = m[j]*rinv*rinv*rinv;
            a[i][0] -= mjr3inv * dx;
            a[i][1] -= mjr3inv * dy;
            a[i][2] -= mjr3inv * dz;
            p[i]    -= m[j] * rinv;
        }
    }
}
double  energy( double  (*v)[3],  double *m, double *p, const  int n){
  double e = 0.0;
  int i;
  for(i=0;i<n;i++){
    e += p[i] + m[i]*(v[i][0]*v[i][0]+v[i][1]*v[i][1]+v[i][2]*v[i][2])/2;  
    }
    return e;
}
void  runge2( double  (*x)[3],  double  (*v)[3],  double *m, double  (*a)[3],
    double *p, const  double dt , const  int n){
    int i,j;    
    int  nmax = 128;
    double  xtmp[nmax][3];
    double k[nmax][3], k1[nmax][3], k2[nmax][3], k3[nmax][3], k4[nmax][3];
    double l[nmax][3], l1[nmax][3], l2[nmax][3], l3[nmax][3], l4[nmax][3];
    gravity(x,m,a,p,n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            k1[i][j] = v[i][j]*dt;
            l1[i][j] = a[i][j]*dt;
            xtmp[i][j] = x[i][j] + (k1[i][j]/2);
        }
    }
    gravity(xtmp,m,a,p,n);
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            k2[i][j] = (v[i][j] + l1[i][j]*dt);
            l2[i][j] = a[i][j] * dt;
        }
    }

	for(i=0;i<n;i++){
        for(j=0;j<n;j++){
			k3[i][j] = (v[i][j] + l2[i][j]*dt);
           	l3[i][j] = a[i][j] * dt;
        }
    }
        for(j=0;j<n;j++){
            k4[i][j] = (v[i][j] + l3[i][j]*dt);
           	l4[i][j] = a[i][j] * dt;
            k[i][j] = (k1[i][j] + 2*k2[i][j]+2*k3[i][j]+k4[i][j])/2;
            l[i][j] = (l1[i][j] + 2*l2[i][j]+2*l3[i][j]+l4[i][j])/2;
            x[i][j] += k[i][j];
            v[i][j] += l[i][j];
        }
    }

int  main (){
    int nmax =128;
    double x[nmax][3];
    double v[nmax][3];
    double a[nmax][3];
    double m[nmax];
    double p[nmax];
    int n,i,j;
    double dt,tend;
    double e0;
    double tnow =0.0;
    double e;

    FILE *file1;    
    FILE *file2;    
    FILE *file3;    
    FILE *file4;        
    
    file1 = fopen("rung4_1.dat", "w");
    file2 = fopen("rung4_2.dat", "w");
    file3 = fopen("rung4_3.dat", "w");
    file4 = fopen("e_rung4.dat", "w");
    printf("input n,dt,tend,m,x,v\n");
    scanf("%d %lf %lf",&n,&dt,&tend);
    for(i=0;i<3;i++){
        scanf("%lf",&m[i]);
        for(j=0; j<3; j++){
            scanf("%lf",&x[i][j]);
        }
        for(j=0; j<3; j++){
            scanf("%lf",&v[i][j]);
        }
    }
    gravity(x,m,a,p,n);
    e0 = energy(v,m,p,n);
    /*printf("%lf\t",tnow):
    for(i=0;i<n;i++){
        for(j=0;j<3;j++){
        printf("%lf\t",x[i][j]);
    }*/
    while(tnow < tend){
    runge2(x,v,m,a,p,dt,n);
    e = energy(v,m,p,n);
    tnow += dt;
    /*printf("%lf\t",tnow):
    for(i=0;i<n;i++){
        for(j=0;j<3;j++){
        printf("%lf",x[i][j]);
    }
    printf("%lf\t",e);
    printf("%lf\t",(e-e0)/e0);*/
    
        
    fprintf(file1, " %lf %lf\n",x[0][0],x[0][1]);
    fprintf(file2, " %lf %lf\n",x[1][0],x[1][1]);
    fprintf(file3, " %lf %lf\n",x[2][0],x[2][1]);
    fprintf(file4, " %lf %lf\n",tnow,(e-e0)/e0);
    

    }
    fclose(file1);
    fclose(file2);
    fclose(file3);
    fclose(file4);  
  }
  • 0
  • 1
anonymous タイトルなし
C
#include <stdio.h>
#include <stdlib.h>
#include<math.h>

static  const  int  NMAX = 128;
static  double  xtmp[NMAX][3];
static  double k[NMAX][3], k1[NMAX][3], k2[NMAX][3], k3[NMAX][3], k4[NMAX][3];
static  double l[NMAX][3], l1[NMAX][3], l2[NMAX][3], l3[NMAX][3], l4[NMAX][3];

void  gravity( double  (*x)[3],  double *m, double  (*a)[3],double *p, const int n){
	int i,j;
	for(i=0; i<n; i++){
		a[i][0]=a[i][1]=a[i][2]=p[i]=0.0;
		for(j=0;j<n;j++){
			if(i==j) continue;
			double dx = x[i][0] - x[j][0];
			double dy = x[i][1] - x[j][1];
			double dz = x[i][2] - x[j][2];
			double r2 = dx*dx + dy* +dz*dz;
			double rinv = 1.0 / sqrt(r2);
			double mjr3inv = m[j]*rinv*rinv*rinv;
			a[i][0] -= mjr3inv * dx;
			a[i][1] -= mjr3inv * dy;
			a[i][2] -= mjr3inv * dz;
			p[i]    -= m[j] * rinv;
		}
	}
}



double  energy( double  (*v)[3],  double *m, double *p, const  int n){

  double e = 0.0;
  int i;
  for(i=0;i<n;i++){
	e += p[i] + m[i]*(v[i][0]*v[i][0]+v[i][1]*v[i][1]+v[i][2]*v[i][2])/2;  
	}
	return e;
}


void  runge2( double  (*x)[3],  double  (*v)[3],  double *m, double  (*a)[3],
	double *p, const  double dt , const  int n){
	int i,h;
	gravity(x,m,a,p,n);
	for(i=0;i<n;i++){
		for(h=0;h<3;h++){
			k1[i][h] = v[i][h]*dt;
			l1[i][h] = a[i][h]*dt;
			xtmp[i][h] = x[i][h] + (k[i][h]/2);
		}
	}

	gravity(xtmp,m,a,p,n);
	for(i=0;i<n;i++){
		for(h=0;h<3;h++){
			k2[i][h] = (v[i][h] + l1[i][h]*dt);
			l2[i][h] = a[i][h] * dt;
		}
	}

	for(i=0;i<n;i++){
		for(h=0;h<3;h++){
			k[i][h] = (k1[i][h] + k2[i][h])/2;
			k[i][h] = (k1[i][h] + k2[i][h])/2;
			x[i][h] += k[i][h];
			v[i][h] += l[i][h];
			}
	}
}


int  main (){

	double x[NMAX ][3];
	double v[NMAX ][3];
	double a[NMAX ][3];
	double m[NMAX];
	double p[NMAX];
	int n,i,j;
	double dt,tend;
	double e0;
	double tnow =0.0;
	double e;
	FILE *file1;	
	FILE *file2;	
	FILE *file3;	
	FILE *file4;		
	
	file1 = fopen("rung2_1.dat", "w");
	file2 = fopen("rung2_2.dat", "w");
	file3 = fopen("rung2_3.dat", "w");
	file4 = fopen("e_rung2.dat", "w");


	printf("input n,dt,tend,m,x,v");
	scanf("%d,%lf,%lf",n,dt,tend);
	for(i=0;i<n; i++){
		scanf("%lf",m[i]);
		for(j=0; j<3; j++){
			scanf("%lf",x[i][j]);
		}
		for(j=0; j<3; j++){
			scanf("%lf",v[i][j]);
		}
	}
	gravity(x,m,a,p,n);
	e0 = energy(v,m,p,n);

	/*printf("%lf\t",tnow):
	for(i=0;i<n;i++){
		for(j=0;j<3;j++){
		printf("%lf\t",x[i][j]);
	}*/
	while(tnow < tend){

	runge2(x,v,m,a,p,dt,n);
	e = energy(v,m,p,n);
	tnow += dt;

	/*printf("%lf\t",tnow):
	for(i=0;i<n;i++){
		for(j=0;j<3;j++){
		printf("%lf",x[i][j]);
	}
	printf("%lf\t",e);
	printf("%lf\t",(e-e0)/e0);*/
	
		
	fprintf(file1, " %lf %lf\n",x[0][0],x[0][1]);
    	fprintf(file2, " %lf %lf\n",x[1][0],x[1][1]);
	fprintf(file3, " %lf %lf\n",x[2][0],x[2][1]);
	fprintf(file4, " %lf %lf\n",tnow,(e-e0)/e0);
	
	fclose(file1);
	fclose(file2);
	fclose(file3);
	fclose(file4);	

	}
	

}
  • 0
  • 1
anonymous タイトルなし
Python
//@version=4
strategy(title="いるまてぃっく", overlay=true)

//source(平均足)
isHA = input(true, "平均足を使用する。", input.bool)
Hdata = isHA ? heikinashi(syminfo.tickerid) : syminfo.tickerid
HAo = security(Hdata, timeframe.period, open)
HAh = security(Hdata, timeframe.period, high)
HAl = security(Hdata, timeframe.period, low)
HAc = security(Hdata, timeframe.period, close)

HAhlc = (HAh + HAl + HAc)/3
HAup = HAo < HAc
HAdw = HAo > HAc

source = HAc

//Setting
len = input(title=' 期間', type=input.resolution, defval="60")
res = input(defval=false, title="チェック=MTF表示")
malt = input(1, title='シグマ', step=1, minval=1, maxval=50)
res2 = res ? len : timeframe.period
dev = security(Hdata, res2, stdev(source, 20))
basis = security(Hdata, res2, sma(source, 20))
useTrueRange = input(true)
length = input(20, minval=1)

//ロジック
upper = basis + dev * malt
lower = basis - dev * malt
ma = sma(source, length)
range = useTrueRange ? max(HAh - HAl, abs(HAh - HAc[1]), abs(HAl - HAc[1])) : HAh - HAl
rangema = sma(range, length)
upper2 = ma + rangema * malt
lower2 = ma - rangema * malt
basis2 = avg(upper2, lower2)

ures = security(Hdata, res2, upper2)
lres = security(Hdata, res2, lower2)
bres = security(Hdata, res2, basis2)
sig = upper > ures

//ENTRY条件
LONG = basis[1]<basis and sig
SHORT = basis[1]>basis and sig
strategy.entry("LONG", true, when=LONG)
strategy.entry("SHORT", false, when=SHORT)

//表示
HAcol = HAc > HAo ? color.green : color.red
plot_HAo = isHA ? HAo :na
plot_HAh = isHA ? HAh :na
plot_HAl = isHA ? HAl :na
plot_HAc = isHA ? HAc :na
plotcandle(plot_HAo, plot_HAh, plot_HAl, plot_HAc, "平均足", HAcol, color.black)
  • 0
  • 1
anonymous タイトルなし
Python
//@version=4
strategy(title="いるまてぃっく", overlay=true)

//source(平均足)
isHA = input(true, "平均足を表示する", input.bool)
Hdata = isHA ? heikinashi(syminfo.tickerid) : syminfo.tickerid
HAo = security(Hdata, timeframe.period, open)
HAh = security(Hdata, timeframe.period, high)
HAl = security(Hdata, timeframe.period, low)
HAc = security(Hdata, timeframe.period, close)

HAhlc = (HAh + HAl + HAc)/3
HAup = HAo < HAc
HAdw = HAo > HAc

source = HAc

//Setting
len = input(title=' 期間', type=input.resolution, defval="60")
res = input(defval=false, title="チェック=MTF表示")
malt = input(1, title='シグマ', step=1, minval=1, maxval=50)
res2 = res ? len : timeframe.period
dev = security(Hdata, res2, stdev(source, 20))
basis = security(Hdata, res2, sma(source, 20))
useTrueRange = input(true)
length = input(20, minval=1)

//ロジック
upper = basis + dev * malt
lower = basis - dev * malt
ma = sma(source, length)
range = useTrueRange ? tr : HAh - HAl
rangema = sma(range, length)
upper2 = ma + rangema * malt
lower2 = ma - rangema * malt
basis2 = avg(upper2, lower2)

ures = security(Hdata, res2, upper2)
lres = security(Hdata, res2, lower2)
bres = security(Hdata, res2, basis2)
sig = upper > ures

//ENTRY条件
LONG = basis[1]<basis and sig
SHORT = basis[1]>basis and sig
strategy.entry("LONG", true, when=LONG)
strategy.entry("SHORT", false, when=SHORT)

//表示
HAcol = HAc > HAo ? color.green : color.red
plotcandle(HAo, HAh, HAl, HAc, "平均足", HAcol, color.black)
  • 0
  • 0
LC23 タイトルなし
Python
//@version=4
strategy("MTF Stochastic Slow ", overlay=false)
len = input(title='時間軸', type=input.resolution, defval="60")
length = input(14, minval=1)
res = input(defval=true, title="チェック=MTF表示")
res2 = res ? len : timeframe.period
OverBought = input(80)
OverSold = input(20)
smoothK = 3
smoothD = 3
k = security(syminfo.tickerid, res2, sma(stoch(close, high, low, length), smoothK))
d = security(syminfo.tickerid, res2, sma(k, smoothD))
plot(OverBought, color=color.red, style=3)
plot(OverSold, color=color.lime, style=3)
plot(k, color=color.aqua)
plot(d, color=color.purple)
  • 0
  • 1
Programmerryoki 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold In Python Brute Force
Python
class Solution:
    def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
        for n in range(1, min(len(mat), len(mat[0]))+1):
            done = False
            for b in range(len(mat)-n+1):
                for a in range(len(mat[0])-n+1):
                    s = sum([sum(mat[i][a:a+n]) for i in range(b,b+n)])
                    if s <= threshold:
                        done = True
                        break
                if done:
                    break
            else:
                return n-1
        return min(len(mat),len(mat[0]))
  • 0
  • 1
Kohei Arai LeetCode 1292 Maximum Side Length of a Square with Sum Less than or Equal to Threshold
Java
class Solution {
    public int maxSideLength(int[][] mat, int threshold) {
		if (mat == null || mat.length == 0 || mat[0].length == 0) {
			return 0;
		}
		
		int M = mat.length;
		int N = mat[0].length;
		
		// Precompute the sum
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				mat[i][j] = getAt(mat, i, j) + getAt(mat, i - 1, j) + getAt(mat, i, j - 1) - getAt(mat, i - 1, j - 1);
			}
		}
		
		int ans = 0;
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				for (int k = 1; i + k < M && j + k < N; k++) {
					int area = getAt(mat, i + k, j + k) - getAt(mat, i + k, j - 1) - getAt(mat, i - 1, j + k) + getAt(mat, i - 1, j - 1);
					if (area <= threshold) {
						ans = Math.max(ans, k + 1);
					}
				}
			}
		}
		
		return ans;
    }

	public int getAt(int[][] mat, int i, int j) {
		if (i < 0 || j < 0) return 0;
		return mat[i][j];
	}

}
  • 0
  • 0
anonymous タイトルなし
Python
sdfaf
  • 0
  • 1
anonymous タイトルなし
Python
N=int(input())

def solve(num):#桁和の計算
    return num+sum([int(x) for x in str(num)])

dp=[0]*(N+1)
dp[N]=True #Nは当然成り立つ

for num in range(N-1,0,-1):#後からやる!
    a=solve(num)
    if a>N:
        dp[num]=False
    elif a==N:
        dp[num]=True
    else:
        dp[num]=dp[a]

print(sum(dp))

"""
13行目がこのコードのキモ
例えば、N=2019の場合、

num=2018 → solve(num)>Nなので、dp[num]=False
num=2013 → solve(num)==Nなので、dp[num]=True
num=2010 → solve(num)=2013 なので、計算を続ければTrueになる
num=2008 → solve(num)=2018なので、計算を続けるとFalseになる

つまり、solve(num)の数字で成り立つならば、numでも成り立つし、逆もしかり。
→solve(num)はnumよりも必ず大きいので、もうすでに計算してある!!
"""
  • 0
  • 0
anonymous タイトルなし
Python
N=int(input())

def solve(num):# 桁和の計算
    return num+sum([int(x) for x in str(num)])

#numを計算した時に、Nになるかならないか(True/False)
dp=[-1]*(N+1)

def dfs(num): 

    #dp[num]にTrueかFalseがメモられてたら、
    #どうせ2回目以降も計算結果はおんなじだから、その値を返す
    if dp[num]!=-1: 
        return dp[num]


    a=solve(num)
    if a==N:
        return True
    elif a>N:
        return False

        #aがNより小さい → まだ希望がある!
        #aついて成り立つかどうかを調べる
        #aについて成り立つ(Trueが返ってくる)→numについても成り立つ(Trueを返す)
        #aについて成り立たない(Falseが返ってくる)→numについても成り立たない(Falseを返す)
        
    result=dfs(a)
    dp[num]=result#dfs(a)の結果=dfs(num)の結果
    return result 

count=0

for start in range(1,N+1):
    if dfs(start):#startから始めて成り立つかどうか?
        count+=1

print(count)
  • 0
  • 0