数组练习题(编程题)

1.从终端(键盘)读⼊20个数据到数组中,统计其中正数的个数,并计算这些正数之和

    int sum = 0;
	int count = 0;
	int input;
	int arr[20] = {0};//初始化处理 arr	0x0000002d1b13f8c0 {-858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, ...}	int[20]
	cout << arr << endl;//输出首地址 +		arr	0x0000002d1b13f8c0 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}	int[20]
	for (int i = 0; i <= 19; i++)
	{
		cin >> arr[i];	
	}
	for (int b = 0; b < 20; b++) {
		if (arr[b] > 0) {
			count++;
			sum += arr[b];
		}
	}
	printf("正数个数为为:%d", count);
	printf("正数之和为:%d", sum);

2、 从终端(键盘)将5个整数输⼊到数组a中,然后将a逆序复制到数组b中,并输出b中各元素的值。

    int arr[5] = { 0 };
	int brr[5];
	for (int i = 0; i <= 4; i++)
	{
		cin >> arr[i];
		brr[4 - i] = arr[i];
	}
	for (int b = 0; b <= 4; b++)
	{
		cout << brr[b] << "\t";
	}

悲哀you!冒泡排序又忘了

3.随机产⽣N个两位⾃然数,降序排列后输出。(设N为20)

    const int N = 20;
	int arr[N];
	srand(time(NULL));//产生N个那就N次循环  这里是随机数种子
	for (int i = 0; i < N; i++) {
		arr[i] = rand() % 90 + 10;//第一步生成20个数据
	}
	for (int i = 0; i < N; i++) {
		cout << arr[i] << "\t";
	}
	cout << "----------------------------------------------------------------------------------" << endl;
	//第二步排序
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			if (arr[i] < arr[j]) {
				arr[i] = arr[i] ^ arr[j];
				arr[j] = arr[j] ^ arr[i];
				arr[i] = arr[i] ^ arr[j];
			}
		}
	}
	//第三步输出
	for (int i = 0; i < N; i++) {
		cout << arr[i] << "\t";
	}

4、 从键盘输⼊10个战⼠的⾝⾼,输出最⾼、最低的⾝⾼。

int hrr[10] = {0};
int x;
int y;

for (int i = 0; i<10; i++) {
	
	cin >> hrr[i];
}
for (int i = 0; i < 10; i++) {
	x = hrr[0];
	if (hrr[i] <= hrr[i + 1]) {
		x = hrr[i];//最小值
	}
	else if (hrr[i] > hrr[i + 1]) {
		y = hrr[i];//最大值
	}
}
std::cout << "最大身高是"<< x << " "<<"最小身高是" << y << endl;

5、 实现随机打乱⼤⼩为5的数组,要求所有元素不能出现在原位上。

214214

10、输⼊⼀⾏字符,统计其中有多少个字母(包括⼤写字母和⼩写字母)。

    int count = 0;
	string line;
	getline(cin,line);
	for (int i = 0; i < line.length(); i++) {
		char str = line[i];
		if ((str >= 'a' && str <= 'z') || (str >= 'A' && str <= 'Z')) count++;
	}
	cout << "总共有字母:" << count << "个" << endl;

11、输⼊⼀个字符串,判断它是否是对称串。如”abcdcba”是对称串,”123456789”不是。

string str;
getline(cin,str);
int a = 0;
int b = str.length() - 1;
while (a < b) {
	if (str[a] != str[b]) {
		return false;
		cout << "不是对称字符串" << endl;
	}
	else {
		a++;
		b--;
		cout << "是对称字符串" << endl;
	}
}

12、从键盘输⼊两个字符串,判断它们是否相同。

string str1;
string str2;
getline(cin, str1);
getline(cin, str2);
int i,j;
if (str1.length() !=  str2.length()) {
	return false;
}
else {
	for (i = 0,j = 0; i < str1.length(),j < str2.length(); i++, j++) {
		if (str1[i] != str2[j]) {
			return false;
		}
		else {
			cout << "两字符串相同" << endl;
		}
	}
}

