编程题-期末考试

1完备数(40分)
如果一个数正好是他的所有约数(除了它本身以外)的和,称为完备数,

如:6,它的约数有1,2,3,并且1+2+3=6.

请输出n以内的所有完备数(完备数递增输出),每个完备数1行,每行按照下例输出:

比如某完备数是6,则该行输出:6=1+2+3

如果输入错误,则输出"error"。

例如:

输入:

40

输出:

6=1+2+3空格回车

28=1+2+4+7+14空格回车

时间限制:500ms内存限制:32000kb

//完备数

#include<stdio.h>
#include<stdlib.h>
int find(int m)
{
   
    int count;
    int i, j;
    for (i = 4; i <= m; i++)
    {
   
        for (count=1,j = 2; j*j <= i; j++)
        {
   
            if (!(i%j))
                count += (j != i / j ? j + i / j : i);
        }
        if (count == i)
        {
   
            printf("%d=1", i);
            for ((count >>= 1) += 1, j = 2; j < count; j++)
            {
   
                if (!(i%j))
                    printf("+%d", j);
            }
            printf(" \n");
        }
    }
    return 0;
}
int main()
{
   
    int m,f;
    f=scanf("%d", &m);
    if (f!=1||m<0)
    printf(" ");
    else
    find(m);
    return 0;
}

2逆序memcpy(40分)
实现逆序的Memcpy方法。

void * reversememcpy ( void * destination, const void * source, int num );
从source所指的内存地址的起始位置开始拷贝num个字节,逆序保存到目标destination所指的内存地址的

起始位置中。返回destination首地址。

注意为逆序拷贝。比如source指向的位置,依次保存了abcdef,当num=3时,则逆序拷贝后destination指向的

位置应该依次保存cba.

输入:abcdef

输出:cba

提交的程序包括main函数,具体内容如下:

#include<stdio.h>

#include<string.h>

void * reversememcpy ( void * destination, const void * source, int num );

int main()

{
   

    char source[100],destionation[100];

    int n; 

    scanf("%s",source);

    scanf("%d".&n);

    destionation=reversememcpy (destionation,scource,strlen(source),n);

   printf("%s",destionation);



    return 0;

}

注意:目标地址不能为空,源和目标空间首地址差应该大于num。如果num>strlen(source),应该输出error

时间限制:500ms内存限制:32000kb

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

/*void * reversememcpy ( char * destination, const char * source, int num )
{
	char src[100],des[100];
	strcpy(src,source);
	strcpy(des,destination);
	if(num>strlen(src))
	cout<<"error";
	else
	{
		int i;
		for(i=0;i<num;i++)
		{
			des[num-i]=source[i];
		}
	}
	printf("%s",des);
};

int main()

{

    char source[100],destionation[100];

    int n; 

    scanf("%s",source);

    scanf("%d",&n);

    reversememcpy (destionation,source,n);
    return 0;
}*/
int main()
{
   
	char source[100],destination[100];
	int n;
	gets(source);
	cin>>n;
	if(n>strlen(source))
	cout<<"error";
	else
	{
   
		int i;
		for(i=0;i<n;i++)
		destination[i]=source[n-i-1];
		puts(destination);
	}
	return 0;
}

3冒泡排序(40分)
题目内容:

输入n个整数,用冒泡排序对这n个数进行递增/非递减排序,输出排序后的结果.如果输入不符要求,则输出"error"

输入格式:第一行是待排序的数据个数n,第二排是逗号分隔的n个正整数

9

9,8,7,6,5,4,3,2,1

输出格式:输出排序后的用逗号分隔的n个数据,最后1个数据后面没有逗号

1,2,3,4,5,6,7,8,9

时间限制:500ms内存限制:32000kb

/*冒泡排序*/
#include<iostream>
#define N 100 
using namespace std;
int main()
{
   
	int i,j,n,a[N],
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值