题目:1.表达式求值
问题描述
以字符串形式输入仅有整数和加减(正负)号构成的表达式,输出该表达式的值。
输入说明
标准输入设备中有多组测试数据,每组输入数据由一行组成,输入仅有整数和加减(正负)号构成的表达式(但是表达式可以插入空格)。
输出说明
依次输出从标准输入设备中读入的每一组测试数据对应的结果,输出一行,输出该表达式的值。所有数据前后没有多余的空格,两组数据之间也没有多余的空行。
输入范例
3+ 4+ 5+6
0+1
输出范例
18
1
个人总结:
1.直接用getline读取一行,去除空格后在开始读入数字和符号。
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[1000], clean[1000];
while (cin.getline(str, 1000)) {
// 移除所有空格
int len = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ') {
clean[len++] = str[i];
}
}
clean[len] = '\0';
// 补开头的正号
char expr[1001];
expr[0] = '+';
strcpy(expr + 1, clean);
// 计算表达式
int res = 0, num = 0;
char op = '+';
for (int i = 0; expr[i] != '\0'; i++) {
if (expr[i] == '+' || expr[i] == '-') {
if (i > 0) {
res += (op == '+') ? num : -num;
num = 0;
}
op = expr[i];
} else {
num = num * 10 + (expr[i] - '0');
}
}
// 处理最后一个数字
res += (op == '+') ? num : -num;
cout << res << endl;
}
return 0;
}
题目:2.删除字符
问题描述
从键盘输入一个字符串和一个字符,将输入字符从字符串中删除,输出新的字符串。如果字符串中没有此字符,则原样输出字符串。
输入说明
输入两行,第一行输入一个字符串,第二行输入一个字符。
字符串最多允许输入20个任意字符。
输出说明
输出删除字符后的字符串。
输入范例
ab ccdc
c
输出范例
ab ccdc
c
个人总结:
1.直接读入整行字符串,输出时跳过目标字符,并且特殊处理开头就是空格的字符。
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[21], ch, res[21] = {0};
// 直接读取整行字符串,不提前忽略任何字符
cin.getline(str, 21);
// 读取要删除的字符(处理换行符干扰)
cin.get(ch);
// 过滤掉读取字符时可能捕获的换行符
if (ch == '\n') cin.get(ch);
int len = 0;
// 逐字符复制,仅跳过目标字符
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ch) {
res[len++] = str[i];
}
}
res[len] = '\0';
cout << res << endl;
return 0;
}
题目:3.手机短号
问题描述
大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号。假设所有的短号都是"6"+手机号的后5位,比如号码为13512345678的手机,对应的短号就是645678。
现在,如果给你一个11位长的手机号码,你能找出对应的短号吗?
输入说明
输入数据的第一行是一个N(N <= 1000),表示有N个数据,接下来的N行每一行为一个11位的手机号码。
输出说明
输出应包括N行,每行包括一个对应的短号,输出应与输入的顺序一致。
输入范例
2
70711457490
47810534444
输出范例
657490
634444
个人总结:
1.直接将电话号码输入进数组里面,然后先输出一个6在输出后五位。
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int N;
cin >> N;
char phone[12];
for (int i = 0; i < N; i++) {
cin >> phone;
cout << "6";
for (int j = 6; j < 11; j++) {
cout << phone[j];
}
cout << endl;
}
return 0;
}
Also in the 19th century, the British mathematician and inventor Charles Babbage
worked out the principles of the modern digital computer.He conceived a number of machines, such as the Difference Engine, that were designed to handle complicated mathematical problems. Many historians consider Babbage and his associate, the mathematician Augusta Ada Byron, the true pioneers of the modern digital computer. One of Babbage's designs, the Analytical Engine, had many features of a modern computer. It had an input stream in the form of a deck of punched cards, a "store" for saving data, a "mill" for arithmetic operations, and a printer that made a permanent record. Babbage failed to put this idea into practice,though it may well have been technically possible at that date.
同样在 19 世纪,英国数学家、发明家查尔斯・巴贝奇提出了现代数字计算机的工作原理。他设计了多种机器,例如差分机,用于解决复杂的数学问题。许多历史学家认为,巴贝奇和他的合作者 ------ 数学家奥古斯塔・埃达・拜伦,是现代数字计算机真正的先驱。巴贝奇设计的分析机具备现代计算机的许多特征:它有以穿孔卡片为形式的输入装置、用于存储数据的 "存储器"、用于算术运算的 "运算单元",以及用于生成永久记录的打印机。尽管在当时技术上完全可行,巴贝奇最终还是没能将这一构想付诸实践。
Analogue computers began to be built in the late 19th century. Early models calculated by means of rotating shafts and gears. Numerical approximations of equations too difficult to solve in any other way were evaluated with such machines. Lord Kelvin built a mechanical tide predictor that was a specialized analogue computer. During World Wars I and II, mechanical and, later, electrical analogue computing systems were used as torpedo course predictors in submarines and as bombsight controllers in aircraft. Another system was designed to predict spring floods in the Mississippi River basin.
模拟计算机于 19 世纪末开始出现。早期机型通过旋转轴和齿轮进行计算,用来求解用其他方法难以处理的方程的数值近似解。开尔文勋爵制造过一台机械式潮汐预报机,属于专用模拟计算机。在第一次和第二次世界大战期间,机械式以及后来的电子式模拟计算系统,被用作潜艇鱼雷航向预测装置和飞机投弹瞄准控制器。还有一套系统被用来预测密西西比河流域的春季洪水。
During World War II, a team of scientists and mathematicians, working at Bletchley Park, north of London, created one of the first all-electronic digital computers: Colossus. By December 1943, Colossus,which incorporated 1,500 vacuum tubes, was operational. It was used by the team headed by Alan Turing?, in the largely successful attempt to crack German radio messages enciphered in the Enigma code.
第二次世界大战期间,一组科学家和数学家在伦敦北部的布莱切利园研制出了最早的全电子数字计算机之一 ------ 巨人计算机(Colossus)。到 1943 年 12 月,这台搭载了 1500 个真空管的计算机投入使用。在艾伦・图灵带领的团队手中,它被成功用于破解德军恩尼格玛密码加密的无线电情报。