string s1, s2;
cin >> s1 >> s2;
if (s1 == s2) {//问题:直接这么比就行了,那么我前面那种方法是不是多余了?
	cout << "Yes" << endl;
}
else {
	cout << "No" << endl;
}

for (int i = 0; i < str.length(); i++) {
	if (str[a] == str[b]) {
		a++;
		b--;
	}
	else {
		return false;
	}
}

13、从键盘输⼊两个字串,输出其中较短的那个字串,并输出它的长度。

string str1, str2;
cin >> str1 >> str2;
//法一
str1.length() > str2.length() ? cout << str2.length() << endl : cout << str1.length() << endl;
//法二
if (str1.length() < str2.length()) {
	cout << str1.length() << endl;
}
else {
	cout << str2.length() << endl;
}

14、从键盘输⼊长度不等的两个字串,将短串连接于长串之后输出。(有问题)

为啥第一个方法会报出这样的弹窗??

char str2[10];
char str1[10];
cin >> str1 >> str2;
int len = -1;
int len1 = -1;
while (str1[++len] != '\0') {};
while (str2[++len1] != '\0') {};
if (len == len1)return false;
if (len > len1) {
	for (int i = 0; (str1[i + len] = str2[i]) != '\0'; i++);
	cout << str1;
}
else {
	for (int j = 0; (str2[j + len1] = str1[j]) != '\0'; j++);
	cout << str2;//问题:为啥汇报个弹窗警告
}

string str1, str2;
cin >> str1 >> str2;
int len = -1;
string str3;
if (str1.length() == str2.length()) {
	return false;
}
else {
	if (str1.length() > str2.length()) {
		while (str1[++len] != '\0');
		cout << len;
		//for (int i = 0; (str3[i + len] = str2[i]) != '\0'; i++);用于字符数组
		cout << str1 << endl;
	}
}

15、从键盘输⼊长度不等的两个字串,将长串连接于短串之后输出。

char str2[10];
char str1[10];
cin >> str1 >> str2;
int len = -1;
int len1 = -1;
while (str1[++len] != '\0') {};
while (str2[++len1] != '\0') {};
if (len == len1)return false;
if (len > len1) {
	for (int i = 0; (str1[i + len] = str2[i]) != '\0'; i++);
	cout << str1;
}
else {
	for (int j = 0; (str2[j + len1] = str1[j]) != '\0'; j++);
	cout << str2;//问题:为啥汇报个弹窗警告
}

string str1, str2;
cin >> str1 >> str2;
int len = -1;
string str3;
if (str1.length() == str2.length()) {
	return false;
}
else {
	if (str1.length() > str2.length()) {
		while (str1[++len] != '\0');
		cout << len;
		//for (int i = 0; (str3[i + len] = str2[i]) != '\0'; i++);用于字符数组
		cout << str1 << endl;
	}
}

16、从键盘输⼊两个字串,输出其中较长的那个字串,并输出它的长度。

char str[100] = { 0 };
char str1[100] = { 0 };
cin >> str >> str1;
strcmp(str, str1) == 1 ? cout << strlen(str) : cout<< strlen(str1);

17、输⼊任意⼀⾏字符,降序排列之。

char str[100];
cin >> str;
for (int i = strlen(str) - 1; i >= 0; i--) {
	cout << str[i];
}


string str1;//char str[10] = {0};
getline(cin, str1);//cin>>str1;
for (int i = str1.length() - 1; i >= 0; i--) {
	cout << str1[i];
}

18、输⼊⼀⾏字符串,按如下规则加密:如果是英⽂字母则⼤写变⼩写、⼩写变⼤写,对⾮英⽂字符则保持不变。试写加密程序。

问题:有什么优化吗?????第一种方法

