//2+4+6+... 遞迴版
#include <iostream>
using namespace std;
int sum=0;
int f(int s,int e,int a)
{
if (s>e)
return sum;
else
return s+f(s+a,e,a);
}
int main()
{
cout << f(2,100,2)<< endl;
}
標籤雲
2014年12月18日 星期四
2+4+6+... 遞迴版
2014年11月29日 星期六
c++ 自建 split 函式
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
void split(string str,string word[])
{
istringstream iss(str);
int i=0;
while(iss >> word[i])
i++;
}
int main()
{
string str = "220.70.76.1";
replace( str.begin(), str.end(), '.', ' ');
string word[30];
split(str,word);
int i=0;
while(word[i]!="")
{
cout << word[i] << '\n';
i++;
}
}
2014年10月30日 星期四
stringstream 字串處理
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
// 字串轉數字
// stringstream ss;
// string str1="123";
// int n1;
// ss << str1;
// ss >>n1;
// cout << n1 << endl;
// 數字轉字串
//stringstream ss;
//int n1= 123;
//string str1;
//ss << n1;
//ss>> str1;
//cout << str1 << endl;
//取字串中數字
string str2="a3b5c";
string str3="";
for (int i=0;i<str2.size();i++)
if (str2[i]>='1' && str2[i]<='9')
str3 = str3 + str2[i];
stringstream ss;
int n;
ss <<str3;
ss >> n;
cout << n*10 <<endl;
}
2014年10月28日 星期二
小數點處理
2014年6月18日 星期三
powerpoint VBA -- 自動調整位置
Public Sub test()
For Each sl In ActivePresentation.Slides
sl.Shapes(1).Top = 40
sl.Shapes(1).Left = 50
sl.Shapes(2).Top = 150
sl.Shapes(2).Left = 50
Next
End Sub
2014年6月17日 星期二
dp - 最大連續元素和
#include <iostream>
using namespace std;
int main()
{
int n;
int a[100];
while (cin>>n)
{
for (int i=0;i<n;i++)
cin >> a[i];
int sMax = 0;
for (int i=0;i<n;i++)
{
int curSum=a[i];
for (int j=i+1;j<n;j++)
{
curSum = curSum + a[j];
if (sMax < curSum)
sMax = curSum;
}
}
cout << sMax<<endl;
}
return 0;
}
2014年6月16日 星期一
動態規劃
#include <cstdlib>
#include <iostream>
using namespace std;
long long int t[100];
long long int f(long long int n)
{
if (t[n]!=0) return t[n];
if (n <=2)
{
t[n] = n;
return t[n];
}
else
{
t[n] = f(n-1)+f(n-2);
return t[n];
}
}
int main()
{
memset(t,0,100*sizeof(t[0]));
long long int n;
while (cin >> n)
cout << f(n) << ' ' << f(f(n)%n) << endl;
}
1000 !
#include <cstdlib>
#include <iostream>
using namespace std;
int a[3000];
int n_to_array(int n)
{
int j=0;
while (n>0)
{
a[j] = n%10;
n = n/10;
j++;
}
}
int array_mul(int n)
{
int c[3000];
memset(c,0,3000*sizeof(c[0]));
for (int i=0;i<3000;i++)
{
if (i==0)
a[i] = a[i] * n;
else
a[i] = a[i] * n+c[i-1];
c[i] = a[i]/10;
a[i] = a[i]%10;
}
}
int out_array(int *a)
{
int i=3000-1;
while (a[i] == 0)
i--;
while (i>=0)
{
cout << a[i];
i--;
}
cout << endl;
}
int main()
{
int n;
while ( cin >> n)
{
memset(a,0,3000*sizeof(a[0]));
a[0]=1;
//n_to_array(1);
for (int i=1;i<=n;i++)
array_mul(i);
out_array(a);
}
//system("PAUSE");
return EXIT_SUCCESS;
}
2014年6月10日 星期二
大數乘法
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
int mul(int *n1,int *n2,int *n3)
{
for (int i=0;i<100;i++)
{
for (int j=0;j<100;j++)
{
n3[i+j] =n3[i+j]+ n1[j] * n2[i];
if (n3[i+j]>9)
{
n3[i+j+1] =n3[i+j+1]+ n3[i+j]/10;
n3[i+j]=n3[i+j]%10;
}
}
}
}
int toBig(string s,int *n)
{
int x=0;
for (int i=s.length()-1 ;i>=0;i--)
{
n[x]=s[i]-'0';
x++;
}
}
int outResult(int *n)
{
int k=99;
while (n[k]==0)
k--;
for (int i=k;i>=0;i--)
cout << n[i];
cout << endl;
}
int main()
{
string s1,s2;
int n1[100], n2[100], n3[100];
memset(n1,0,100*sizeof(n1[0]));
memset(n2,0,100*sizeof(n1[0]));
memset(n3,0,100*sizeof(n1[0]));
cin >> s1>>s2;
toBig(s1,n1);
toBig(s2,n2);
mul(n1,n2,n3);
outResult(n3);
system("PAUSE");
return EXIT_SUCCESS;
}
大數乘法
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s1,s2;
int n1[100], n2[100], n3[100];
memset(n1,0,100*sizeof(n1[0]));
memset(n2,0,100*sizeof(n1[0]));
memset(n3,0,100*sizeof(n1[0]));
cin >> s1>>s2;
int x=0;
for (int i=s1.length()-1 ;i>=0;i--)
{
n1[x]=s1[i]-'0';
x++;
}
x=0;
for (int i=s2.length()-1 ;i>=0;i--)
{
n2[x]=s2[i]-'0';
x++;
}
for (int i=0;i<100;i++)
{
for (int j=0;j<100;j++)
{
n3[i+j] =n3[i+j]+ n1[j] * n2[i];
if (n3[i+j]>9)
{
n3[i+j+1] =n3[i+j+1]+ n3[i+j]/10;
n3[i+j]=n3[i+j]%10;
}
}
}
int k=99;
while (n3[k]==0)
k--;
for (int i=k;i>=0;i--)
cout << n3[i];
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
大數加法
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s1,s2;
int n1[100], n2[100], n3[100];
memset(n1,0,100*sizeof(n1[0]));
memset(n2,0,100*sizeof(n1[0]));
memset(n3,0,100*sizeof(n1[0]));
cin >> s1>>s2;
int x=0;
for (int i=s1.length()-1 ;i>=0;i--)
{
n1[x]=s1[i]-'0';
x++;
}
x=0;
for (int i=s2.length()-1 ;i>=0;i--)
{
n2[x]=s2[i]-'0';
x++;
}
for (int i=0;i<100;i++)
{
n3[i] =n3[i]+ n2[i] + n1[i];
if (n3[i]>9)
{
n3[i+1] =n3[i+1]+ n3[i]/10;
n3[i]=n3[i]%10;
}
}
int k=99;
while (n3[k]==0)
k--;
for (int i=k;i>=0;i--)
cout << n3[i];
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
stringstream
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- int main()
- {
- string line;
- stringstream ss;
- string a;
- bool isFound = false;
- int i = 0;
- int findL;
- int findN;
- while (i<3 )
- {
- getline(cin, line);
- ss.clear();
- ss.str(line);
- int j = 1;
- while (1)
- {
- ss >> a;
- if (ss.fail()) break;
- if (a == "BILL")
- {
- isFound = true;
- findN = j;
- findL = i + 1;
- }
- j++;
- }
- i++;
- }
- if (isFound == true)
- cout << findL << ' ' << findN << endl;
- else
- cout << "NO" << endl;
- return 0;
- }
sstream
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- int main()
- {
- string line;
- stringstream ss;
- int sum[3],a;
- for (int i = 0; i < 3; i++)
- {
- getline(cin, line);
- ss.clear();
- ss.str(line);
- sum[i] = 0;
- while (1)
- {
- ss >> a;
- if (ss.fail()) break;
- sum[i] += a;
- }
- //cout << sum[i] << endl;
- }
- int iMax = 0;
- for (int i = 1; i < 3;i++)
- if (sum[i]> sum[iMax])
- iMax = i;
- cout << iMax + 1 << ' '<< sum[iMax] << endl;
- return 0;
- }
2014年6月9日 星期一
cin.getline
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- char line1[100];
- char line2[100];
- char line3[100];
- while (cin.getline(line1,100) )
- {
- cin.getline(line2, 100);
- cin.getline(line3, 100);
- cout << line3 << endl;
- cout << line1 << endl;
- cout << line2<< endl;
- }
- return 0;
- }
string to int
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string s1,s2,s3,s1a="",s2a="",s3a="";
cin >> s1>>s2>>s3;
for (int i=0;i<s1.length();i++)
if (s1[i]>='0' && s1[i]<='9')
s1a = s1a + s1[i];
for (int i=0;i<s2.length();i++)
if (s2[i]>='0' && s2[i]<='9')
s2a = s2a + s2[i];
for (int i=0;i<s3.length();i++)
if (s3[i]>='0' && s3[i]<='9')
s3a = s3a + s3[i];
int n1,n2,n3;
istringstream(s1a) >>n1;
istringstream(s2a) >> n2;
istringstream(s3a) >>n3;
cout << n1+n2+n3<< endl;
return 0;
}
字元比對
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string rstr, mstr;
- cin >> rstr >>mstr;
- int i=1,j=1;
- bool isOk = true;
- while (i<=rstr.length() && isOk == true)
- {
- while (rstr[i]!= mstr[j] && j<=mstr.length())
- {
- j++;
- }
- if (rstr[i]==mstr[j])
- {
- i++;
- j++;
- }
- else
- isOk = false;
- }
- if (isOk == true)
- cout << "YES" << endl;
- else
- cout << "NO" << endl;
- return 0;
- }
2014年6月5日 星期四
2014年6月4日 星期三
- // 小數點處理例
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- int main()
- {
- int x1,y1,x2, y2;
- while (cin >> x1 >> y1 >> x2 >> y2)
- {
- double ans = sqrt((double)(x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
- cout.setf(ios::fixed, ios::floatfield);
- cout.precision(3);
- cout << ans << endl;
- }
- return 0;
- }
2014年5月30日 星期五
//故障計算機
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
if (n<=9)
cout <<'|' <<"_______" << n <<'|' << endl;
else if (n<=99)
cout <<'|' << "______" << n <<'|' << endl;
else if (n<=999)
cout <<'|' << "_____" << n<<'|' << endl;
else if (n<=9999)
cout <<'|' << "____" << n<<'|' << endl;
else
{
n = n%10000;
if (n<=9)
cout <<'|' <<"____000" << n <<'|' << endl;
else if (n<=99)
cout <<'|' << "____00" << n <<'|' << endl;
else if (n<=999)
cout <<'|' << "____0" << n<<'|' << endl;
else
cout <<"|____" << n<<'|' << endl;
}
}
system("PAUSE");
return 0;
}
// 大數相加、比大小
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b;
while (cin >> a >> b)
{
int ar[20], br[20], cr[20],dr[20];
memset(ar, 0, 20 * sizeof(ar[0]));
memset(br, 0, 20 * sizeof(br[0]));
memset(cr, 0, 20 * sizeof(cr[0]));
memset(dr, 0, 20 * sizeof(dr[0]));
int j = 0;
for (int i = a.length() - 1; i >= 0; i--)
{
ar[j] = a[i] - 48;
j++;
}
j = 0;
for (int i = b.length() - 1; i >= 0; i--)
{
br[j] = b[i] - 48;
j++;
}
//subtract
for (int i = 19; i >= 0; i--)
dr[i] = ar[i] - br[i];
for (int i = 0; i <= 18; i++)
if (dr[i] < 0)
{
dr[i + 1] = dr[i + 1] -1;
dr[i] = dr[i] + 10;
}
int i = 19;
while (dr[i] == 0) i--;
if (dr[i] > 0)
cout << "A ";
else if (dr[i] < 0)
cout << "B ";
else
cout << " ";
//plus
for (int i = 19; i >= 0; i--)
cr[i] = br[i] + ar[i];
for (int i = 0; i <= 18; i++)
if (cr[i] >= 10)
{
cr[i + 1] = cr[i + 1] + cr[i] / 10;
cr[i] = cr[i] % 10;
}
i = 19;
while (cr[i] == 0) i--;
while (i >= 0)
{
cout << cr[i];
i--;
}
cout << endl;
}
return 0;
}
2014年4月25日 星期五
Maze Sample
2014年4月22日 星期二
Resurive Sample
'' 求1+3+5+ + 99 = ?
'Public Class Form1
' Dim sumN = 0
' Sub sum(sN, eN, stepN)
' '判斷結束否
' If sN > eN Then Return
' '每次過程要做的事
' sumN = sumN + sN
' '往完成工作方向前進
' sum(sN + stepN, eN, stepN)
' End Sub
' Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Call sum(1, 100, 2)
' MsgBox(sumN)
' End Sub
'End Class
' 求1!+2!+3!+ + 10! = ?
'Public Class Form1
' Dim sumN = 0
' Sub sum(sN, eN, stepN)
' '判斷結束否
' If sN > eN Then Return
' '每次過程要做的事
' Dim tsum = 1
' For i = 1 To sN
' tsum = tsum * i
' Next
' sumN = sumN + tsum
' '往完成工作方向前進
' sum(sN + stepN, eN, stepN)
' End Sub
' Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Call sum(1, 5, 1)
' MsgBox(sumN)
' End Sub
'End Class
'尋找最大值
'Public Class Form1
' Dim maxValue = 0
' Dim a() = {10, 200, 100, 50, 30}
' Sub fMax(sN, eN)
' '判斷結束否
' If sN > eN Then Return
' '每次過程要做的事
' If a(sN) > maxValue Then maxValue = a(sN)
' '往完成工作方向前進
' fMax(sN + 1, eN)
' End Sub
' Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Call fMax(0, a.Length - 1)
' MsgBox(maxValue)
' End Sub
'End Class
重覆排列
2014年4月1日 星期二
Kill Process
'Kill process
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 5000
Timer1.Enabled = True
End Sub
Sub killProcess(processName)
Dim process1 As Process()
process1 = Process.GetProcessesByName(processName)
For Each proces As Process In process1
proces.Kill()
Next
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("iexplore")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Call killProcess("iexplore")
Call killProcess("mspaint")
End Sub
End Class