牛客竞赛语法入门班选择结构习题
C语言版本的参考代码
重点题:
F 吃瓜群众
H 小名的回答
N 送分题
O 四季
P B是不是太迟了
A 比大小
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
if (a > b) cout << ">";
else if (a == b) cout << "=";
else cout << "<";
return 0;
}
B 卡拉兹函数
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n % 2) cout << 3 * n + 1;
else cout << n / 2;
return 0;
}
C 默契
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
if (a == b) cout << "Tacit!";
else cout << "No Tacit!";
return 0;
}
D 整除判断
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
if (a % b) cout << "NO";
else cout << "YES";
return 0;
}
E CSimplemathproblem
需要开long long
#include <iostream>
using namespace std;
int main()
{
long long a,b;
cin >> a >> b;
if (b % a) cout << b - a;
else cout << b + a;
return 0;
}
F 吃瓜群众
两个偶数之和一定是偶数,即如果这个数字是偶数,就可以分解为两个偶数的部分;
需要对数字2进行特判
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
if (a % 2 || a == 2) cout << "NO, you can't divide the watermelon into two even parts.";
else cout << "YES, you can divide the watermelon into two even parts.";
return 0;
}
G jyq跳格子
从1开始,每次加2或者加4,多次之后最后的结果一定是奇数。故若输入是奇数则输出其本身,若输出数据是偶数则输出-1
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n % 2) cout << n;
else cout << "-1";
return 0;
}
H 小名的回答
I 牛妹数
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (! (n % 2) && n > 50) cout << "yes";
else cout << "no";
return 0;
}
J 判断闰年
闰年的判断方法:
① 是4的倍数,且不是100的倍数;
② 是400的倍数。
满足其中之一的年份是闰年
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n % 4 == 0 && n % 100 || n % 400 == 0) cout << "yes";
else cout << "no";
return 0;
}
K 统计数据正负个数
#include <iostream>
using namespace std;
int main()
{
long long n;
int po = 0,ne = 0;//赋初始值
while (cin >> n)
if (n > 0) po++;
else if (n < 0) ne++;
cout << "positive:" << po << endl << "negative:" << ne;
return 0;
}
L 小乐乐是否被叫家长
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
double ans = 1.0 * (a + b + c) / 3;
if (ans >= 60) cout << "NO";
else cout << "YES";
return 0;
}
M 最大最小值
0x3f3f3f3f
是10 ^ 9级别大小
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
int maxn = 0,minn = 0x3f3f3f3f;
cin >> a >> b >> c;
if (maxn < a) maxn = a;
if (maxn < b) maxn = b;
if (maxn < c) maxn = c;
if (minn > a) minn = a;
if (minn > b) minn = b;
if (minn > c) minn = c;
cout << "The maximum number is : " << maxn << endl;
cout << "The minimum number is : " << minn << endl;
return 0;
}
N 送分题
O 四季
#include <iostream>
using namespace std;
int main()
{
int year,month;
scanf("%4d%2d",&year,&month);//取出月份的方法
if (month >= 3 && month <= 5) cout << "spring";
else if (month >= 6 && month <= 8) cout << "summer";
else if (month >= 9 && month <= 11) cout << "autumn";
else cout << "winter";
return 0;
}
P B是不是太迟了
错误代码:测试样例为2020/09/30回输出QAQ
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
if (b <= 10 && c < 29) printf("No. It's not too late.");
else printf("QAQ");
return 0;
}
AC代码:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
if (b < 10) printf("No. It's not too late.");
else if (b == 10 && c < 29) printf("No. It's not too late.");
else printf("QAQ");
return 0;
}