第五周实验(HZAU)

1.任意输入2-4位的正整数,判断是否为一个回文数,若是输出“yes",否则输出“no”

>>1221

>>yes

//#include<iostream>
//using namespace std;
请任意输入一个2到4位的正整数,判断它是否为一个回文数,若是输出"yes",否则输出"no"
//int main()
//{
//	int i, j, input;
//	int arr[4] = { 0 };
//	cin >> input;
//	for (i = 0; input > 0; i++)
//	{
//		arr[i] = input % 10;
//		input /= 10;
//	}
//	for (j = 0; j < i / 2; j++)
//	{
//		if (arr[j] != arr[i - 1 - j])
//		{
//			break;
//		}
//	}
//	if (i / 2 == j)
//	{
//		cout << "yes";
//	}
//	else
//	{
//		cout << "no";
//	}
//	return 0;
//}

2.从键盘上输入一个三位数,判断是否为水仙花数

>>153

>>yes

#include<iostream>
//#include<cmath>
using namespace std;
int is_lili_number(int input)
{
	int i = 0;
	while (input)
	{
		input /= 10;
		i++;
	}
	return i;
}
int main()
{
	int input = 0;
	//求出水仙花数的位数
	cin >> input;
	int ret = is_lili_number(input);
	//cout << ret;
	//判断该数是不是水仙花数
	int j = 0;
	int sum = 0;
	int c = input;
	for (j = 0; j < ret; j++)
	{
		sum +=(int) pow(input % 10, ret);
		input /= 10;
	}
	if (c == sum)
	{
		cout << "yes";
	}
	else
	{
		cout << "no";
	}
	return 0;
}

3.将程序补充完整,计算并输出一个三正整数的个位、十位、百位数字之和

>>123

>>sum is :6

#include<iostream>
using namespace std;
int main()
{
	int x, gw, sw, bw;
    cin >> x;
	gw = x % 10;
	bw = x / 100;
	sw = (x / 10) % 10;
	cout << "sum is:" << gw + bw + sw;
	return 0;
}

4.将程序补充完整,设定银行定期存款的年利率是3%,并已知存款本金任意,计算n年后本利之和

>>100 3

>>109.3

#include<iostream>
#include<iomanip>
//1.2 将程序补充完整,设银行定期存款的年利率是3 % ,并已知存款期为n年,存款本金任意,计算n年后本利之和。
//(1)数学函数头文件
using namespace std;
int main()
{
    double cash, total=0;
    int n,i;
    cin >> cash >> n;
    //计算本利之和(2)
    for (i = 0; i < n; i++)
    {
        cash= cash + cash * 0.03;
    }
    total = cash;
    cout << fixed << setprecision(1) << total;
    return 0;
}

5.将程序补充完整,实现两个变量的值交换

>>1 2

>>2 1

#include<iostream>
using namespace std;
int main()
{
    int a, b, t;
    cin >> a >> b;
    //cout << "a=" << a << ",b=" << b << endl;
    //交换a、b的值
    t = a;
    a = b;
    b = t;
    cout << "a=" << a << ",b=" << b << endl;
    return 0;
}

6.编程求三个数中的最大者

>>2 1 3

>>3

#include<iostream> 
using namespace std;
//编程求三个数中的最大者
int main()
{
    float x, y, z,max;
    //cout<<"输入三个数:"<<endl; 
    cin >> x >> y >> z;
    if (x > y) 
        max = x;
    else
        max = y;
    if (max<z)
        max = z;
    //cout<<"最大数是:"; 
    cout << max << endl;
    return 0;
}

7.输入四个整形数据,请按照从小到大的顺序将其输出

>>5 4 2 6

>>2 4 5 6