char str[10];
cin >> str;
int i = -1;
while (str[++i] != '\0') {
	if ((str[i] >= 'A' && str[i] <= 'Z')) {
		str[i] += 32;
	}
	else if(str[i] >= 'a' && str[i] <= 'z'){
		str[i] -= 32;//问题:有什么优化吗?????
	}
}
string str;
cin >> str;
for (int i = 0; i < str.length(); i++) {
	if ((str[i] >= 'A' && str[i] <= 'Z')) {
		str[i] = str[i] + 32;
	}
	else if(str[i] >= 'a' && str[i] <= 'z'){
		str[i] = str[i] - 32;
	}
}
cout << str;

19、输⼊⼀⾏⼩写英⽂字母,按如下规则加密:a→c、b→d、…、x→z、y→a、z→b,试写加密程序。

有个问题就是,他要每个单词之间有间隔,但是最后一个单词我没有在他后面加上空格

这个有点问题?????????,感觉逻辑上没有错呀!

char str[100];
cin >> str;
int count = 0;
if (str[0] == ' ') {
	count++;//只有一个单词的情况
}
for (int i = 0; i < strlen(str) ; i++) {
	if (str[i] == ' ' || str[i+1] == '\0') {
		count++;//有问题还是不会改
	}
}
cout << count << endl;
//这个方法没得毛病
string s;
getline(cin, s);
int result = 0;
if (s[0] == ' ') {
	result++;//只有一个单词的情况
}
for (int i = 0; i < s.length(); i++) {
	if (s[i] == ' ' || s[i+1] == '\0') {
		result++;//有个问题就是,他要每个单词之间有间隔,但是最后一个单词我没有在他后面加上空格键,漏了
	}
}
cout << "单词数是:" << result << endl;

20、输⼊⼀⾏英⽂,已知各单词之间⽤1个空格相隔(设第⼀个单词前没有空格),统计这⾏英⽂有多少个单词

char c;
    while (cin >> c) {
        if (c >= 'a' && c <= 'z') {
            if (c <= 'x') {
                c += 2;
            } else {
                c -= 24;
            }
        }
        cout << c;
    }


string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
	if (s[i] >= 'a' && s[i] <= 'z') {
		s[i] = 'a' + (s[i] - 'a' + 2) % 26;
	}
}
cout << s << endl;

编写程序,求⼀个整数任意次⽅的最后三位数。即求x^y值的最后三位数,要求x,y从键盘输⼊

    int x, y;
	cin >> x >> y;
	int res = 1;
	for (int i = 0; i < y; i++)
	{
		res *= x;
		res = res % 1000;
	}
	cout << fixed << setprecision(0) << x << "^" << y << "的最后三位数为:" << setw(3) << res << endl;
	cout << x << "^" << y << "的最后三位数为:" << setfill('0') << setw(3) << res << endl;

编写程序,找出1⾄99之间的全部同构数。同构数是这样⼀组数:它出现在平⽅数的右边。例如,5是25右边的数,25是625右边的数, 5和25就是同构数。

有啥优化的办法??

for (int i = 1; i <= 99; i++) {
		int sqrt = i * i;
		int qb = sqrt % 10;
		int qc = sqrt % 100;
		if (qb == i || qc == i) {
			cout << i << " ";
		}
	}
有啥优化的办法??

每个苹果0.8元,第⼀天买2个苹果,第⼆天开始买前⼀天的2倍,直⾄购买的苹果个数达到不超过100的最⼤值。编写程序求每天平均花 多少钱。

    int total_apples = 0;  // 已经购买的苹果个数
	double total_cost = 0.0;  // 已经花费的总金额
	int apples = 2;  // 第一天购买的苹果个数
	double cost = 0.8 * apples;  // (第一天的)花费
	total_apples += apples;
	total_cost += cost;
	while (apples * 2 <= 100 - total_apples) {  // 每天购买的苹果个数不能超过100个
		apples *= 2;
		cost = 0.8 * apples;
		total_apples += apples;
		total_cost += cost;
	}
	double average_cost = total_cost / total_apples;  // 每天平均花费的金额
	cout << "每天平均花费:" << average_cost << "元" << endl;

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值