标签:return int 题解 scanf C语言 ++ 训练赛 ans sum
A题 P5660 数字游戏
https://www.luogu.com.cn/problem/P5660
根据此题题意,我们只需要统计字符1的个数并输出即可。
#include<stdio.h>
int main()
{
char x;
int ans = 0;
for (int i = 1; i <= 8; i++)
{
scanf("%c", &x);
if (x == '1')
ans++;
}
printf("%d\n", ans);
return 0;
}
B题 P1739 表达式括号匹配
https://www.luogu.com.cn/problem/P1739
拟统计括号是否匹配,就是当右括号多于左括号时,需要结束并输出。
#include<stdio.h>
int main() {
char ch; //字符
int ans = 0; //统计括号
ch = getchar(); //接收字符
while (ch != '@') { //等于@的时候退出循环
if (ch == '(') {
ans++;
}
else if (ch == ')') {
ans--;
}
if (ans < 0) { //特判 当右括号多于左括号时也是非法的
puts("NO");
return 0;
}
ch = getchar(); //输入字符
}
(ans == 0) ? puts("YES") : puts("NO");
return 0;
}
C题 P1179 数字统计
https://www.luogu.com.cn/problem/P1179
根据此题题意,我们只需要统计两个区间内,统计每个数中的数字2出现的次数累加并输出即可。
#include<stdio.h>
int main() {
int l, r; //左右区间
int i, j; //循环变量
int cnt = 0; //统计
scanf("%d%d", &l, &r);
for (i = l; i <= r; i++) {
j = i;
while (j != 0) { //分离每位数
if (j % 10 == 2) {
cnt++;
}
j = j / 10;
}
}
printf("%d\n", cnt);
return 0;
}
D题 P2010 回文日期
https://www.luogu.com.cn/problem/P2010
枚举后面四位(月份+日期)会更快,枚举后四位然后求出整个日期,判断是否在范围内即可。2月不需要判断是否是闰年,因为0229反过来是9220,整个日期是92200229,而9220年是闰年。
#include<stdio.h>
int i, j, n, m, a, b, c, sum, ans;
int s[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int main()
{
scanf("%d%d", &n, &m);
for (i = 1; i <= 12; i++)//枚举月和日
for (j = 1; j <= s[i]; j++)
{
c = (j % 10) * 1000 +
(j / 10) * 100 +
(i % 10) * 10 +
(i / 10);//算出前四位。
sum = c * 10000 + i * 100 + j;//算出整个日期
if (sum<n || sum>m) continue;
ans++;//统计
}
printf("%d\n", ans);
return 0;
}
E题 P1051 谁拿了更多的奖学金
https://www.luogu.com.cn/problem/P1051
定义它所需要的变量,然后累加每个人的奖学金,不断和最多奖学金的人进行比较,如果比它多,就更新最多值并保存它的名字。
#include<stdio.h>
#include<string.h>
int main()
{
char name[20]; //名字
int avg; //期末平均成绩
int cscore; //班级评议成绩
char gb; //学生干部
char xibu; //西部身份学生
int lunwen; //论文数
char Maxname[20]; //保存最多的奖学金学生姓名
int Maxmoney = 0; //保存最多奖学金学生的奖级
int totol = 0; //保存总的奖学金
int n;
int sum; //临时统计每个人的奖学金
scanf("%d", &n);
for (int i = 0; i < n; i++) {
sum = 0;
scanf("%s %d %d %c %c %d", name, &avg, &cscore, &gb, &xibu, &lunwen);
if (avg > 80 && lunwen >= 1) { //院士奖学金
sum += 8000;
}
if (avg > 85 && cscore > 80) { //五四奖学金
sum += 4000;
}
if (avg > 90) { //成绩优秀奖学金
sum += 2000;
}
if (avg > 85 && xibu == 'Y') { //西部奖学金
sum += 1000;
}
if (cscore > 80 && gb == 'Y') { //班级贡献奖
sum += 850;
}
totol += sum; //所有人总共的奖学金
if (sum > Maxmoney) { //找最大的那个人
Maxmoney = sum;
strcpy(Maxname, name);
}
}
printf("%s\n%d\n%d\n", Maxname, Maxmoney, totol);
return 0;
}
F题 P1605 迷宫
https://www.luogu.com.cn/problem/P1605
DFS模板题,从起点开始走,判断起点的上下左右哪个点能走并且没走过,就按照这个点开始走,之后也按这种方法走,直到出口或者超越了范围,就开始回溯继续进行判断,注意一些边界即可。
#include<stdio.h>
#define maxn 6 //地图大小
int maps[maxn][maxn];
int N, M, T;
int sx, sy, fx, fy;
int ans; //储存方案数
void DFS(int x, int y) {
if (x<1 || y<1 || x>N || y>M) { //超越地图范围
return;
}
if (maps[x][y] == 0) { //障碍点或已走过
return;
}
if (x == fx && y == fy) { //到达终点
ans++;
return;
}
maps[x][y] = 0; //标记已走过
DFS(x - 1, y); //往上走
DFS(x + 1, y); //往下走
DFS(x, y - 1); //往左走
DFS(x, y + 1); //往右走
maps[x][y] = 1; //还原状态
}
int main() {
scanf("%d%d", &N, &M);
scanf("%d", &T);
scanf("%d%d%d%d", &sx, &sy, &fx, &fy);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
maps[i][j] = 1; //初始化地图为1
}
}
for (int i = 0; i < T; i++) {
int xz, yz;
scanf("%d%d", &xz, &yz); //标记障碍点
maps[xz][yz] = 0;
}
DFS(sx, sy);
printf("%d\n", ans);
return 0;
}
G题 P1223 排队接水
https://www.luogu.com.cn/problem/P1223
我们想要n个人平均等待时间最小,我们就要从最小的开始排队接水,所以需要排序,然后套公式求所有人的等待时间,最后除以n即可。
s=a1*(n-1)+a2*(n-2)+a3*(n-3)……一直到 an。
#include<stdio.h>
void Swap(int* a, int* b) {
int t;
t = *a;
*a = *b;
*b = t;
}
int main() {
int i, n, a[1005], b[1005], f;//定义数组,一个赋值,一个记录序号
double s = 0;//结果要是浮点数形式
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = i + 1;//如果i为0,要加一
}
for (i = 0; i < n; i++) {
for (int j = n - 1; j > 0; j--) {
if (a[j] < a[j - 1]) {
Swap(&a[j], &a[j - 1]); //冒泡大法!!!
Swap(&b[j], &b[j - 1]); //注意,这里要同时变换
}
}
}
for (i = 0; i < n; i++) {
printf("%d ", b[i]);
}
for (i = 0; i < n; i++) { //套公式
a[i] *= (n - i - 1);
s += a[i];
}
printf("\n");
s /= n;
printf("%.2f", s);
return 0;
}
H题 P1873 砍树
https://www.luogu.com.cn/problem/P1873
有单调性,所以用二分法。
#include<stdio.h>
long long n, bz, s = 0, mid, leftt, longest, trees[1000008];
long long Max(long long a, long long b) {
if (a > b) {
return a;
}
return b;
}
int main()
{
scanf("%lld%lld", &n, &bz);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &trees[i]);
longest = Max(longest, trees[i]);//找到最长木材
}
while (leftt <= longest)
{
mid = (leftt + longest) / 2; //从中间点开始作为伐木机高度
s = 0;
for (int i = 1; i <= n; i++)
if (trees[i] > mid) //树的高度大于伐木机高度
s += trees[i] - mid; //高的部分累加
if (s < bz) //木材不足
longest = mid - 1;//在左边搜 减小高度增加木材
else
leftt = mid + 1;//在右边搜 增加高度减小木材
}
printf("%d\n", leftt - 1);
return 0;
}
标签:return,int,题解,scanf,C语言,++,训练赛,ans,sum 来源: https://blog.csdn.net/qq_45189114/article/details/113779624
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。