28. 水果价格

题目

问题描述

一家水果店出售四种水果,每公斤价格的苹果(代码为a)1.5元,橘子(代码为o)1.4元,香蕉(代码为b)1.48元,菠萝(代码为p)1.08元。编一个程序,使售货员只要在键盘上打入货品的代码及重量,计算机将显示货品名、单价、重量及总价。

输入说明

你的程序需要从标准输入设备(通常为键盘)中读入多组测试数据。

每组测试数据的第一行为一个整数m,表示有m件货品要被购买。在接下来的m行中,每行输入两个值d,g。d表示货品的代码,g表示重量。两组数据之间没有多余的空行。

输出说明

对每组测试数据,你的程序需要向标准输出设备(通常为启动该程序的终端)依次输出一组对应的答案。对应每组输入,输出货品名、单个总价及全部总价。具体格式参照样例输出:第一行apple前为7个空格,之后为2个空格,其他水果名后都是1个空格,sum后没有空格;第二行price后有2个空格,其后关于价格的表示多为占7格2位小数且左对齐,但其中pineapple为占10格2位小数且左对齐,注意sum的价格仍然占7格,如第一组样例中的54.60后还有2个空格;第三行weight后有1个空格,其后的数据与第二行一致。每两组数据之间有一个空行,最后一组测试数据之后没有空行。

个人总结

  1. 解题思路是用变量分别累计四种水果的重量,读完一组数据的所有商品后再统一计算总价。需要注意多组数据的处理,使用while(cin >> m)循环读入,并通过布尔标记控制换行,确保只在两组数据之间输出空行,避免最后一组多输出空行导致格式错误。输出格式是难点,表头建议直接复制题目字符串以防数错空格。
  2. 总结一下这道题涉及的格式化输出知识点:

    需要引入头文件 <iomanip>,然后组合使用以下控制符:

    setw(7):设置域宽为7。
    left:设置左对齐(默认为右对齐)。
    fixed:固定点显示(防止科学计数法)。
    setprecision(2):保留2位小数。

    #include <iostream>
    #include <iomanip> // 必须包含这个头文件
    
    using namespace std;
    
    int main() {
        double a = 12.3456;
        
        // 连写格式
        cout << left << setw(7) << fixed << setprecision(2) << a;
        
        return 0;
    }
    


    注意 setw(7) 是一次性的,只对紧跟在后面的那个数据生效,下次输出如果还需要限制宽度,必须重写 setw;而 left、fixed 和 setprecision 是持久生效的,设置一次后直到程序结束或被修改前一直有效。
     

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	// 记录每个水果单价
	double AP = 1.5, OP = 1.4, BP = 1.48, PP = 1.08;
	
	int m;
	bool f = true; // 是否第一组
	while (cin >> m){ // 每组数据
		// 先输出表头
		if (f){
			f = false;
		} else{
			cout << "\n";
		}
		cout << "       apple  orange banana pineapple sum\n";
		
		// 开始录入数据
		double aw = 0, ow = 0, bw = 0, pw = 0;
		double as = 0, os = 0, bs = 0, ps = 0;
		
		for (int i = 0; i < m ; i++){ 
			char d;
			double g;
			cin >> d >> g;
			
			// 增加重量
			switch (d) {
				case 'a':
					aw += g;
					break;
				case 'o':
					ow += g;
					break;
				case 'b':
					bw += g;
					break;
				case 'p':
					pw += g;
					break;
			}
		}
		
		// 统计价格
		as = aw * AP;
		os = ow * OP;
		bs = bw * BP;
		ps = pw * PP;
		double ss = as + os + bs + ps;
		double sw = aw + ow + bw + pw;
		
		// 输出结果
		cout << left << fixed << setprecision(2) << "price  ";
		cout << setw(7) << as << setw(7) << os << setw(7) << bs;
		cout << setw(10) << ps << setw(7) << ss << "\n";
		cout << "weight ";
		cout << setw(7) << aw << setw(7) << ow << setw(7) << bw;
		cout << setw(10) << pw << setw(7) << sw << "\n";
	}
	
	
	
	return 0;
}

29. 求奇数的乘积

题目

问题描述

给你n个整数,求他们中所有奇数的乘积。

输入说明

输入数据包含两行,第一行为一个数为n,表示第二行将输入n个整数。你可以假设这n个数据中必定至少存在一个奇数。

输出说明

输出一个整数。

个人总结

  1. 判断奇数 :a % 2 == 1;

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	
	int res = 1;
	int a;
	for (int i = 0; i < n; i++){
		cin >> a;
		// 如果是奇数就相乘
		if (a % 2 == 1){
			res *= a;
		}
	}
	cout << res << "\n";
	
	return 0;
}

 题目3

题目

问题描述

输入N个日期,每个以年、月、日的顺序读入,打印输出最晚的日期、最早的日期。

输入说明

你的程序需要从标准输入设备(通常为键盘)中读入多组测试数据。每组输入数据由多行组成。每组测试数据的第一行输入一个整数N(0<N<20),表示有N个日期。其后N行每行有三个整数Y(1≤Y≤2015),M(1≤M≤12),D(1≤D≤31)表示一个日期。

输出说明

