6-2 提取整数中能被3整除的数构成新的数后输出 (10 分)本题要求实现一个函数fun:将整型数x中每一位能被3整除的数依次取出,构成一个新数放在px所指向的变量中。高位仍在高位,低位仍在低位。例

6-2 提取整数中能被3整除的数构成新的数后输出 (10 分)

本题要求实现一个函数fun:将整型数x中每一位能被3整除的数依次取出,构成一个新数放在px所指向的变量中。高位仍在高位,低位仍在低位。例如,当x中的数为:97653140时,px中的数为:9630;如果没有满足要求的数则输出x。

函数接口定义:

 
void fun (int x, int *px);

其中 x 和 num 是用户传入的参数。函数将整数 x 中每一位能被3数依次取出,构成一个新数放在px指针所指的变量中。

裁判测试程序样例:

 
#include <stdio.h> 
void fun (int x, int *px);
int main()
{ 
     int num, t;
     scanf("%d", &num);
     fun(num, &t);
     printf("The result is: %d\n", t);
     return 0;
}

/* 请在这里填写答案 */

输入样例1:

97653140

输出样例1:

The result is: 9630

输入样例2:

7514

输出样例2:

The result is: 7514

 

void fun (int x, int *px)
{
    int r,h=x,s=0;
    while(h)
    {
        r=h%10;
        h/=10;
        if(r%3==0)
        s=s*10+r;
    }
    while(s)
    {
       r=s%10;
        s/=10;
        if(r%3==0)
        h=h*10+r;
    }
    if(h==0)
        *px=x;
    else
    *px=h;
}



#include<stdio.h>
#include<stdlib.h>
struct xinxi
{
	char num[11];
	char name[20];
	char sex[3];
	char institution[20];
	char identify;
	union 
	{
		int clas;
		char title[10];
	}shenfen;
	struct xinxi*next;
};
int main()
{
	struct xinxi*head=NULL,*p1,*p2;
	int i=0;
	while(i<5)
	{
		p1=(struct xinxi*)malloc(sizeof(struct xinxi));
		scanf("%s%s%s%s%s",p1->num,p1->name,p1->sex,p1->institution,p1->identify);
		if(p1->identify=='S')
		scanf("%d",&p1->shenfen.clas);
		else if(p1->identify=='T')
		scanf("%s",p1->shenfen.title);
		else
		break;
		if(head==NULL)
		head=p1;
		else
		p2->next=p1;
		p2=p1;
		i++;
	}
	p2->next=NULL;
	printf("now please output information:\n");
	for(i=0;i<5;i++)
	{
		printf("%s %s %s %s %s ",p1->num,p1->name,p1->sex,p1->institution,p1->identify);
			if(p1->identify=='S')
		printf("%d\n",(*p1).shenfen.clas);
	 if(p1->identify=='T')
		printf("%s\n",p1->shenfen.title);
	}
	printf("the output is over\n");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值