题目:1.字符串统计
问题描述
对于给定的一个字符串,统计其中小写字母出现的次数。
输入说明
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
输出说明
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
输入范例
2
abadvbsbasdfWRWEFASDFASDFwefsadfasdfadef934577&%^&*(
adfasdferdasd233dsf234radfassdfasdf**HHIKKK
输出范例
27
29
个人总结:
1.直接用字符串进行比较。
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int n;
cin >> n;
cin.ignore(); // 清除换行符
char str[1000];
for (int i = 0; i < n; i++) {
cin.getline(str, 1000);
int count = 0;
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] >= 'a' && str[j] <= 'z') {
count++;
}
}
cout << count << endl;
}
return 0;
}
题目:2.弟弟的作业
问题描述
你的弟弟刚做完了"100以内数的加减法"这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。
输入说明
输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
输出说明
输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
输入范例
55+12=67
15-8=7
100-35=?
83-50=33
4-3=6
81+5=21
输出范例
3
个人总结:
1.直接提取一整行,通过符号进行划分。
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char line[20];
int correct = 0;
while (cin >> line) {
int a = 0, b = 0, c = 0;
char op, eq;
int i = 0;
// 提取第一个数a
while (line[i] != '+' && line[i] != '-') {
a = a * 10 + (line[i++] - '0');
}
op = line[i++]; // 提取运算符
// 提取第二个数b
while (line[i] != '=') {
b = b * 10 + (line[i++] - '0');
}
eq = line[i++]; // 提取等号
// 提取答案c(处理?的情况)
if (line[i] == '?') {
continue;
}
while (line[i] != '\0') {
c = c * 10 + (line[i++] - '0');
}
// 判断答案是否正确
if (op == '+') {
if (a + b == c) correct++;
} else {
if (a - b == c) correct++;
}
}
cout << correct << endl;
return 0;
}
题目:3.字符串排序
问题描述
明明刚刚开始学英文,对于26个英文字母的顺序总是记不住,每次默写英文字母的时候,顺序总是前后颠倒。明明的爸爸对此相当着急,想有没有很好的办法来帮助明明记住字母的顺序。一天,明明的爸爸突然想到了一个游戏,能够帮助明明记住英文字母。这个游戏如下,给明明一个任意长度的英文字串,其中只包含小写字母,然后让明明对字符串中的字母排序,然后再把排完序的字符串写出来,如果连续写对10次的话,明明的爸爸就会奖励他,带他出去吃KFC。 例如:有一个字符串为asdf,经过排序后的字符串就是adfs。 明明显然对这个游戏非常感兴趣(其实明明更感兴趣的是那顿KFC),接受了他爸爸的提议,玩起了这个游戏。但是明明的爸爸在出题目考明明的时候,自己却犯了难,用英文随便写出一个无序的英文字符串是很简单的,但是排序的话却要费一些功夫,而且还不能出错,否则就无法知道明明做的对不对了。于是明明的爸爸请你帮忙,帮他写一个排序程序,来输出排序后的字符串。
明明爸爸的问题可以归结为:输入一行字符串,全部由小写字母构成,对字符串按26个英文字母的先后顺序进行排序,然后输出。
输入说明
你写的程序要求从标准输入设备中读入测试数据作为你所写程序的输入数据。标准输入设备中有多组测试数据,每组测试数据仅占一行,每行由一个字符串组成,字符串中只包含小写字母,字符串的长度不超过100个字符。每组测试数据与其后一组测试数据之间没有任何空行,第一组测试数据前面以及最后一组测试数据后面也都没有任何空行。
输出说明
对于每一组测试数据,你写的程序要求计算出一组相应的运算结果,并将这一组运算结果作为你所写程序的输出数据依次写入到标准输出设备中。每组运算结果为一个排序后的字符串。每组运算结果单独形成一行数据,其行首和行尾都没有任何空格,每组运算结果与其后一组运算结果之间没有任何空行,第一组运算结果前面以及最后一组运算结果后面也都没有任何空行。
输入范例
asdf
shflsahfslfdjsldfsjd
输出范例
adfs
adddffffhhjjlllsssss
个人总结:
1.统计每个字母出现的次数,在按字母顺序拼接。
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[101], sorted[101];
while (cin >> str) {
int count[26] = {0};
int len = strlen(str);
// 统计每个字母出现次数
for (int i = 0; i < len; i++) {
count[str[i] - 'a']++;
}
// 按字母顺序拼接字符串
int idx = 0;
for (int i = 0; i < 26; i++) {
while (count[i]--) {
sorted[idx++] = 'a' + i;
}
}
sorted[idx] = '\0';
cout << sorted << endl;
}
return 0;
}
Independently of this, in the United States, a prototype electronic machine had been built as early as 1939, by John Atanasoff' and Clifford Berry at Iowa State College? This prototype and later research were completed quietly and later overshadowed by the development of the Electronic Numerical Integrator And Computer (ENIAC*) in 1945. ENIAC was granted a patent, which was overturned decades later, in 1973, when the machine was revealed to have incorporated principles first used in the Atanasoff-Berry Computer.
同样在美国,早在 1939 年,爱荷华州立学院的约翰・阿塔纳索夫与克利福德・贝里就已独立研制出一台电子原型机。这项原型机及后续研究是在低调中完成的,后来便被 1945 年电子数字积分计算机(ENIAC)的问世所掩盖。ENIAC 曾获得专利,但数十年后的 1973 年该专利被撤销,因为有证据表明,这台机器采用了最早在阿塔纳索夫‑贝里计算机中使用的原理。
ENIAC (see Figure 1A-1) contained 18,000 vacuum tubes and had a speed of several hundred multiplications per minute, but originally its program was wired into the processor and had to be manually altered.Later machines were built with program storage, based on the ideas of the Hungarian-American mathematician John von Neumann. The instructions,like the data, were stored within a "memory", freeing the computer from the speed limitations of the paper-tape reader during execution and permitting problems to be solved without rewiring the computer.
ENIAC(见图 1A‑1)装有 18000 个真空管,每分钟能完成几百次乘法运算,但最初它的程序是通过线路接在处理器里的,必须手动改接线路才能更换程序。后来的计算机依据匈裔美国数学家约翰・冯・诺依曼的思想,采用了程序存储结构。指令和数据一样被存放在 "存储器" 中,使计算机在运行时不再受纸带阅读器速度的限制,也无需重新接线就能解决不同问题。
The use of the transistor in computers in the late 1950s marked the advent of smaller, faster, and more versatile logical elements than were possible with vacuum-tube machines. Because transistors use much less power and have a much longer life, this development alone was responsible for the improved machines called second-generation computers.Components became smaller, as did inter-component spacings, and the system became much less expensive to build.
20 世纪 50 年代末,晶体管在计算机中的应用标志着第二代计算机的到来 ------ 相比真空管机器,它体积更小、速度更快、逻辑功能更强。由于晶体管功耗更低、寿命更长,仅这一项进步就催生了性能更优的新一代计算机。元器件变得更小,元件间距也随之缩小,系统制造成本大幅降低。