题目一:输出一个字符串中的大写字母
题目要求
编写函数,将一个字符串中的大写字母作为字符串按顺序存放到另一个字符数组中。编写主函数,在主函数中输入字符串,调用函数找出其中的大写字母,存放到另一字符数组中,在主函数中输出大写字母组成的字符串。字符串的总长度不大于200,其中可能还有空格,字符串存放在字符数组中。
输入:带空格的字符串,长度小于等于200。
输出:一行,字符串中的大写字母。
输入输出样例:
输入;
abc DeF
输出:
DF
代码
#include <iostream>
#include <string>
using namespace std;
string DaXie(string a);
string DaXie(string a) {
string b;
for (int i = 0; a[i] != 0; i++) {
if (a[i] >= 'A' && a[i] <= 'Z') {
b.push_back(a[i]);
}
}
return b;
}
int main() {
int n = 200;
string a = "\n", b;
getline(cin, a);
//cout << a;
b = DaXie(a);
cout << b << endl;
}
题目二:找出字符串中的整数
题目要求
编写函数,找出字符串中的整数,将绝对值乘以2,符号不变, 结果作为返回值。字符串中没有整数时返回0.编写主函数,在函数中输入一个字符串,其中有一个整数,可能带正负号,调用函数找出整数按要求返回结果,在主函数中输出。字符串保存在字符数组中。
例如输入“abc-99def”返回结果为-198。
输入:一个字符串,其中含至多一个整数。
输出:一个整数
输入输出样例
输入:
abc-99def
输出:
-198
代码
#include <iostream>
using namespace std;
int *ZhengShu(int *a);
string ZhengShu(string a) {
string b;
for (int i = 0; a[i] != 0; i++) {
if (a[i] >= '0' && a[i] <= '9') {
if (i > 1 && a[i - 1] == '-') {
b.push_back(a[i - 1]);
b.push_back(a[i]);
} else {
b.push_back(a[i]);
}
}
if (i == 0 && a[i] == '-') {
b.push_back(a[i]);
}
}
return b;
}
int main() {
string a = "\n", b;
getline(cin, a);
b = ZhengShu(a);
int c = atoi(b.c_str());
cout << c * 2 << endl;
}
题目三:矩阵转置
题目要求
写一个函数,使给定的一个nn的二维整型数组转置,即行列互换。注意,要在一个矩阵中完成转置运算!!!编写主函数,在主函数中输入矩阵,调用函数转置,在主函数中输出。
输入:输入一个正整数n,换行后输入nn的数组元素(n<=10)
输出: 输出数组的转置 n行,每行相邻数字之间有空格末尾无空格。
【输入输出样例】
输入:
3
1 2 3
4 5 6
7 8 9
输出:
1 4 7
2 5 8
3 6 9
代码
#include <iostream>
using namespace std;
void swap(int &a, int &b) { //交换函数
int temp = a;
a = b;
b = temp;
}
void Transpose(int n, int *b) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
break;
} else {
swap(b[n * i + j], b[n * j + i]);
}
}
}
for (int i = 0; i < n; i++) {
cout << b[n * i];
for (int j = 1; j < n; j++) {
cout << ' ' << b[n * i + j];
}
cout << endl;
}
}
int main() {
int n;
cin >> n;
int b[n][n] = {0};
string x[n];
string s;
s = "\n";
getline(cin, s);
char c;
for (int k = 0; k < n; k++) {
int i = 0;
int a[n] = {0};
while ((c = getchar()) != '\n') {
if (c != ' ') { //把这句判断条件改动
ungetc(c, stdin);
cin >> a[i++];
}
}
for (int j = 0; j < n; j++) {
//cout << a[j];
b[k][j] = a[j];
}
}
/*
for (int i = 0; i < n; i++) {//错误的,这种方式只能存范围1-9的元素,10以上会出错
x[i] = {"\n"};
getline(cin, x[i]);
int k = 0, j = 0;
for (int j = 0; x[i][j] != '\0'; j++) {
while (x[i][j] != ' ' && x[i][j] != '\0') {
//cout << x[i][j];
b[i][k] = x[i][j] - '0'; //也可以直接给b[j][i]赋值
k++;
j++;
}
}
}
*/
Transpose(n, *b);
}
题目四:牛顿法迭代求根
题目要求
用牛顿迭代法求根。方程为ax³+bx²+cx+d=0,系数a,b,c,d的值依次为1,2,3,4。求x在x0附近的一个实根。数据类型用float。
输入:输入x0和迭代次数n。
输出:输出一个实数(用printf的%.6f格式输出)
【输入输出样例】
输入:
1 10
输出:
-1.650629
代码
#include <iostream>
#include <cmath>
using namespace std;
float fd(float x);
float newton();
float fd(float x) {
int a = 1, b = 2, c = 3, d = 4;
float f = a * pow(x, 3) + b * pow(x, 2) + c * x + d;
float f1 = 3 * a * pow(x, 2) + 2 * b * x + c;
return (f / f1);
}
float newton(float x0, int n) { //初值和迭代次数
float k = x0;
float x = x0 - fd(x0);
int i = 0;
int s = 0;
while (i < n) {
//cout << k << endl;
k = x;
i++;//循环次数
x = x - fd(x);
}
//cout << i << endl;
return k;
}
int main() {
float x0, z;
int n;
cin >> x0 >> n;
z = newton(x0, n);
printf("%.6f", z);
}
题目五:输入、排序、查找
题目要求
分别编写函数输入数组元素(输入-999表示结束)、从小到大排序数组元素、用二分法在有序数组中查找(返回排序后的元素的下标,找不到时返回-1)。
编写主函数,调用函数进行输入、排序和查找。数组元素不超过100个,均为整数。
输入格式:
两行,第1行为输入的数组元素,以-999结束;第2行输入5个待查找的元素。数据用空格隔开。
输出格式:
5行,每行是查找到的元素的下标,找不到时结果为-1。
输入样例:
2 1 3 4 5 -999
2 3 4 -10 10
输出样例:
1
2
3
-1
-1
代码
#include <iostream>
#include <cmath>
using namespace std;
float fd(float x);
float newton();
float fd(float x) {
int a = 1, b = 2, c = 3, d = 4;
float f = a * pow(x, 3) + b * pow(x, 2) + c * x + d;
float f1 = 3 * a * pow(x, 2) + 2 * b * x + c;
return (f / f1);
}
float newton(float x0, int n) { //初值和迭代次数
float k = x0;
float x = x0 - fd(x0);
int i = 0;
int s = 0;
while (i < n) {
//cout << k << endl;
k = x;
i++;//循环次数
x = x - fd(x);
}
//cout << i << endl;
return k;
}
int main() {
float x0, z;
int n;
cin >> x0 >> n;
z = newton(x0, n);
printf("%.6f", z);
}