2019年6月8日 星期六
2019年1月25日 星期五
0125練習 uva11470
0125資料夾
UVA 11470題目
題意: N x N 方陣,最多10x10,算每一圈的數字和
vb版 解題參考,輸入 in.txt 輸出out.txt
UVA 11470題目
題意: N x N 方陣,最多10x10,算每一圈的數字和
vb版 解題參考,輸入 in.txt 輸出out.txt
https://uva.onlinejudge.org/external/114/11470.pdf
Public Class Form1
Dim a(10, 10) As Integer '最多 10x10
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Hide()
FileOpen(1, "in.txt", OpenMode.Input)
FileOpen(3, "out.txt", OpenMode.Output)
For fn = 1 To 1
'If fn = 2 Then PrintLine(3)
Do While True
Dim n% = LineInput(fn)
If n = 0 Then Exit Do
For i = 0 To n - 1
Dim s = LineInput(fn)
Dim dat() = s.Split("
")
For j = 0 To n - 1
a(i, j) = dat(j)
Next
Next
PrintLine(3, fxx(n))
Loop
Next
End
End Sub
Dim dr() = {0, 1, 0, -1}
Dim dc() = {1, 0, -1, 0}
' d 0右 1下 2左 3上
Function fxx(ByVal n As Integer) As String
If (n = 1) Then Return a(0, 0)
fxx = ""
Dim m = (n + 1) \ 2 '共幾圈
Dim r, c, sum, k As Integer
For i = 0 To m - 1 '第 i 圈
sum = 0 : r = i : c = i
k = n - 1 - 2 * i '每個方向幾個數字
If k = 0 Then sum = a(r, c)
For d = 0 To 3 '四個方向
For j = 1 To k '每個方向幾個數字
r += dr(d) : c += dc(d) '下一個位置座標
sum += a(r, c)
Next j
Next d
If i > 0 Then fxx &= " "
fxx &= sum
Next i
End Function
End Class
2018年12月19日 星期三
篩法、陣列的應用
篩法400.xlsx
找 2~n(<10^7)的所有質數 虛擬碼
動態陣列 p 、靜態陣列 c(n) 初值 false設為質數、true被刪記(非質數)
push (2)
for i= 3 ~ n jump 2
if not c(i) then
'//這是質數,將所有 i 的倍數刪掉
push( i ) '將 i 加入 p 陣列
for j=i*i ~ n jump i
c(j) = true ' j 被刪記
end for
end if
end for
應用題連結
1220-0 ~ 1220-3 參考解
找 2~n(<10^7)的所有質數 虛擬碼
動態陣列 p 、靜態陣列 c(n) 初值 false設為質數、true被刪記(非質數)
push (2)
for i= 3 ~ n jump 2
if not c(i) then
'//這是質數,將所有 i 的倍數刪掉
push( i ) '將 i 加入 p 陣列
for j=i*i ~ n jump i
c(j) = true ' j 被刪記
end for
end if
end for
應用題連結
1220-0 ~ 1220-3 參考解
2018年12月11日 星期二
2018年11月29日 星期四
1129C++
c/c++研習資料夾
#include <iostream>
using namespace std;
int p[ 100 ];
int pcnt;
bool isp(int x)
{
int i;
// cout << "試" << x;
if(x<2) return false;
for (i=2; i*i<=x; i++)
{// cout <<"%" << i << ',';
if (x%i==0) return false;
}
// cout << "是\n" ;
return true;
}
int main()
{
int n, i,j;
cin >> n;
pcnt =0;
for (i=1 ; i<=n; ++i)
{
if( isp( i ) )
{
p[pcnt] = i; // i=1
pcnt ++;
//cout << pcnt <<":"<<p[pcnt-1] << " ";
}
}
// 共 pcnt , p[0] ~ p[cnt-1]
cout << pcnt <<"," << p[pcnt-1] << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int p[ 100 ];
int pcnt;
bool isp(int x)
{
int i;
// cout << "試" << x;
if(x<2) return false;
for (i=2; i*i<=x; i++)
{// cout <<"%" << i << ',';
if (x%i==0) return false;
}
// cout << "是\n" ;
return true;
}
int main()
{
int n, i,j;
cin >> n;
pcnt =0;
for (i=1 ; i<=n; ++i)
{
if( isp( i ) )
{
p[pcnt] = i; // i=1
pcnt ++;
//cout << pcnt <<":"<<p[pcnt-1] << " ";
}
}
// 共 pcnt , p[0] ~ p[cnt-1]
cout << pcnt <<"," << p[pcnt-1] << endl;
system("pause");
return 0;
}
