運算式
#include <iostream>
using namespace std;
int main()
{
int a,b;
double c;
char op;
cout<<"輸入運算式:";
cin>>a>>op>>b;
switch(op)
{
case '+':
c=a+b;
cout<<a<<op<<b<<"="<<c<<endl;
break;
case '-':
c=a-b;
cout<<a<<op<<b<<"="<<c<<endl;
break;
case '*':
c=a*b;
cout<<a<<op<<b<<"="<<c<<endl;
break;
case '/':
c=(double)a/b;
cout<<a<<op<<b<<"="<<c<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------------
輸入年份月份判斷該月有幾天
#include <iostream>
using namespace std;
int main()
{
int y,m;
cout<<"輸入年份月份:";
cin>>y>>m;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout<<m<<"月有31天"<<endl;
break;
case 4:
case 6:
case 9:
case 11:
cout<<m<<"月有30天"<<endl;
break;
case 2:
if(y%4==0&&y%100!=0||y%400==0)
cout<<m<<"月有29天"<<endl;
else
cout<<m<<"月有28天"<<endl;
break;
default:
cout<<"輸入錯誤"<<endl;
}
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------------------------
是2或3的倍數且不是6的倍數
#include <iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=100;i++)
{
if((i%2==0 || i%3==0)&&i%6!=0) //優先順序!>&&>||
{
cout<<i<<endl;
}
}
system("pause");
return 0;
}
----------------------------------------------------------------------------------------------------------------------
輸入n
計算1/n+2/n+3/n+....1
#include <iostream>
using namespace std;
int main()
{
int i,n;
double sum=0;
i=1;
cout<<"Input n:";
cin>>n;
while(i<=n)
{
sum+=(double)i/n;
cout<<(double)i/n<<" "<<sum<<endl;
i++;
}
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------------
找出三位數中那些是水仙花數 153=1*1*1+5*5*5+3*3*3
#include <iostream>
using namespace std;
int main()
{
int a,b,c,i;
for(i=100;i<=999;i++)
{
a=i/100;
b=i/10%10;
c=i%10;
if(a*a*a+b*b*b+c*c*c==i)
cout<<i<<endl;
}
system("pause");
return 0;
}
-----------------------------------------------------------------------------------------------------------------------
f(n)=1/2*2+2/3*3+.....(n-1)/n*n
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
int i=2,n;
double sum=0;
cout<<"Input n:";
cin>>n;
while(i<=n)
{
sum+=(i-1)/pow(i,2);
i++;
}
cout<<sum<<endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------