項目5:貪財的富翁
代碼:
#include<iostream>
using namespace std;
int main()
{
int a,n=1;
double b;
a=100000;b=0.01;
cout<<n<<" "<<"陌給富:"<<a<<" \t"<<"富給陌:"<<b<<" "<<endl;
for(n=2;n<=30;n++)
{
a=a+100000;
b=b+b*2;
cout<<n<<" "<<"陌給富:"<<a<<" \t"<<"富給陌:"<<b<<" "<<endl;
}
if(a>=b)
cout<<"陌生人虧了\n";
else
cout<<"富翁虧了\n";
return 0;
}
項目7:窮舉法解決組合問題
三色球問題:若一個口袋中放有12個球,其中有3個紅的。3個白的和6個黒的,問從中任取8個共有多少種不同的顏色搭配
代碼
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"不同顏色的搭配有:\n";
for(a=0;a<=3;a++)
for(b=0;b<=3;b++)
{
if(8-a-b<=6)
cout<<"紅球:"<<a<<"白球:"<<b<<"黑球:"<<8-a-b<<endl;
}
return 0;
}
項目6:輸出星號圖
代碼
#include <iostream>
using namespace std;
int main( )
{
int a,b,c=6;
for(b=1; b<=c-1; ++b)
cout<<" ";
cout<<"*"<<endl;
for(a=2;a<=c-1;++a)
{
for(b=1; b<=c-a; ++b)
cout<<" ";
cout<<"*";
for(b=1; b<=2*a-3; ++b)
cout<<" ";
cout<<"*"<<endl;
}
for(b=1; b<=2*c-1; ++b)
cout<<"*";
cout<<endl;
return 0;
}