寒假集訓開始學習C++,用C++實作了杭電oj上2000-2009的題目,
杭電2000ASCII碼排序 C++實作,
#include<iostream>
using namespace std;
int main()
{
char str[3],temp;
while(cin>>str)
{
if(str[0]>str[1])
{
temp=str[0],str[0]=str[1],str[1]=temp;
}
if(str[0]>str[2])
{
temp=str[0],str[0]=str[2],str[2]=temp;
}
if(str[1]>str[2])
{
temp=str[1],str[1]=str[2],str[2]=temp;
}
cout<<str[0]<<" "<<str[1]<<" "<<str[2]<<endl;
}
return 0;
}
注意是定義字符,輸出時要有空格,
杭電oj2001計算兩點間的距離 C++實作,

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
double x1,x2,y1,y2,x;
while(cin>>x1>>y1>>x2>>y2)
{
x=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
cout<<fixed<<setprecision(2)<<x<<endl;
}
return 0;
}
結果要保留兩位小數,此時就需要用到:
#include<iomanip> //setiosflags(ios::fixed),頭檔案為:include<iomanip>
cout<<fixed<<setprecision(2)<<x<<endl;//setprecision(n)與setiosflags(ios::fixed)合用,可以控制小數點右邊的數字個數,
杭電oj2002計算球的體積 C++實作,

#include<iostream>
#include<iomanip>//setiosflags(ios::fixed),頭檔案為:include<iomanip>
#define pi 3.1415927
using namespace std;
int main()
{
double v,r;
while(cin>>r)
{
v=pi*r*r*r*4/3;
cout<<setiosflags(ios::fixed)<<setprecision(3)<<v<<endl;//setprecision(n)與setiosflags(ios::fixed)合用,可以控制小數點右邊的數字個數,
}
return 0;
}
注意結果保留3位小數,
杭電oj2003 求絕對值 C++實作,

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double n;
while(cin>>n)
{
if(n>0)
{
cout<<fixed<<setprecision(2)<<n<<endl;
}
else
cout<<fixed<<setprecision(2)<<-n<<endl;
}
return 0;
}
杭電oj2004 成績轉換 C++實作,

#include<iostream>
using namespace std;
int main()
{
int a;
while(cin>>a)
{
if(a>=90&&a<=100)
{
cout<<'A'<<endl;
}
else if(a>=80&&a<=89)
{
cout<<'B'<<endl;
}
else if(a>=70&&a<=79)
{
cout<<'C'<<endl;
}
else if(a>=60&&a<=69)
{
cout<<'D'<<endl;
}
else if(a>=0&&a<=59)
{
cout<<'E'<<endl;
}
else cout<<"Score is error!"<<endl;
}
return 0;
}
杭電oj2005 第幾天 C++實作

#include<iostream>
using namespace std;
int main()
{
int y,m,d,i,s;
while(cin>>y)
{
s=0;
int t1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int t2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
cin.ignore();
cin>>m;
cin.ignore();
cin>>d;
if((y%400==0)||((y%100!=0)&&(y%4==0)))
{
for(i=0;i<m-1;i++)
{
s=s+t2[i];
}
s=s+d;
}
else
{
for(i=0;i<m-1;i++)
{
s=s+t1[i];
}
s=s+d;
}
cout<<s<<endl;
}
return 0;
}
注意
要判斷是不是閏年,閏年的2月份是29天,
cin.ignore();
cin.ignore();的作用是消除輸入中的1985/1/20中的斜杠,
杭電2006 求奇數的乘積 C++實作

#include<iostream>
using namespace std;
int main()
{
int n,m,sum;
while(cin>>n)
{
sum=1;
for(int i=0;i<n;i++)
{
cin>>m;
if(m%2!=0)
{
sum=sum*m;
}
}
cout<<sum<<endl;
}
return 0;
}
杭電oj2007 平方和與立方和

#include<iostream>
using namespace std;
int main()
{
int n,m,x,y,i;
while(cin>>m>>n)
{
x=0,y=0;
if(m>n)//比較n m的大小,
{
i=m,m=n,n=i;
}
for(;m<=n;m++)
{
if(m%2==0)
{
x=x+m*m;
}
else y=y+m*m*m;
}
cout<<x<<" "<<y<<endl;
}
return 0;
}
注意要比較m和n的大小,
杭電oj2008 數值統計 C++實作

#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
if(n==0)
{
break;
}
double a=0,b=0,c=0,i=0,x;
while(cin>>x)
{
i++;
if(x<0)
{
a++;
}
if(x==0)
{
b++;
}
if(x>0)
{
c++;
}
if(i==n)
{
break;
}
}
cout<<a<<" "<<b<<" "<<c<<endl;
}
return 0;
}
注意輸出中的空格,
杭電oj2009 求數列的和 C++實作

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
double b,sum=0;
b=n,sum=n;
for(int i=1;i<m;i++)
{
b=sqrt(b);
sum=sum+b;
}
cout<<fixed<<setprecision(2)<<sum<<endl;
}
return 0;
}
注意結果要求保留小數點后兩位,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/250685.html
標籤:其他
下一篇:單鏈表的實作以及相關操作
