c语言程序问题

C语言 码拜 8年前 (2016-09-24) 1099次浏览
求帮助 题目如下 在
/*********Begin**********/
/**********  End  **********/
中编入适当语句完成程序功能,求帮助!
谢谢大神们~

/*--
【程序设计】
--
功能:从低位开始取出长整型变量s中偶数位上的数,依次
      构成一个新数放在t中。     
例如:当s中的数为:7654321时,t中的数为:642。
     
--*/
#include "stdio.h"
  
long fun (long s)
{
  /*********Begin**********/
  /**********  End  **********/
}  
main()     
{
  long s,m;
  void TestFunc();
  printf("\nPlease enter s:"); scanf("%ld", &s);     
  m=fun(s);
  printf("The result is: %ld\n", m);
  TestFunc();
}
解决方案

40

参考:

#include<stdio.h>
long fun (long s) 
{
	long sl=10, t;
	s /= 10; 
	t = s % 10; 
	while(s> 0)
	{ 
		s = s/100;
		t = t+s%10*sl ;
		sl = sl * 10;
	}
	return t;
}
void main()     
{
	long s,m;
	//void TestFunc();
	printf("\nPlease enter s:");
	scanf("%ld", &s);     
	m=fun(s);
	printf("The result is: %ld\n", m);
	//TestFunc();
}

5

假如单纯取偶数位的数字,也可以先转成字符串;
sprintf或snprintf, 然后按照字符串下标依次取出对应的数值;

10

long fun (long s)
{
	/*********Begin**********/
	long t = 0;
	long k = 1;
	while (s >= 10)
	{
		s /= 10;
		t += s % 10 * k;
		k *= 10;
		s /= 10;
	}
	return t;
	/**********  End  **********/
}  

5

int func(int s)
{      /*********begin************/
int tmp = 0;
int t = 0;
int count = 0;
while(s > 0)
{
tmp = s % 10;
if((tmp % 2) == 0){
if(count == 0)
t = tmp;
else{
tmp *= pow(10,count);
t += tmp;
}
count++;
}
s /=10;
}
return t;
/******************end*****************/
}

5

int func(int s)
{
	int tmp = 0;
	int t = 0;
	int count = 0;
	while(s > 0)
	{
		tmp = s % 10;
		if((tmp % 2) == 0){
			if(count == 0)
				t = tmp;
			else{
				tmp *= pow(10,count);
				t += tmp;
			}
			count++;
		}
		s /=10;
	}
	return t;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c语言程序问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)