C++小程序

题目1:水仙花数
输出所有的“水仙花数”,什么是水仙花数呢?所谓的水仙花数就是指一个3位数,其各个位数字的立方和等于该数本身。立方大家都知道吧,比如2的立方就是 2x2x2=8。例如:153就是一个水仙花数,因为:
1的立方=1x1x1=1
5的立方=5x5x5=125
3的立方=3x3x3=27

#include <iostream>
using namespace std;
int main()
{
    int a;
	for (a = 100; a <= 999; a++)
	{
		int k,m,n;
		k = a % 10; //k为个位数字       589
		m = a/100;  //m为百位数字
		n = a % 100 / 10;   //n为十位数字
		k = k*k*k;
		m = m*m*m;
		n = n*n*n;
		if (k + m + n == a)
			cout << a << endl;
	return 0;
}

题目2:打印星号图案

#include <iostream>
using namespace std;
int main()
{
	int i, j;
	for (i = 1; i < 20; i += 2)
	{
		for (j = 1; j <= i; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
	for (i = 21; i >= 1; i -= 2)
	{
		for (j = 1; j <= i; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
	return 0;
}

题目3:输出此规律的分数序列:
2/1,3/2,5/3,8/5,13/8,21/13,…

#include <iostream>
using namespace std;
int main()
{
	int a, b, m;
	a = 2;
	b = 1;
	for (;a < 200; )
	{
		cout << a << "/" << b << endl;
		m = a;
		a = a + b;
		b = m;
	}
	return 0;
}

题目4:按从小到大的顺序输出数组中各数: 5, 8, 9, 0, 2, 1, 4, 7, 6, 3

#include <iostream>
using namespace std;
int main()
{
	int a[10] = { 5, 8, 9, 0, 2, 1, 4, 7, 6, 3 };
	int i, j, k;
	for (i = 0; i < 10;++i)
	{
		for (j = i + 1; j < 10; j++)
		{
			if (a[i] > a[j])
			{
				k = a[j];
				a[j] = a[i];
				a[i] = k;
			}
		}
	}
	for (i = 0; i < 10;++i)
	{
		cout << a[i] << "  ";
	}
	cout <<endl;
	cout << "hello world" << endl;
	system("pause");
	return 0;
}

题目5:将一i西安字符串中的字母i替换为@,输出替换后的字符串和替换个数。

#include <iostream>
using namespace std;
int main()
{
	char szbuf[100] = "hello, friends, my name is cctry.com. what is your name ?";
	int i;
	int k = 0;
	for (i = 0; i <= 100; ++i)
	{
		if (szbuf[i]=='i')
		{
			szbuf[i] = '@';
			++k;
		}
	}
	for (i = 0; i <= 100; ++i)
	{
		cout << szbuf[i];
	}
	cout << endl;
	cout << k << endl;
	system("pause");
	return 0;
}

题目6:定义一个学生类型的结构体,包含学生的:姓名、学号、分数。用该结构体定义大小为5的结构体变量数组。手动输入给数组成员赋值,然后输出5个学生的信息,求出5个学生的分数的平均值并输出。

#include <iostream>
#include <string>
using namespace std;
struct Student
{
	string name;
	int num;
	int score;
};

int main()
{
	int i;
	float ave=0;
	Student stu[5];
	for (i = 0; i < 5; ++i)
	{
		cin >> stu[i].name;
		cin >> stu[i].num;
		cin >> stu[i].score;
	}
	Student* ave_score;
	for (i = 0; i < 5;++i)
	{
		ave_score = &stu[i];
		ave = ave + ave_score->score;
	}
	ave = ave / 5;
	cout << endl;
	for (i = 0; i < 5;++i)
	{
		cout << stu[i].name << " " << stu[i].num << " " << stu[i].score << endl;
	}
	cout << endl;
	cout << "5个学生的平均分为"<<ave << endl;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值