本文主要是介绍C语言-翁恺-PTA-121-160课后练习题-04,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
title: C语言-翁恺-PTA-121-160课后练习题-04
tags:
- C
- PTA
description: ’ ’
mathjax: true
date: 2024-04-09 23:16:22
categories: - C
- PTA
7-137 Average
You are going to read a serial of none-negative integers, which ends with a negative number that does not count as one of the data. Your program calculates the average of all the none-negative integers, and prints the average which rounds to two decimal places.
When there’s no valid data at all, your program prints out:
None
输入格式:
A serial of none-negative integers, ending with a negative number, which is not one of the valide data.
输出格式:
A number rounds to two decimal places, which is the average of the serial.
The printf for this case is:
printf("%.2f\n", (double)sum/count);
输入例子
1 2 3 4 5 6 -1
输出例子
3.50
代码长度限制
16 KB
时间限制
400 ms
内存限制
错误原因
这题失败的原因,就是纯纯的语言问题了,没别的好说的
You are going to read a serial of none-negative integers
猜测翻译 :我们要读一串非负数的整数
机器翻译:您将读取一个非负整数序列
which ends with a negative number that does not count as one of the data.
猜测翻译:当读取负数的时候,这个将不再记录在数据之中
机器翻译:
以负数结尾,不作为数据之一。
Your program calculates the average of all the none-negative integers
猜测翻译:你的程序要计算,这一串非数据的平均值
机器翻译:你的程序计算所有非负整数的平均数
and prints the average which rounds to two decimal places.
猜测翻译:这个我不太会
机器翻译: 并打印出四舍五入到小数点后两位的平均值。
理解了题目之后,就非常非常非常好些了
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<limits.h>
#include<float.h>
int main()
{int sum = 0, count = 0, n = 0;scanf("%d", &n);while (n >= 0) {sum += n;count++;scanf("%d", &n);}if (sum == 0)printf("None");elseprintf("%.2f\n", (double)sum / count);return 0;
}
7-139 手机话费
小明的手机每天消费1元,每消费K元就可以获赠1元,一开始小明有M元,问最多可以用多少天?
输入格式:
每个测试实例包括2个整数M,K(2<=k<=M<=1000)。
输出格式:
对于每个测试实例输出一个整数,表示M元可以用的天数。
输入样例:
2 2
输出样例:
3
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
错误原因
这个思路有问题
抄一下别人的思路
正确代码如下:
#include <stdio.h>
int main(){int m,k,i=0;scanf("%d %d",&m,&k);while(m>0){m--;i++;if(i%k==0)m++;}printf("%d",i);}
7-144 藏头诗
本题要求编写一个解密藏头诗的程序。
注:在 2022 年 7 月 14 日 16 点 50 分以后,该题数据修改为 UTF-8 编码。
输入格式:
输入为一首中文藏头诗,一共四句,每句一行。注意:一个汉字占三个字节。
输出格式:
取出每句的第一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。
输入样例:
一叶轻舟向东流
帆稍轻握杨柳手
风纤碧波微起舞
顺水任从雅客流
输出样例:
一帆风顺
代码长度限制
16 KB
时间限制
400 ms
内存限制
错误原因
不理解中文字符的占比,不理解如何输出输出
正确代码
#include <stdio.h>
int main() {char c[100][3];for (int i = 1; i <= 4; ++i) {scanf("%s", c);printf("%c%c%c", c[0][0], c[0][1], c[0][2]);}return 0;
}
这篇关于C语言-翁恺-PTA-121-160课后练习题-04的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!