3.11
#include<iostream>
#include<string>
using namespace std;
int main()
{
float score;
cin >> score;
while (score > 100 || score < 0)
{
cout << "输入成绩范围错误!重新输入:" << endl;
cin >> score;
}
switch (int(score / 10))
{
case 10:
case 9:cout << 'A' << endl; break;
case 8:cout << 'B' << endl; break;
case 7:cout << 'C' << endl; break;
case 6:cout << 'D' << endl; break;
default:cout << 'E' << endl;
}
return 0;
}
3.12
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n,i=0, a[5],cnt=0;
cin >> n;
int temp = n;
while (temp)
{
a[i++] = temp % 10;
temp = temp / 10;
cnt++;
}
cout << "该数为" << cnt << "位数" << endl;
cout << "打印每位数:" << endl;
for (int i = cnt - 1; i >= 0; i--)
cout << a[i] << ' ';
cout << endl;
cout << "逆序:" << endl;
for (int i = 0; i < cnt; i++)
cout << a[i] << ' ';
cout << endl;
return 0;
}
3.14
#include<iostream>
#include<string>
using namespace std;
void fun(int &a, int &b, int &c, int &d)
{
if (a > b)
swap(a, b);
if (a > c)
swap(a, c);
if (a > d)
swap(a, d);
if (b > c)
swap(b, c);
if (b > d)
swap(b, d);
if (c > d)
swap(c, d);
}
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
fun(a, b, c, d);
cout << "从小到大:" << endl;
cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
return 0;
}
3.15
#include<iostream>
#include<string>
using namespace std;
int gcd(int a, int b) {
if (a%b == 0)
return b;
else
return gcd(b, a%b);
}
int main()
{
int m, n;
cin >> m >> n;
cout << "最大公约数:" << gcd(m, n) << endl << "最小公倍数:" << m * n / gcd(m, n) << endl;
return 0;
}
3.16
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
getline(cin, a);
int len = a.size();
int digit = 0, letter = 0, space = 0, other = 0,i=0;
for (int i = 0; i < len; i++)
{
if (a[i] == ' ')
space++;
else if (a[i] >= 'a'&&a[i] <= 'z' || a[i] >= 'A'&&a[i] <= 'Z')
letter++;
else if (a[i] >= '0'&&a[i] <= '9')
digit++;
else
other++;
}
cout << "数字:" << digit << endl;
cout << "字母:" << letter << endl;
cout << "空格:" << space << endl;
cout << "其他:" << other << endl;
return 0;
}
2.17
#include<iostream>
#include<string>
using namespace std;
int main()
{
long sum = 0, t = 0;
int n, a;
cin >> a >> n;
t = a;
for (int i = 1; i <= n; i++)
{
sum = sum + t;
t = t * 10 + a;
}
cout << sum << endl;
return 0;
}
3.18
#include<iostream>
#include<string>
using namespace std;
long fun(int n)
{
if (n == 1)
return 1;
else
return n * fun(n - 1);
}
int main()
{
long sum = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
sum += fun(i);
cout << "1!+2!+3!+4!+...+n!=" << sum << endl;
return 0;
}
3.19
#include<iostream>
#include<string>
using namespace std;
bool fun(int n)
{
int i = n / 100;//百位
int j = n % 10;//个位
int k = n / 10 % 10;//十位
if ((i*i*i + j * j*j + k * k*k) == n)
return true;
return false;
}
int main()
{
for (int i = 100; i < 999; i++)
if(fun(i))
cout << i << endl;
return 0;
}
3.20
#include<iostream>
#include<string>
using namespace std;
bool fun(int n)
{
int sum = 0;
for (int i = 1; i <= n / 2; i++)
{
if (n%i == 0)
sum += i;
}
if (sum == n)
return true;
return false;
}
int main()
{
for (int i = 2; i <= 1000; i++)
if (fun(i))
cout << i << endl;
return 0;
}
3.21
#include<iostream>
#include<string>
using namespace std;
int main()
{
double f1 = 1, f2 = 2, temp, t, sum = 0;
for (int i = 1; i <= 20; i++)
{
t = f2 / f1;
sum += t;
t = f2;
f2 += f1;
f1 = t;
}
cout << sum << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int sum = 1;
for (int i = 1; i < 10; i++)
{
sum = 2 * (sum + 1);
}
cout << sum << endl;
return 0;
}
3.24
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= 2 * i - 1; j++)
cout << "*";
cout << endl;
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 1; j <= 2 * i - 1; j++)
cout << "*";
cout << endl;
}
return 0;
}
3.25
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a, b, c;
for(a='x';a<='z';a++)
for (b = 'x'; b <= 'z'; b++)
{
if (b != a)
for (c = 'x'; c <= 'z'; c++)
if (c != a && c != b)
if (a != 'x'&&c != 'x'&&c != 'z') {
cout << "A--" << a << endl;
cout << "B--" << b << endl;
cout << "C--" << c << endl;
}
}
return 0;
}
4.1
#include<iostream>
#include<string>
using namespace std;
int gcd(int a, int b)
{
if (a%b == 0)
return b;
else
return gcd(b, a%b);
}
int hcd(int a, int b) {
return a * b / gcd(a, b);
}
int main()
{
int a, b;
cin >> a >> b;
cout << "最大公约数:" << gcd(a, b) << endl;
cout << "最小公倍数:" << hcd(a, b) << endl;
return 0;
}
4.2
#include<iostream>
#include<string>
using namespace std;
double dis = 0, x1, x2, p, q;
void great(double a, double b)
{
x1 = (-b + sqrt(dis)) / (2 * a);
x2 = (-b - sqrt(dis)) / (2 * a);
}
void equal(double a, double b)
{
x1 = x2 = (-b) / (2 * a);
}
void small(double a, double b)
{
p = -b / (2 * a);
q = sqrt(-dis) / (2 * a);
}
int main()
{
double a, b, c;
cin >> a >> b >> c;
dis = b * b - 4 * a*c;
if (dis > 0)
{
great(a, b);
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
}
else if (dis == 0) {
equal(a, b);
cout << "x1=x2=" << x1 << endl;
}
else
{
small(a, b);
cout << "x1=" << p << "+" << q << "i" << endl;
cout << "x2=" << p << "-" << q << "i" << endl;
}
return 0;
}
4.3
#include<iostream>
#include<string>
using namespace std;
bool fun(int n)
{
for (int i = 2; i <= sqrt(n); i++)
if (n%i == 0)
return false;
return true;
}
int main()
{
int n;
cin >> n;
if (fun(n))
cout << n << "是素数!" << endl;
else
cout << n << "不是素数!" << endl;
return 0;
}
4.4
#include<iostream>
#include<string>
using namespace std;
long fun(int n)
{
if (n == 1)
return 1;
else
return n * fun(n - 1);
}
int main()
{
int a, b, c;
cin >> a >> b >> c;
cout << "a!+b!+c!=" << fun(a) + fun(b) + fun(c) << endl;
return 0;
}
4.7
#include<iostream>
#include<string>
using namespace std;
bool fun(int n)
{
for (int i = 2; i <= sqrt(n); i++)
if (n%i == 0)
return false;
return true;
}
void gotbaha(int n)
{
for (int i = 3; i < n; i++)
{
if (fun(i) && fun(n - i)&&i<=(n-i))
cout << n << "=" << i << "+" << n - i << endl;
}
}
int main()
{
int n;
cin >> n;
gotbaha(n);
return 0;
}
4.8
#include<iostream>
#include<string>
using namespace std;
bool fun(int y)
{
if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
return true;
return false;
}
int main()
{
int y, m, d, days = 0;
int tab[13] = { 0,31,30,31,30,31,30,31,31,30,31,30,31 };
cin >> y >> m >> d;
if (fun(y) && m > 2)
days++;
for (int i = 1; i < m; i++)
days += tab[i];
days += d;
cout << y << "/" << m << '/' << d << "是该年的第" << days << "天" << endl;
return 0;
}
4.9
#include<iostream>
#include<string>
using namespace std;
double fun(double n,double x)
{
if (n == 0)
return 1;
if (n == 1)
return x;
else
return ((2 * n - 1)*x - fun(n - 1, x) - (n - 1)*fun(n - 2, x)) / n;
}
int main()
{
double n, x;
cin >> n >> x;
cout << fun(n, x) << endl;
return 0;
}
4.9
#include<iostream>
#include<string>
using namespace std;
void move(char x, char y)
{
cout << x << "-->" << y << endl;
}
void hanoi(int n, char one, char two, char three)
{
if (n == 1)
move(one, three);
else
{
hanoi(n - 1, one, three, two);
move(one, three);
hanoi(n - 1, two, one, three);
}
}
int main()
{
int m;
cin >> m;
hanoi(m, 'A', 'B', 'C');
return 0;
}
4.10
#include<iostream>
#include<string>
using namespace std;
int main()
{
void convert(int n);
int number;
cout << "input an integer:";
cin >> number;
cout << "output:" << endl;
if (number < 0)
{
cout << "-";
number = -number;
}
convert(number);
cout << endl;
return 0;
}
void convert(int n)
{
int i;
char c;
if ((i = n / 10) != 0)
convert(i);
c = n % 10 + '0';
cout << " " << c;
}
4.11
#include<iostream>
#include<string>
using namespace std;
int main()
{
int fun(int n);
int n,sum=0;
cin >> n;
for (int i = 1; i <= n; i++)
sum += fun(i);
cout << sum<< endl;
return 0;
}
int fun(int n)
{
return n * n;
}
4.12
#include<iostream>
#include<string>
using namespace std;
int main()
{
double fun(int, int, int);
double fun2(int, int, int);
int a, b, c;
cin >> a >> b >> c;
cout << "s=1/2(a+b+c)=" << fun(a, b, c) << endl;
cout << "area=" << fun2(a, b, c) << endl;
return 0;
}
double fun(int a,int b,int c)
{
return double(1) / (2 * (a + b + c));
}
double fun2(int a, int b, int c)
{
double s = double(1) / (2 * (a + b + c));
double area = s * (s - a)*(s - b)*(s - c);
return area;
}
选择排序 冒泡排序
#include<iostream>
#include<string>
using namespace std;
//选择排序
void sort(int a[], int n)
{
int t;
for(int i=0;i<n-1;i++)
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
//冒泡排序
void pop(int a[], int n)
{
int t, k;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (int j = i + 1; j < n; j++)
if (a[k] > a[j])
k = j;
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
int main()
{
int a[100],b[100], n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
b[i] = a[i];
}
cout << "选择排序:" << endl;
sort(a, n);
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl<< "冒泡排序:" << endl;
pop(b, n);
for (int i = 0; i < n; i++)
cout << b[i] << ' ';
cout << endl;
return 0;
}
5.1
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int i, j, n, a[101];
for (i = 1; i <= 100; i++)
a[i] = i;
a[1] = 0;
for(i=2;i<sqrt(100);i++)
for (j = i + 1; j <= 100; j++)
{
if (a[i] != 0 && a[j] != 0)
if (a[j] % a[i] == 0)
a[j] = 0;
}
cout << endl;
for (i = 1, n = 0; i <= 100; i++)
{
if (a[i] != 0)
{
cout << setw(5) << a[i] << " ";
n++;
}
if (n == 10)
{
cout << endl;
n = 0;
}
}
return 0;
}
5.2
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void sort(int a[], int n)
{
int t, k;
for (int i = 0; i < n - 1; i++)
{
k = i;
for (int j = i + 1; j < n; j++)
if (a[k] > a[j])
k = j;
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
int main()
{
int a[10];
for (int i = 0; i < 10; i++)
cin >> a[i];
sort(a, 10);
cout << "选择排序后的数组:" << endl;
for (int i = 0; i < 10; i++)
cout << a[i] << ' ';
cout << endl;
return 0;
}
5.3
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int a[3][3], sum = 0;
cout << "输入3*3阶的矩阵:" << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
if (i == 0)
sum += a[i][j];
}
cout << "三阶矩阵的对角线之和为:" << sum << endl;
return 0;
}
5.4
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
void fun(int a[], int n, int key)
{
int pos,i;
for (i = 0; i < n; i++)
if (key < a[i]) {
pos = i;
break;
}
if (i == n)
a[n] = key;
else
{
for (int j = n - 1; j >= pos; j--)
a[j + 1] = a[j];
a[pos] = key;
}
}
void sort(int a[], int n)
{
int t;
for(int i=0;i<n-1;i++)
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
int main()
{
int a[100], n, key;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "数组排序" << endl;
sort(a, n);
cout << "排序后的数组:" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
cout << "输入你要插入的数据:" << endl;
cin >> key;
fun(a, n, key);
cout << "插入后的数组:" << endl;
for (int i = 0; i < n + 1; i++)
cout << a[i] << ' ';
cout << endl;
return 0;
}
5.5
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void fun(int a[], int n);
int a[100], n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "逆序:" << endl;
fun(a, n);
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
return 0;
}
void fun(int a[], int n)
{
int t;
for (int i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
5.6
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
int a[10][10];
for (int i = 0; i < 10; i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for(int i=2;i<10;i++)
for (int j = 1; j < i; j++)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= i; j++)
cout << a[i][j] << ' ';
cout << endl;
}
return 0;
}
5.7
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
const int n = 4, m = 5;
int i, j, a[n][m], max, maxj;
bool f;
cout << "输入4*5阶矩阵:" << endl;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
for (int i = 0; i < n; i++)
{
max = a[i][0], maxj = 0;
f = true;
for(int j=0;j<m;j++)
if (a[i][j] > max)
{
max = a[i][j];
maxj = j;
}
for (int k = 0; k < n; k++)
{
if (a[k][j] < max)
{
f = false;
break;
}
}
if (f) {
cout << "a[" << i << "][" << "[" << maxj << "]=" << max << endl;
break;
}
}
if (!f)
{
cout << "不存在!" << endl;
}
return 0;
}
5.8
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
int a[15] = { 2,4,5,8,10,13,15,17,20,23,25,27,30,32,37 };
int key;
cout << "输入你要查找的数:" << endl;
cin >> key;
int l = 0,r = 14;
bool f = 0;
while (l <= r)
{
int mid = (l + r) / 2;
if (a[mid] == key)
{
cout << mid << endl;
f = 1;
break;
}
else if (a[mid] > key)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
if (!f)
cout << "无此数" << endl;
return 0;
}
5.9
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
bool fun(int y)
{
if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
return true;
return false;
}
int main()
{
int tab[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int y, m, d, days = 0;
cin >> y >> m >> d;
if (fun(y) && m > 2)
days++;
for (int i = 1; i < m; i++)
days += tab[i];
days += d;
cout << y << "/" << m << "/" << d << "是该年的第" << days << "天" << endl;
return 0;
}
5.10
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
string s[3];
for (int i = 0; i < 3; i++)
{
int digit = 0, letter = 0, space = 0, other = 0;
getline(cin, s[i]);
int len = s[i].size();
for (int j = 0; j < len; j++)
{
if (s[i][j] == ' ')
space++;
else if (s[i][j] >= 'a'&&s[i][j] <= 'z' || s[i][j] >= 'A'&&s[i][j] <= 'Z')
letter++;
else if (s[i][j] >= '0'&&s[i][j] <= '9')
digit++;
else
other++;
}
cout << "数字:" << digit << endl;
cout << "字母:" << letter << endl;
cout << "空格:" << space << endl;
cout << "其他:" << other << endl;
}
return 0;
}
5.11
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i ; j++)
cout << " ";
for (int k = 1; k <= n; k++)
cout << "*";
cout << endl;
}
return 0;
}
5.13
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
char *str(char *a, char *b)
{
char *p = a, *q = b;
while (*p)
p++;
while (*q) {
*p = *q;
p++;
q++;
}
*p = '\0';
return a;
}
int main()
{
char a[100], b[50];
cout << "输入字符串a:" << endl;
gets_s(a);
cout << "输入字符串b:" << endl;
gets_s(b);
cout << "自定义函数合并:" << endl;
cout << str(a, b) << endl;
cout << a << endl;
string s1, s2;
cout << "输入string1:" << endl;
cin >> s1;
cout << "输入string2:" << endl;
cin >> s2;
cout << "string 合并:" << endl;
string s3 = s1 + s2;
cout << s3 << endl;
return 0;
}
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
char a[100], b[50];
cout << "输入字符串a:" << endl;
gets(a);
cout << "输入字符串b:" << endl;
gets(b);
strcat(a, b);
cout << "合并后的字符串:" << a << endl;
return 0;
}
3.14
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
string s[10];
int n;
cin >> n;
getchar();
for (int i = 0; i < n; i++)
getline(cin, s[i]);
//冒泡排序比较string字符串
for(int i=0;i<n-1;i++)
for (int j = 0; j < n - i - 1; j++)
{
if (s[j] > s[j + 1])
{
string t = s[j];
s[j] = s[j + 1];
s[j + 1] = t;
}
}
for (int i = 0; i < n; i++)
cout << s[i] << endl;
return 0;
}
3.15
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
string a[10];
int n;
cin >> n;
getchar();
for (int i = 0; i < n; i++)
{
getline(cin, a[i]);
if (a[i][0] == 'A')
cout << a[i] << endl;
}
return 0;
}
5.16
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
char a[100];
gets_s(a);
int len = strlen(a);
for (int i = len - 1; i >= 0; i--)
cout << a[i];
cout << endl;
return 0;
}
3.17
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
struct Student {
string name;
string num;
float score;
}stu[20];
int main()
{
int n, a[100] = { 0 };
cin >> n;
cout << "输入" << n << "个学生的信息:姓名 学号 成绩:" << endl;
for (int i = 0; i < n; i++)
{
cin >> stu[i].name >> stu[i].num >> stu[i].score;
if (stu[i].score < 60)
a[i] = 1;
}
cout << "不及格学生信息:" << endl;
for(int i=0;i<n;i++)
if (a[i] == 1)
{
cout << "name:" << stu[i].name << endl;
cout << "number:" << stu[i].num << endl;
cout << "score:" << stu[i].score << endl;
}
return 0;
}
6.1
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
void fun(int &a, int &b, int &c)
{
if (a > b)
swap(a, b);
if (a > c)
swap(a, c);
if (b > c)
swap(b, c);
}
int main()
{
int a, b, c;
cin >> a >> b >> c;
fun(a, b, c);
cout << a << ' ' << b << ' ' << c << endl;
return 0;
}
6.2
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
void fun(string &a, string &b, string &c)
{
void swap(string &, string &);
if (a > b)
swap(a, b);
if (a > c)
swap(a, c);
if (b > c)
swap(b, c);
}
void swap(string &a, string &b)
{
string t;
t = a;
a = b;
b = t;
}
int main()
{
string a, b, c;
cout << "输入三个字符串:" << endl;
getline(cin, a);
getline(cin, b);
getline(cin, c);
fun(a, b, c);
cout << "输出排序后的字符串:(从小到大)" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
6.3
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
void input(int a[], int n)
{
cout << "输入" << n << "个数:" << endl;
for (int i = 0; i < n; i++)
cin >> a[i];
}
void change(int a[], int n)
{
int min, max, x=0, y=0;
min = max = a[0];
for (int i = 0; i < n; i++)
{
if (min > a[i])
{
min = a[i];
x = i;
}
if (max < a[i])
{
max = a[i];
y = i;
}
}
int t = a[x];
a[x] = a[0];
a[0] = t;
t = a[y];
a[y] = a[n - 1];
a[n - 1] = t;
}
void put(int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
int a[10];
input(a, 10);
change(a, 10);
put(a, 10);
return 0;
}
6.4
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void move(int *array, int n, int m);
int number[20], n, m, i;
cout << "输入数据个数:" << endl;
cin >> n;
cout << "输入具体的数据:" << endl;
for (int i = 0; i < n; i++)
cin >> number[i];
cout << "输入你想要移动的位数:" << endl;
cin >> m;
move(number, n, m);
cout << "移动后的数组:" << endl;
for (int i = 0; i < n; i++)
cout << number[i] << ' ';
cout << endl;
return 0;
}
void move(int *array, int n, int m)
{
int *p, array_end;
array_end = *(array + n - 1);
for (p = array + n - 1; p > array; p--)
*p = *(p - 1);
*array = array_end;
m--;
if (m > 0)
move(array, n, m);
}
6.5
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void fun(int a[], int n);
cout << "输入总人数:" << endl;
int n, a[100] = { 0 };
cin >> n;
fun(a, n);
return 0;
}
void fun(int a[], int n)
{
int cnt = 0, m = n,i=0;
while (m > 1)
{
if (a[i] == 0)
cnt++;
if (cnt == 3)
{
cnt = 0;
a[i] = 1;//出去
cout << i + 1 << "号出去" << endl;
m--;
}
i = (i + 1) % n; //循环数组
}
for (int i = 0; i < n; i++)
if (a[i] == 0)
cout << "最后一个留下的人是" << i + 1 << "号" << endl;
}
6.6
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
int fun(char *);
cout << "输入一个字符串:" << endl;
char a[100];
gets_s(a);
cout << "改字符串的长度为:" << endl;
cout << fun(a) << endl;
return 0;
}
int fun(char *a)
{
char *p = a;
int len = 0;
while (*p)
{
len++;
p++;
}
return len;
}
6.7
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
char *fun(char*, char *,int);
char a[100], b[100];
cout << "输入两个字符串:" << endl;
cin >> a >> b;
int m;
cout << "输入m" << endl;
cin >> m;
cout << "复制后的字符串a:" << endl;
cout << fun(a, b, m) << endl;
return 0;
}
char *fun(char *a, char *b, int m)
{
char *p = a, *q = b;
int n = m - 1;
while (n--)
p++;
while (*q)
{
*p = *q;
p++;
q++;
}
*p = '\0';
return a;
}
6.8
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void fun(char *);
char a[100];
cout << "输入字符串a:" << endl;
gets_s(a);
fun(a);
return 0;
}
void fun(char *a)
{
char *p = a;
int digit = 0, letter = 0, space = 0, other = 0;
while (*p)
{
if (*p == ' ')
space++;
else if (*p >= 'a'&&*p <= 'z' || *p >= 'A'&&*p <= 'Z')
letter++;
else if (*p >= '0'&&*p <= '9')
digit++;
else
other++;
p++;
}
cout << "数字:" << digit << endl;
cout << "字母:" << letter << endl;
cout << "空格:" << space << endl;
cout << "其他:" << other << endl;
}
3.9
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void fun(int a[][3]);
int a[3][3];
cout << "输入3阶矩阵:" << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> a[i][j];
fun(a);
cout << "转置后的矩阵:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
cout << a[i][j] << ' ';
cout << endl;
}
return 0;
}
void fun(int a[][3])
{
int t;
for(int i=0;i<3;i++)
for (int j = 0; j < i; j++)
{
t = a[i][j];
a[i][j] = a[j][i];
a[j][i] = t;
}
}
6.11
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
bool cmp(string a, string b)
{
return a < b;
}
int main()
{
string a[5];
cout << "输入5个字符串:" << endl;
for (int i = 0; i < 5; i++)
getline(cin, a[i]);
sort(a,a+5,cmp);
cout << "输出排序后的字符串" << endl;
for (int i = 0; i < 5; i++)
cout << a[i] << endl;
return 0;
}
6.14
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void fun(int a[], int n);
int n, a[100];
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
fun(a, n);
cout << "逆序排后的数组:" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
return 0;
}
void fun(int a[], int n)
{
int t;
for (int i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - 1 - i] = t;
}
}
6.16
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
void fun(char *a);
char a[100];
cout << "输入一个字符串:" << endl;
gets_s(a);
fun(a);
return 0;
}
void fun(char *a)
{
char *p = a;
int i = 0,j=0;
int b[100] = { 0 };
bool f = 1;
int cnt = 0;
while (p[i])
{
if (p[i]>= '0'&&p[i] <= '9')
{
if (f == 0) //判断前一个数是否是非数字 如果是则b数组指向下一位置
j++;
b[j] = b[j] * 10 + (p[i]-'0');
f = 1;
}
else
{
f = 0;
}
i++;
}
int n = j;
for (int j = 0; j <=n; j++)
cout << b[j] << ' ';
cout << endl;
}
6.17
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{
int strcmp(char *p1, char *p2);
cout << "输入两个字符串:" << endl;
char a[100], b[100];
gets_s(a);
gets_s(b);
cout << "比较两个字符串:" << endl;
cout << strcmp(a, b) << endl;
return 0;
}
int strcmp(char *p1, char *p2)
{
char *a = p1, *b = p2;
while (*a&&*b&&*a == *b)
{
a++;
b++;
}
return *a - *b;
}