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;
}