对每组测试数据,你的程序需要向标准输出设备(通常为启动该程序的文本终端)输出两行,每行包括3个整数,第一行为最晚日期,第二行为最早日期,整数之间以一个空格分隔,行首与行尾无空格,所有数据前后没有多余的空行,两组数据之间也没有多余的空行。

个人总结

  1. 核心思路是打擂台法,遍历所有 N 个日期,同时维护当前已知的最早日期和最晚日期。
  2. 初始化:不要给最早和最晚变量赋随意的初值(比如 0 或 9999)。最稳妥的方法是在读入第一个数据(循环变量 i 为 0)时,直接把当天日期作为初始的最早和最晚值。
  3. 日期比较逻辑:采用字典序(层级比较)原则。先比较年份,年份大则大;如果年份相同,比较月份;如果月份也相同,再比较日期。
  4. if 判断需要把三个条件用或(or)并列连接,例如判断更早时:年更小、或者年相等但月更小、或者年月都相等但日更小。
  5. 审题注意:输出要求先输出最晚,再输出最早,注意顺序不要写反。

代码

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int N;
	cin >> N;
	
	int ey,em,ed; // 最早时间
	int ly,lm,ld; // 最晚时间
	
	for (int i = 0; i < N; i++){
		int y,m,d;
		cin >> y >> m >> d;
		
		// 如果是第一轮,初始化数据
		if (i == 0){
			ey = y;
			ly = y;
			em = m;
			lm = m;
			ed = d;
			ld = d;
			continue;
		}
		
		// 处理最早
		if (y < ey or (y == ey and m < em) or (y == ey and m == em and d < ed)) {
			ey = y;
			em = m;
			ed = d;
		}
		
		// 处理最晚
		if (y > ly or (y == ly and m > lm) or (y == ly and m == lm and d > ld)) {
			ly = y;
			lm = m;
			ld = d;
		}
	}
	
	cout << ly << " " << lm << " " << ld << "\n";
	cout << ey << " " << em << " " << ed << "\n";
	
	return 0;
}

计算机英语翻译

In some cases an agent's responses improve over time as the agent learns. This could take the form of developing procedural knowledge(learning "how") or storing declarative knowledge (learning "what"). Learning procedural knowledge usually involves a trial-and-error process by which an agent learns appropriate actions by being punished for poor actions and rewarded for good ones. Following this approach, agents have been developed that, over time, improve their abilities in competitive games such as checkers and chess. Learning declarative knowledge usually takes the form of expanding or altering the "facts" in an agent's store of knowledge. For example, a baseball player must repeatedly adjust his or her database of knowledge from which rational responses to future events are determined.

在某些情况下,智能体的响应会随着学习而改善。

这可能表现为发展程序性知识(学习“怎么做”)的形式,或者是储存陈述性知识(学习“是什么”)的形式。

学习程序性知识通常采用试错法,通过惩罚错误行为和奖励正确行为,来让智能体学习正确的行为。通过这种方法,智能体会逐渐改善在诸如跳棋或棋牌一类的竞技性游戏中的能力。

学习陈述性知识通常表现为扩展或修改智能体知识储备中的“事实”。例如,棒球运动员必须不断调整其知识库,并以此确定对未来事件做出的理性反应。

To produce rational responses to stimuli, an agent must "understand"the stimuli received by its sensors. That is, an agent must be able to extract information from the data produced by its sensors, or in other words, an agent must be able to perceive. In some cases this is a straightforward process. Signals obtained from a gyroscope are easily encoded in forms compatible with calculations for determining responses. But in other cases extracting information from input data is difficult. Examples include understanding speech and images. Likewise, agents must be able to formulate their responses in terms compatible with their actuators. This might be a straightforward process or it might require an agent to formulate responses as complete spoken sentences—meaning that the agent must generate speech. In turn, such topics as image processing and analysis, natural language understanding, and speech generation are important areas of research.

为了对刺激做出理性响应,智能体必须“理解”传感器接收到的刺激。也就是说,智能体必须能够从传感器产生的数据中提取信息,换言之,智能体必须具备感知能力。

在某些情况下,这是一个直接的过程。例如陀螺仪获取的信号很容易被编码成与确定响应的计算相兼容的形式。

但在其他情况下,从输入数据中提取信息却很困难。理解语言和图片就是其中的例子。

同样,智能体必须能够以与其执行器相兼容的方式来制定响应。这可能是一个直接过程,也可能需要智能体制定响应。例如构思为完整的口语句子,这意味着智能体必须生成语音。

因此,诸如图片处理和分析、自然语言理解、语音生成这类话题是研究中的重要领域。

The agent attributes that we have identified here represent past as well as current areas of research. Of course, they are not totally independent of each other. We would like to develop agents that possess all of them, producing agents that understand the data received from their environments and develop new response patterns through a learning process whose goal is to maximize the agent's abilities. However, by isolating various types of rational behavior and pursuing them independently, researchers gain a toehold that can later be combined with progress in other areas to produce more intelligent agents.

我们所定义的智能体属性,既代表了过去,也代表了现在的研究领域。

当然,它们彼此之间并非完全独立。

我们希望开发出具备所有这些属性的智能体,使其能够理解从环境接收到的数据,并通过以最大化自身能力为目标的学习过程,发展出新的响应模式。

然而,通过分离各种类型的理性行为并对其进行独立研究,研究人员可以获得一个立足点,随后将其与其他领域的进展相结合,从而创造出更智能的智能体。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