继续活动生疏的小手。
比起刚开始写的代码,更加的简洁生动。
P5710 数的性质
熟练使用逻辑运算符,正确辨别逻辑关系。
#include<bits/stdc++.h>
using namespace std;
bool fun1(int x)
{
if(x%2==0)
return 1;
return 0;
}
bool fun2(int x)
{
if(x>4&&x<=12)
return 1;
return 0;
}
int main()
{
int x;
cin >> x;
if(fun1(x)&&fun2(x))
cout << 1 << " ";
else
cout << 0 << " ";
if(fun1(x)||fun2(x))
cout << 1 << " ";
else
cout << 0 << " ";
if((fun1(x)&&!fun2(x))||(!fun1(x)&&fun2(x)))
cout << 1 << " ";
else
cout << 0 << " ";
if(!fun1(x)&&!fun2(x))
cout << 1;
else
cout << 0;
return 0;
}
P5711 闰年判断
闰年的判断表达式。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
cin >> x;
if((x%4==0&&x%100!=0)||x%400==0)
cout << 1;
else
cout << 0;
return 0;
}
P5712 Apples
if else 分支结构
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
cin >> x;
if(x<2)
printf("Today, I ate %d apple.",x);
else
printf("Today, I ate %d apples.",x);
return 0;
}
P5713 洛谷团队系统
if else 分支结构
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
cin >> x;
if(5*x>3*x+11)
cout << "Luogu";
else
cout << "Local";
return 0;
}
P5714 肥胖问题
简单计算加分支结构
#include<bits/stdc++.h>
using namespace std;
int main()
{
double m,n;
cin >> m >> n;
double BMI = m/n/n;
if(BMI<18.5)
cout << "Underweight";
else if(BMI>=24)
{
cout << BMI << endl;
cout << "Overweight";
}
else
cout << "Normal";
return 0;
}
P5715 三位数排序
注意先排ab,再排ac,最后排bc,要有顺序排序。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
if(a>b)
{
int temp = a;
a = b;
b = temp;
}
if(a>c)
{
int temp = a;
a = c;
c = temp;
}
if(b>c)
{
int temp = b;
b = c;
c = temp;
}
cout << a <<" "<< b << " " << c;
return 0;
}
P5716 月份天数
使用数组输出更加方便。
#include<bits/stdc++.h>
using namespace std;
int normal_year[13]={31,28,31,30,31,30,31,31,30,31,30,31};
int special_year[13]={31,29,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y;
cin >> y;
int m;
if((y%4==0&&y%100!=0)||y%400==0)
{
cin >> m;
cout << special_year[m-1];
}
else
{
cin >> m;
cout << normal_year[m-1];
}
return 0;
}