题目:
输入一个十进制数,将其化成N进制数输出(2≤N≤16)。 输出结果时,大于等于10的数字用字母代替,A代表10,B代表11以此类推。
个人总结:
- char类型需要进行初始化,'\0'
- 注意数值范围,采用string字符串类型来输出
cpp
#include<iostream>
#include<string>
using namespace std;
string zhuan(int n,int k) {//2~10
string m = "";
if (n == 0)return "0";
while (n > 0) {
m =(char)(n % k+'0')+m;
n /= k;
}
return m;
}
string jin(int n, int k) {//11~16
string m = "";
if (n == 0)return "0";
while (n > 0) {
int t = n % k;
char c='\0';
if (t >= 10) {
c = t-10 + 'A';
}
else
{
c = char(t + '0');
}
m = c + m;
n /= k;
}
return m;
}
int main() {
int n, k;
while (cin >> n >> k) {
if (k <= 10 && k >= 2) {
cout << zhuan(n, k) << endl;
}
else if (k > 10) {
cout << jin(n, k) << endl;
}
}
return 0;
}
**题目:**以字符串形式输入仅有整数和加减(正负)号构成的表达式,输出该表达式的值。
个人总结:
- 循环结束后,要进行收尾
- 用op来存储上一个运算符,防止运算错位
- k需要计算运算符与运算符之间所有的数
cpp
#include<iostream>
#include<string>
using namespace std;
string remove(const string& a) {
string ans = "";
for (char c : a) {
if(c!=' ')ans += c;
}
return ans;
}
int main() {
string a;
while (getline(cin,a)) {
int sum = 0;
a = remove(a);
int k = 0;
char op = '+';
for (char s:a) {
if (s >= '0' && s <= '9') {
int t = s - '0';
k = k * 10 + t;
}
else if (s == '+' || s == '-') {
if (op == '+') {
sum += k;
}
else {
sum -= k;
}
k = 0;
op = s;
}
}
if (op == '+') {
sum += k;
}
else {
sum -= k;
}
cout << sum << endl;
}
return 0;
}
**题目:**从键盘输入一个字符串和一个字符,将输入字符从字符串中删除,输出新的字符串。如果字符串中没有此字符,则原样输出字符串。
个人总结:
- 用cin.get()不漏过b输入空格
cpp
#include<iostream>
#include<string>
using namespace std;
string remove84(const string& a) {
string ans = "";
for (char c : a) {
if (c != ' ')ans += c;
}
return ans;
}
int main() {
string a;
char b;
getline(cin, a);
cin.get(b);
string ans = "";
if (b == ' ') {
cout << remove84(a) << endl;
return 0;
}
for (char x : a) {
if (x != b) {
ans += x;
}
}
cout << ans << endl;
return 0;
}
Translation:
The first adding machine, a precursor of the digital computer,was devised in 1642 by the French scientist, mathematician, andphilosopher Blaise Pascal. This device employed a series of ten-toothed wheels, each tooth representing a digit from 0 to 9.The wheels were connected so that numbers could be added toeach other by advancing the wheels by a correct number ofteeth. In the 1670s the German philosopher and mathematicianGottfried Wilhelm Leibniz improved on this machine bydevising one that could also multiply.
第一个加法机器,是数字计算机的前身,在1642年由法国科学家、数学家和哲学家Blaise Pascal设计而成。这个设备应用了一系列10齿轮子,每个齿代表0-9的数字。齿轮相互连接,所以数字可以通过将齿轮推动一个正确的数字来相互进行加法运算。在1670年代,德国哲学家数学家Gottfried Wilhelm Leibniz通过创造出也可以进行乘法运算的机器,改进这个机器。
The French inventor Joseph-Marie Jacquard, in designing anautomatic loom, used thin, perforated wooden boards tocontrol the weaving of complicated designs. During the 1880sthe American statistician Herman Hollerith conceived theidea of using perforated cards, similar to Jacquard's boards, forprocessing data. Employing a system that passed punched cardsover electrical contacts, he was able to compile statistical information for the 1890 United States census.
法国发明家Joseph-Marie Jacquard设计了自动化织布机,使用纤细、带有孔洞的木板来控制复杂图案的编制。在1880年代之间,美国统计学家Herman Hollerith想到了使用带孔卡片,相似于Jacquard的板子,来处理数据。应用带孔卡片穿过电触点的系统,他能为1890年美国人口普查编译统计信息。
- perforated 带有孔洞的
Also in the 19th century, the British mathematician and inventorCharles Babbage worked out the principles of the moderndigital computer. He conceived a number of machines, such asthe Difference Engine, that were designed to handlecomplicated mathematical problems. Many historians considerBabbage and his associate, the mathematician Augusta AdaByron, the true pioneers of the modern digital computer. Oneof Babbage's designs, the Analytical Engine, had many featuresof a modern computer. It had an input stream in the form of a deck of punched cards, a "store" for saving data, a "mill" forarithmetic operations, and a printer that made a permanent record. Babbage failed to put this idea into practice, though itmay well have been technically possible at that date.
同样在19世纪,英国数学家和发明家Charles Babbage研究出来现代数字计算机的原理。他构想了许多机器,例如差分机,它被设计用于解决复杂的数学问题。许多历史学家认为Babbage和他的助手,数学家 Augusta AdaByron,是真正的现代数字计算机的先驱。它由以一叠穿孔卡片形式存在的输入流,一个储存数据的"仓库",一个进行算术运行的"磨坊",和一个永久记录的打印机。Babbage没有将这个想法运用于实际,尽管在那个时期它在技术上可以实现。
- Difference Engine 差分机
- associate 助手
- a deck of punched cards 一叠穿孔卡片
- arithmetic 算术
