牛客竞赛语法入门班选择结构习题
C语言版本的参考代码
重点题: F 吃瓜群众 H 小名的回答 N 送分题 O 四季 P B是不是太迟了 Q 前天是哪天 R L1-2单位换算 T 排队领水 U 可编程拖拉机比赛
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 小名的回答
a x b > 0保证了终点在第一象限或者第三象限。
- 经过n步到达坐标(a,b),至少需要走a + b步,即
n >= a + b - 从(0,0)到(a,b)可能出现往返的情况,但是不管如何往返,往返的步数一定是偶数
- 往返的步数 = 多走的无用的步数 = n - (a + b)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b,n;
cin >> a >> b >> n;
if (n >= a + b && (n - a - b) % 2 == 0) cout << "YES";
else cout << "NO";
return 0;
}
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 送分题
暴力枚举所有可能的组合情况即可
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
int ans1 = a + b + c;
int ans2 = a * b * c;
int ans3 = a * b + c;
int ans4 = a + b * c;
int ans5 = (a + b) * c;
int ans6 = a * (b + c);
cout << max({ans1,ans2,ans3,ans4,ans5,ans6}) << endl;
return 0;
}
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;
}
Q 前天是哪天(嫌麻烦,不写了)
R L1-2单位换算
n为正整数,故n x 12 x 2.54 x 10最多只有一位小数,n x 12 x 2.54 x 10 x 10必为整数 ① 取 n = 1,1 x 12 x 2.54 x 10 x 10 = 3048,3048 / 10 = 304(整数部分),3048 % 10 = 8(小数部分) ② 取 n = 10,10 x 12 x 2.54 x 10 x 10 = 30480,30480 / 10
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int ans = n * 12 * 254;
if (ans % 10 == 0) cout << ans / 10;
else cout << ans / 10 << "." << ans % 10 << endl;
return 0;
}
S 纸牌 顺序结构中有相同的题,略
T 排队领水
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
if (b + c < a) cout << c + 1 << endl;
else if (b + c == a) cout << c << endl;
return 0;
}
U 可编程拖拉机比赛
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
int a,b,c,d,e,f;
a = ceil(n * 0.1);
b = ceil(n * 0.2);
c = ceil(n * 0.3);
d = floor(n * 0.1);
e = floor(n * 0.2);
f = floor(n * 0.3);
cout << a - d << " " << (a + b) - (d + e) << " " << (a + b + c) - (d + e + f);
return 0;
}
V [NOIP2004]不高兴的津津 使用数组和循环写法会清晰一些,
#include <iostream>
using namespace std;
int arr[10];
int main()
{
int a,b;
int pos = 1;
for (int i = 1;i <= 7;i++)
{
cin >> a >> b;
arr[i] += a;
arr[i] += b;
}
for (int i = 1;i <= 7;i++)
if (arr[i] > arr[pos])
pos = i;
if (arr[pos] > 8) cout << pos;
else cout << 0;
return 0;
}
W [NOIP2008]ISBN号码
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d;
scanf("%d-%d-%d-%d",&a,&b,&c,&d);
int sum = 0;
int bai = b / 100;
int shi = b / 10 % 10;
int ge = b % 10;
int wan = c / 10000;
int qian = c % 10000 / 1000;
int bai_2 = c % 10000 % 1000 / 100;
int shi_2 = c % 10000 % 1000 % 100 / 10;
int ge_2 = c % 10000 % 1000 % 100 % 10;
sum = (a*1+bai*2+shi*3+ge*4+wan*5+qian*6+bai_2*7+shi_2*8+ge_2*9) % 11;
if (d == sum) printf("Right");
else printf("%d-%d-%d-%d",a,b,c,sum);
return 0;
}
|