#include<stdio.h>
int main()
{
	//输入四个整型
	int arr[4] = { 0 };
	int i = 0;
	for (i = 0; i < 4; i++)
	{
		scanf("%d", &arr[i]);
	}
	for (i = 0; i < 3; i++)
	{
		int j = 0;
		for (j = 0; j < 3 - i;j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
	}
	for (i = 0; i < 4; i++)
		printf("%d ", arr[i]);
	return 0;
}

8.三角形的面积:给出任意三角形的三条边的值a,b,c,要求该三角形的面积area。

>>3 4 5

>>6.000

#include<stdio.h>
#include<math.h>
//给出任意三角形的三条边的值a、b、c,要求该三角形的面积area。
//三角形的面积可以根据下面的海伦公式求解: 
//area = sqrt(x * (x - a) * (x - b) * (x - c)),其中x是三边和的一半,即(a + b + c) / 2。
//sqrt函数包含在cmath的头文件中,它的功能是求平方根。
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d %d %d",&a, &b, &c);
	double sum = (a + b + c) / 2.0;
	double area = 0;
	if (a + b > c && b + c > a && c + a > b)
	{
		area = sqrt(sum * (sum - a) * (sum - b) * (sum - c));
		printf("%.3lf", area);
	}
	else
	{
		printf("ERROR");
	}
	return 0;
}

9.数的分解:给出一个不多于5位的正整数,要求:分别打印出每一个数字,并求出它是几位数

>>6408

>>6 4 0 8

    4

#include<stdio.h>
//给一个不多于5位的正整数,要求:分别打印出每一位数字,并求出它是几位数。
int find_num(int input)
{
	int i = 0;
	while (input)
	{
		input = input /= 10;
		i++;
	}
	return i;
}
void print(int input)
{
	if (input)
	{
		print(input / 10);
		printf("%d ", input % 10);
	}
}
int main()
{
	int input = 0;
	scanf("%d", &input);
	//求出位数
	int ret = find_num(input);
	
	//分别打印这个数
	print(input);
	printf("\n%d", ret);
	return 0;
}

10.成绩等级(switch语句)给出一个百分制的成绩,要求输出成绩等级ABCDEF。

>>88.5

>>B

#include<stdio.h>
//给出一个百分制的成绩,要求输出成绩等级ABCDE。
//90分及以上为A,80~89分为B,70~79分为C,60~69分为D,60分以下为E。
int main()
{
	double score = 0;
	scanf("%lf", &score);
	int i = (int)score / 10;
	switch (i)
	{
	case 9:
		printf("A");
		break;
	case 8:
		printf("B");
		break;
	case 7:
		printf("C");
		break;
	case 6:
		printf("D");
		break;
	default:
		printf("E");
		break;
	}

	return 0;
}

11.字符判断(switch语句)从键盘任意输入一个字符,编程判断该字符的种类

>>A

>>Uppercase Letter

#include<stdio.h>
//给出一个百分制的成绩,要求输出成绩等级ABCDE。
//90分及以上为A,80~89分为B,70~79分为C,60~69分为D,60分以下为E。
int main()
{
	double score = 0;
	scanf("%lf", &score);
	int i = (int)score / 10;
	switch (i)
	{
	case 9:
		printf("A");
		break;
	case 8:
		printf("B");
		break;
	case 7:
		printf("C");
		break;
	case 6:
		printf("D");
		break;
	default:
		printf("E");
		break;
	}

	return 0;
}

12.满足条件的数(for循环-程序填空)找出1000-2000之间所有整数中满足如下条件的数:该数的高端两位数字的和与低端两位数字的和值相等,如1001,1120。程序能输出所有满足条件的数以及他们的总个数。

//找出1000-2000之间所有整数中满足如下条件的数:该数的高端两位数字的和与低端两位数字的和值相等,
//如1001, 1120。程序能输出所有满足条件的数以及它们的总个数。
#include<iostream>
using namespace std;
int main()
{
    int n, a1, a2, b1, b2, m = 0;
    //cout<<"1000-2000之间满足条件的数有:"<<endl;
    for (n = 1000; n<=2000; n++)
    {
        a1 = n / 1000;
        a2 = (n - a1 * 1000) / 100;
        b1 = n % 100 / 10;
        b2 = n%10;
        if (a1 + a2 == b1 + b2)
        {
            m++;
            cout << n << " ";  //注意此处改为了空格
        }
    }
    cout << endl;
    //cout<<endl<<"一共有"<<m<<"个"<<endl;
    cout << m << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值