C++语言版实现代码
在写习题前想先记录自己在学习过程中碰到的string相关知识点。
字符串相关函数及操作:
1、连接 str1 和 str2:
2、字符串的总长度:
3、复制字符串 str2 到字符串 str1:
4、比较字符串 str2 到字符串 str1:strcmp(s1, s2):
5、查询字符串 str 中字符 ch 的第一次出现的位置:
6、查询向字符串 str1 中字符串 str2 的第一次出现的位置:
使用cin输入字符串方式:
1、接受一个字符串,遇“空格”、“TAB”、“回车”自动结束:
#include
using namespace std;
main ()
{
char a[20];
cin>>a;
cout<<a<<endl;
}
输入:shfus iaad!
输出:shfus
2、接受一个可以带有空格的字符串
#include
using namespace std;
main () {
char a[10];
cin.get(a,10);
cout<<a<<endl;
}
输入:jsh ahf iauf a
输出:jsh ahf i
注意:a数组的最后一位是存储’\0’的。
#include
using namespace std;
main ()
{
char m[10];
cin.getline(m,5);
cout<<m<<endl;
}
输入:sh ahf iauf a
输出:sh a
注意:a数组的最后一位是存储’\0’的。
#include
#include
using namespace std;
main ()
{
string str;
getline(cin,str);
cout<<str<<endl;
}
输入:sh ahf iauf a
输出:sh ahf iauf a
注意:和cin.getline()类似,但是cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数。
题目1(必要的存储量):数组可以用来保存很多数据,但在一些情况下,并不需要把 数据保存下来。下面哪些题目可以不借助数组,哪些必须借助数组?请编程实现。假设输入 只能读一遍
#include
int main{
int count = 0;
int x;
while (cin >> x)
count++;
cout << count << endl;
return 0;
}
#include
int main{
const int INF = 10000000;
int count = 0, s = 0;
int x;
float min = INF, max = -INF;
while (cin >> x) {
if (min > x)
min = x;
if (max < x)
max = x;
s += x;
count++;
}
cout << max << " " << min << " " << (float)s/count<< endl;
return 0;
}
#include
int main{
int a, b, c;
cin >> a >> b;
int difference = abs(a - b);
while (cin >> c) {
if (abs(a - c) < difference) {
difference = abs(a - c);
b = c;
}
else if (abs(b - c) < difference) {
difference = abs(b - c);
a = c;
}
}
cout << a << " " << b << endl;
return 0;
}
#include
int main{
int a, b, c;
cin >> a >> b;
if (a < b) { // 始终保持a为大,b为小
c = a;
a = b;
b = a;
}
while (cin >> c) {
if (a < c) {
b = a;
a = c;
}
else if (b < c)
b = c;
}
cout << b << endl;
return 0;
}
#include
#include
int main{
float variance, mean, a[1000], s=0;
int i = 0;
while (cin >> a[i]) {
s += a[i];
i++;
}
mean = s / i;
s = 0;
for (int j = 0; j < i; j++)
s += pow(a[j] - mean, 2);
variance = s / i;
cout << fixed << setprecision(3) << variance << endl;
return 0;
}
#include
#include
int main{
float mean, a[1000], count = 0, s = 0;
int i = 0;
while (cin >> a[i]) {
s += a[i];
i++;
}
mean = s / i;
for (int j = 0; j < i; j++)
if (a[j] < mean)
count++;
cout << count << endl;
return 0;
}
题目2(统计字符1的个数):下面的程序意图在于统计字符串中字符1的个数,可惜有 瑕疵:
#include
#define maxn 10000000 + 10
int main() {
char s[maxn];
scanf("%s", s);
int tot = 0;
for(int i = 0; i < strlen(s); i++)
if(s[i] == 1)
tot++;
printf("%d\n", tot);
}
该程序至少有3个问题,其中一个导致程序无法运行,另一个导致结果不正确,还有一 个导致效率低下。你能找到它们并改正吗?
改正后:
#include
#include
using namespace std;
#define maxn 10000 + 10 // 原来的导致程序错误,原数值过大
int main()
{
char s[maxn];
cin.get(s, maxn);
int tot = 0;
int len = strlen(s); // 原来的strlen(s)导致效率低下,每次循环都需要计算一边s的长度
for (unsigned int i = 0; i < len; i++)
if (s[i] == '1') // 原来的导致结果不正确,字符元素定义为字符型,不是整型
tot++;
cout << tot << endl;
return 0;
}
改良后可以看直观效率的代码,用运行时间来直观效率
#include
#include
#include
using namespace std;
#define maxn 10000 + 10
int main()
{
char s[maxn];
cin.get(s, maxn);
int len = strlen(s);
int tot = 0,tot2=0;
clock_t start, finish, start2, finish2;
double duration, duration2;
int n = 0;
start = clock();
while (n < 1000000) {
for (unsigned int i = 0; i < strlen(s); i++)
if (s[i] == '1') // 导致结果不正确
tot++;
n++;
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
n = 0;
start2 = clock();
while (n < 1000000) {
for (unsigned int j = 0; j < len; j++)
if (s[j] == '1') // 导致结果不正确
tot2++;
n++;
}
finish2 = clock();
duration2 = (double)(finish2 - start2) / CLOCKS_PER_SEC;
cout << tot << endl;
cout << tot2 << endl;
cout << "for中使用strlrn(s)所用的时间:" << duration << endl;
cout << "for中使用len(变量)所用的时间:" << duration2 << endl;
return 0;
}