2013年3月29日 星期五
c++ Uva 11450
問題連結:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2445
DP問題=>背包變型
#include<iostream>
using namespace std;
int main()
{
int n,m,c,k,min,sum,max;
int table[20][21];
int T[21][201];
cin>>n;
for(int i=1;i<=n;i++)
{
sum=0;
cin>>m;
cin>>c;
for(int j=0;j<=c-1;j++)
{
min=99999;
cin>>table[j][0];
k=1;
while(k<=table[j][0])
{
cin>>table[j][k];
if(table[j][k]<=min)
min=table[j][k];
k++;
}
sum=sum+min;
}
if(sum>m)
{
cout<<"no solution"<<endl;
continue;
}
else
{
for(int b=0;b<=c;b++)
for(int a=0;a<=m;a++)
T[b][a]=0;
for(int q=1;q<=c;q++)
for(int p=1;p<=m;p++)
{
for(int o=1;o<=table[q-1][0];o++)
{
if(p>=table[q-1][o])
{
if((q==1) && (T[q-1][p-table[q-1][o]]+table[q-1][o])>=T[q][p])
T[q][p]=T[q-1][p-table[q-1][o]]+table[q-1][o];
else
if((T[q-1][p-table[q-1][o]]+table[q-1][o])>=T[q][p] && (T[q-1][p-table[q-1][o]])>0)
T[q][p]=T[q-1][p-table[q-1][o]]+table[q-1][o];
}
}
}
cout<<T[c][m]<<endl;
}
}
return 0;
}
c++ 大數加法
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int>bit1,bit2;
string input1,input2;
int length1,length2,tmp;
bool flag=0,next=0; //預先判斷要不要進位;下一次進算時把進位的數字加上去
cout<<"輸入第一個數字"<<endl;
cin>>input1;
cout<<"輸入第二個數字"<<endl;
cin>>input2;
for(int i=0;i<=input1.length()-1;i++) //把string的char一個一個存成int
bit1.push_back(input1[i]-'0');
for(int j=0;j<=input2.length()-1;j++)
bit2.push_back(input2[j]-'0');
length1=input1.length()-1;
length2=input2.length()-1;
if(length1>=length2) //當第1個字串大於第2個字串時
while(1)
{
if(length2==-1)
{
bit1[length1]=(bit1[length1]+next)%10;
if(next==1)
{
bit1.push_back(next);
for(int k=0;k<=input1.length();k++)
{
tmp=bit1[k];
bit1[k]=bit1[input1.length()];
bit1[input1.length()]=tmp;
}
}
break;
}
if((bit1[length1]+bit2[length2])>=10)
flag=1;
bit1[length1]=(bit1[length1]+bit2[length2])%10+next;
next=flag;
flag=0;
length1--;
length2--;
}
else //當第2個字串大於第1個字串時
while(1)
{
if(length1==-1)
{
bit2[length2]=(bit2[length2]+next)%10;
if(next==1)
{
bit2.push_back(next);
for(int k=0;k<=input2.length();k++)
{
tmp=bit2[k];
bit2[k]=bit2[input2.length()];
bit2[input2.length()]=tmp;
}
}
break;
}
if((bit1[length1]+bit2[length2])>=10)
flag=1;
bit2[length2]=(bit1[length1]+bit2[length2])%10+next;
next=flag;
flag=0;
length1--;
length2--;
}
if(input1.length() >= input2.length())
{
if(next==1)
for(int i=0;i<=input1.length();i++)
cout<<bit1[i];
else
for(int i=0;i<=input1.length()-1;i++)
cout<<bit1[i];
}
else
{
if(next==1)
for(int i=0;i<=input1.length();i++)
cout<<bit1[i];
else
for(int i=0;i<=input1.length()-1;i++)
cout<<bit1[i];
}
cout<<endl;
return 0;
}
c++ 簡易linking list
#include<iostream>
using namespace std;
struct link
{
int number;
link *next;
};
int main()
{
int times;
link first;
link *current;
cout<<"輸入要多大的int空間:";
cin>>times;
cout<<"請輸入數值"<<endl;
cin>>first.number;
first.next=new link;
current=first.next;
for(int i=1;i<times;i++)
{
cin>>current->number;
current->next=new link;
current=current->next;
}
current=&first;
for(int j=0;j<times;j++)
{
cout<<"第"<<j+1<<"次輸入的數值"<<current->number<<endl;
current=current->next;
}
return 0;
}
c++ 一串string type的數字,以逗號分開存成int的type
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<int>i_num;
string str="10,20,30,40,50";
string num="";
int location=-1;
for(int i=0;i<=str.length()-1;i++)
{
if(str[i]==',')
{
for(int j=location+1;j<=i-1;j++)
num=num+str[j];
i_num.push_back(atoi(num.c_str()));
num="";
location=i;
}
if(i==str.length()-1)
{
for(int k=location+1;k<=i;k++)
num=num+str[k];
i_num.push_back(atoi(num.c_str()));
num="";
location=i;
}
}
for(int p=0;p<=4;p++)
cout<<i_num[p]<<endl;
return 0;
}
c++ triangle
這是執行檔案,給大家下載去玩
https://skydrive.live.com/redir.aspx?cid=a4872f58128956b2&resid=A4872F58128956B2!268&parid=A4872F58128956B2!267&authkey=!APmGHg_J5c00EGw
下面則是列出MFC主要增加的部分
class CtriangleDlg : public CDialogEx //全域變數 抓輸入資料
{
public:
int number;
bool start;
//其他略
};
BOOL CtriangleDlg::OnInitDialog() //初始化痊癒變數的數值
{
start=0;
//其他略
}
void CtriangleDlg::OnPaint()
{
if (IsIconic())
{
//略
}
else
{
CPaintDC pDC(this);
if(start==1) //button按下時開始
{
pDC.MoveTo(100,500); //初始的三點座標
pDC.LineTo(300,100);
pDC.LineTo(500,500);
pDC.LineTo(100,500);
if(number==0)
;
else
triangle(100,500,300,100,500,500,0,number,&pDC);
}
CDialogEx::OnPaint();
}
}
void triangle(double x1,double y1,double x2,double y2,double x3,double y3,int count,int N,CPaintDC* draw)
{
double leftx=(x1+x2)/2;
double lefty=(y1+y2)/2;
double rightx=(x1+x3)/2;
double righty=(y1+y3)/2;
double downx=(x2+x3)/2;
double downy=(y2+y3)/2;
draw->MoveTo(int(leftx),int(lefty));
draw->LineTo(int(rightx),int(righty));
draw->LineTo(int(downx),int(downy));
draw->LineTo(int(leftx),int(lefty));
count++;
if(count<N)
{
triangle(leftx,lefty,x2,y2,downx,downy,count,N,draw);
triangle(x1,y1,leftx,lefty,rightx,righty,count,N,draw);
triangle(rightx,righty,downx,downy,x3,y3,count,N,draw);
}
}
void CtriangleDlg::OnBnClickedButton1()
{
CString tmp;
GetDlgItemText(IDC_EDIT1,tmp); //抓取edit box的資料
number= _ttoi(tmp.GetBuffer(0));
start=1;
Invalidate(); //畫面清空重新畫圖
}
NCNU LISP hand write 1
Q:
(define (A x y )
(cond((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1) (A x (- y 1 )))))
)
(define (h n) (A 2 n ) )的結果&證明
A:
NCNU LISP HW3
exercise 3: linear recursive and linear iterative
but the function is changed:
(define (recursive x)
(cond ((= x 0) 0)
((= x 1) 1)
((= x 2) 2)
(else (- (+ (* (recursive (- x 1)) 2) (recursive (- x 2)))
(recursive (- x 3))))
)
)
(define (interative x)
(cond ((= x 0) 0)
((= x 1) 1)
((= x 2) 2)
(else (do-interative 0 1 2 (- x 2)))
)
)
(define (do-interative a b c x)
(cond ((= x 0) c)
(else (do-interative b c (- (+ (* c 2) b) a) (- x 1)))
)
)
but the function is changed:
f(n) = n if n < 3 2*f(n-1)+f(n-2)-f(n-3) otherwise
(define (recursive x)
(cond ((= x 0) 0)
((= x 1) 1)
((= x 2) 2)
(else (- (+ (* (recursive (- x 1)) 2) (recursive (- x 2)))
(recursive (- x 3))))
)
)
(define (interative x)
(cond ((= x 0) 0)
((= x 1) 1)
((= x 2) 2)
(else (do-interative 0 1 2 (- x 2)))
)
)
(define (do-interative a b c x)
(cond ((= x 0) c)
(else (do-interative b c (- (+ (* c 2) b) a) (- x 1)))
)
)
訂閱:
文章 (Atom)