慕课编程作业

这篇博客介绍了三个C++编程题目,包括求解水仙花数的个数、计算5个正整数的最小公倍数以及实现字符串拷贝函数。每个题目都提供了示例输入和输出,以及解题思路。

课程:程序设计基础(C&C++)

第六章 函数

声明:以下编程语言均为C++:

1

水仙花数(30分)

设有一个3位数,它的百位数、十位数、个位数的立方和正好等于这个3位数,如153=1+125+27。

编写函数,返回小于等于传入参数n且满足该条件的三位数(称为水仙花数)的个数。

(指定函数原型:int find(int n))

 返回值要求:如果传入参数n不是三位数或者在该范围内没有找到,则find返回0,

                    否则返回找到的水仙花数的个数。

 注意:不要在find函数中打印(如调用printf或puts等函数)任何数据,否则视为错误。

提交的程序需要包含需要的头文件及如下的main函数:

#include<stdio.h>

#include<stdlib.h>

int find(int n);

int main()

{

   int n,r;

  r=scanf("%d",&n);

  if(r!=1){printf("error");return -1;}

  r=find(n);

  printf("%d",r);

  return 0;

}

输入1:

  400

输出1:

  3

输入2:

  59

输出2:

 0

解:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

int find(int n) {
	int q=0, num,unit,ten,hund;
	if (n <= 100||n>=1000) {
		return 0;
	}
	for (num = 100; num <= n; num++) {
		unit = num % 10;
		ten = (num / 10) % 10;
		hund = (num / 100) % 10;
		if (num == unit * unit * unit + ten * ten * ten + hund * hund * hund)
			q++;
	}
	return q;
}

int main(){
	int n, r;
	r = scanf("%d", &n);
	if (r != 1) { printf("error"); return -1; }
	r = find(n);
	printf("%d", r);
	return 0;
}

2

最小公倍数(30分)

编写程序,从键盘输入5个正整数,然后通过多次调用LCM(LCM功能:对两个正整数求最小公倍数),求出这5个数的最小公倍数,并输出该值。

其中最小公倍数函数原型:int LCM(int x, int y) 返回值为x和y的最小公倍数

如果输入数据错误,输出"error"。

例如:

输入:2 3 6 9 10

输出:90

解:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

int LCM(int num1, int num2) {//求两个数的最小公倍数
	for (int i = 1;; i++) {
		if (i % num1 == 0 && i % num2 == 0)
			return i;
	}
}

int main() {
	int a[5];
	cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4];
	if (a[0] < 0 || a[1] < 0 || a[2] < 0 || a[3] < 0 || a[4] < 0) {
		cout << "error";
		return 0;
	}
	int temp=1;
	for (int i = 0; i <= 4; i++) {
		temp = LCM(temp, a[i]);
	}
	cout << temp;
	return 0;
}

3

字符串的拷贝(40分)

编程实现函数:void my_strcpy(char * destination,char * source);

函数功能:将source指向的字符串拷贝到destination指向的位置。

注意:使用空格字符来表示字符串的结束。例如source指向的空间,依次保存了字符'a',字符'b',字符空格' ',字符'c',则source指向的字符串为"ab c"。destionation原来存储的字符串是"xyz tdk",则拷贝后,source字符串的内容不变,destionation存储的字符串修改为“ab  tdk”。遇到异常情况,输出"error";否则不要随意输出,会视为错误.

您的main函数需要读入2个长度不超过80字节的字符串(按行依次读入source和destionation字符串),然后调用my_strcpy函数,最后用puts函数输出destionation里面存储的字符串。

例如:

输入1:

xyz abc

a kp

输出1:

xyz

输入2:

xyz abc

a kppp

输出2:

xyz pp

解:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<iostream>
using namespace std;

void my_strcpy(char* destination, char* source) {
	while ((*source) != ' ') {
		*destination = *source;
		destination++;
		source++;
	}
	*destination = *source;
	return;
}

int main() {
	char source[80], destination[80];
		cin.getline(source, 80);
		cin.getline(destination, 80);
		my_strcpy(destination, source);
		puts(destination);
	return 0;
}

如有纰漏,欢迎指正!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值