24h購物| | PChome| 登入
2008-05-26 15:07:29| 人氣815| 回應0 | 上一篇 | 下一篇

c++程式語言(更新日期2008/6/5)

推薦 0 收藏 0 轉貼0 訂閱站台

請設計一程式,可判斷西元年是平年或閏年。
#include
using namespace std;
int main()
{ int a;
cin>>a;
if (a%4!=0)
cout<<”是平年”< else if (a%4==0 && a%100==0 && a%400!=0)
cout<<”是平年”< else if (a%3200==0)
cout<<”是平年”< else
cout<<”是閏年”<
system(”pause”);
return 0;
}



請設計一程式,能使輸入的數字,反向列印出來。
#include
using namespace std;
int main()
{
int n;
cin >> n;
do
{
cout << n%10 ;
n=n/10;
}

while(n>0);

system(”pause”);
return 0;
}

請設計一程式,讓使用者輸入密碼,密碼如正確即顯示進入程式,錯誤則繼續輸入。
#include
using namespace std;
int main()
{
string a;
int b;
cin >> a;
while ( a!=”CHSH” && a!=”chsh”)
{
cout<<”please try again”<< endl;
cin >> a;
}
cout<<”Congratulations! Please enjoy the game!”< cout<<”test your luck,enter a number”< cin >> b;
cout<< b+1 <
system(”pause”);
return 0;
}

請設計一程式,列出九九乘法表。

#include
#include // 使用 setw 要引用的標題檔
using namespace std;

int main()
{
int i,j;
for(i=1; i<=9; i++) //外層迴圈
{
for (j=1; j<=9; j++) //內層迴圈
cout << i << ”*” << j << ”=” << setw(2) << i*j << ” ”; // setw(n)用來設定欄寬為n
cout << endl;
}
system(”pause”);
return 0;
}

設計一程式,能算出N!

#include
using namespace std;

int main()
{
unsigned int n, fact=1;
cout << ”請輸入一正整數: ”;
cin >> n;
for (int i=1; i<=n; i++)
{
fact *= i;
}
cout << n << ”!= ” << fact << endl;
system(”pause”);
return 0;
}

設計一程式,輸入整數M & N,並列出兩者之間13的倍數。
#include
using namespace std;

int main()
{
int i, m, n;
cout << ”請輸入正整數m= ”;
cin >> m;
cout << ”請輸入正整數n= ”;
cin >> n;
cout << ”介於兩整數間的13倍數有: ” ;
for (i=m; i<=n; i++)
{
if (i%13==0)
cout << i << ” ”;
}
cout << endl;
system(”pause”);
return 0;
}

設計一程式,輸入N,計算(1/1)+(1/2)+(1/3)+...+(1/N)

#include
using namespace std;

int main()
{
int n;
double sum=0;
cout << ”請輸入一正整數: ”;
cin >> n;
for (int i=1; i<=n; i++)
{
sum += 1.0/i; // 相當於 sum = sum + 1.0/i
}
// cout.precision(15);
cout << ”倒數和= ” << sum << endl;
system(”pause”);
return 0;
}
設計一程式,輸入某考生N科的成績,並求其總和及平均。

#include
using namespace std;

int main()
{
int i, n, score, sum=0;

cout << ”請問有幾科: ”;
cin >> n;
for (i=1; i<=n; i++)
{
cout << ”請輸入成績: ”;
cin >> score;
sum += score;
}
cout << endl << ”總分為 ” << sum << endl;
cout << ”平均為 ” << (double)sum/n << endl;
system(”pause”);
return 0;
}

設計一程式,判斷某個字元的性質。
#include
using namespace std;

int main()
{
char ch;
cout << ”請輸入一任意字元: ”;
cin >> ch;
if (ch>=’0’ && ch<=’9’)
cout << ”這是阿拉伯數字!” << endl;
else if (ch>=’a’ && ch<=’z’)
cout << ”這是小寫英文字母!” << endl;
else if (ch>=’A’ && ch<=’Z’)
cout << ”這是大寫英文字母!” << endl;
else
cout << ”這是特殊字元!” << endl;
system(”pause”);
return 0;
}

講義P.61範例。
#include
using namespace std;
int main()
{

int num;
char name[10];
float chi, eng ,mat;
freopen(”score.in”,”r”,stdin);
freopen(”score.out”,”w”,stdout);
while(cin>>num>>name>>chi>>eng>>mat)
{
cout.setf(ios::fixed);
cout.precision(0);
cout < cout << chi+eng+mat <<”t”;
cout.precision(2);
cout << (chi+eng+mat)/3 << endl;
}



system(”pause”);
return 0;
}


設計一程式,讀入一整行的文字,將所輸入的內容反轉過來。如:輸入 abc def 123,則輸出 321 fed cba。

#include
using namespace std;
int main()
{

string s1;
getline(cin,s1);
for(int i=s1.size()-1;i>=0;i--)
cout<< s1.at(i);
cout << endl;

system(”pause”);
return 0;
}


【輸入】:測試檔 pro1.in。每一行有三個整數值,分別代表三角形三邊長a, b, c。其中0< a, b, c < 200,且a, b, c必可構成三角形。

【輸出】:每一個三角形面積輸出一行,並精確到小數點以下六位。

#include
#include
#include
using namespace std;
int main()
{
double a,b,c,d,s;
freopen(”pro1.in”,”r”,stdin);
freopen(”prol.out”,”w”,stdout);
while(cin >> a >> b>> c)
{
s=(a+b+c)/2;
d=s*(s-a)*(s-b)*(s-c);
if(a+b>c && b+c>a && a+c>b && a>0 && a<200 && b>0 && b<200 && c>0 &&c<200)
cout< }

system(”pause”);
return 0;
}










#include
using namespace std;
int main()
{
int n,point,sum=0,h=0;

cout<<”請輸入全班人數:”;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<”請輸入”< cin>>point;
sum=sum+point;

if(point<60)
{
h=h+1;
}
}
cout<<”全班平均為:”<<(double)sum/n <<”分” ;
cout<<”不及格人數為:”<
system(”pause”);
return 0;
}


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include
using namespace std;
int main()
{
int a,b,c,d,e;
e=a%10;
d=a/10%10;
c=a/100%10;
b=a/1000%10;
//freopen(”pro3.in”,”r”,stdin);
//freopen(”pro3.out”,”w”,stdout);
while(cin>>a)
{
if(a>9999 || a<1000)
cout<<”Failure Input”< else if(b=c && c!=d && d!=e)
cout<<”yes”< else if(c=d && d!=e && b!=e)
cout<<”yes”< else if(d=e && b!=e && c!=b)
cout<<”yes”< else if(b=e && e!=d && d!=c)
cout<<”yes”< else
cout<<”no”< }

//system(”pause”);
return 0;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ACM10071
#include
#include
using namespace std;
int main()
{
int a,v,t,tc;
while(cin>>v>>t)
{
a=v/t;
tc=2*t;
cout<< v*tc + 0.5*a*tc*tc < }

return 0;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ACM11172
#include
using namespace std;
int main()
{
int a,b;
while(cin>>a>b)
{
if(a>b)
cout<<”>”< else if(a==b)
cout<<”=”< else
cout<<”<”< }

return 0;
}

台長: Penguin Crazy

您可能對以下文章有興趣

人氣(815) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 上課資料 |
此分類上一篇:ENGLISH NOTES

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文