c语言中有没有能把一串数字中的奇数位和偶数位的数字提出来的代码?

如题所述

#include<stdio.h>
int main()
{int x,n,i,j,a[15]={0};
 scanf("%d",&x);
 for(n=0;x;x/=10)a[n++]=x%10;
 printf("奇数位的数字:");
 for(i=n-1;i>-1;i-=2)
   printf("%d ",a[i]);
 printf("\n偶数位的数字:");
 for(i=n-2;i>-1;i-=2)
   printf("%d ",a[i]);
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-10-24
#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
char buf[128]={0};
char buff = 0;
char *p = NULL;
printf("Please enter a string of Numbers and end with carriage return.\n");
scanf("%s",buf);
getchar();
while(1)
{
printf("Please enter 0 to output the even,enter 1 to output the odd,\n");
printf("enter 2 to continue,enter other to quit.\n");
scanf("%c",&buff);
getchar();
if (buff == '0')
{
p = sizeof(buf)%2 + buf + 1;
while(*p)
{
printf("%c\t",*p);
p += 2;
}
printf("\n");
}
else if (buff == '1')
{
p = sizeof(buf)%2 + buf;
while(*p)
{
printf("%c\t",*p);
p += 2;
}
printf("\n");
}
else if (buff == '2')
{
memset(buf,0,sizeof(buf));
printf("Please enter a string of Numbers and end with carriage return.\n");
scanf("%s",buf);
getchar();
}
else
{
break;
}

}

return 0;
}

//大概就是这样吧,如果要在内部传产,封装的函数的话,给你个思路,对其求10的余数。告辞

#include <stdio.h>
#include <string.h>

int get_event(int num,int choice);

int main(int argc, char const *argv[])
{
int a = 1234;
int b = 12345;
printf("%d\n",get_event(a,0));
printf("%d\n",get_event(a,1));
printf("%d\n",get_event(b,0));
printf("%d\n",get_event(b,1));

return 0;
}

int get_event(int num,int choice)
{
int ret = 0;
int i = 1;
if (choice == 0)
{
num = num / 10;
while(num)
{
ret += (num % 10) * i;
num = num / 100;
i *= 10;
}
}
else if (choice == 1)
{
while(num)
{
ret += (num % 10) * i;
num = num / 100;
i *= 10;
}
}
else
{
ret = -1;
}
return ret;
}

//上班无聊,脑子不好使了,告辞

第2个回答  2018-10-24
#include<stdio.h>

int a[1001];

int main(){
    int i=1,j;
    long long int n;
    while(scanf("%ld",&n)!=EOF){
        while(n){
           a[i++]=n%10;
           n/=10;
        }
        printf("偶数位为:");
        for(j=1;j<i;j++){
            if(j%2==0) printf("%d ",a[j]);
        }
        printf("\n奇数位为:");
        for(j=1;j<i;j++){
            if(j%2==1) printf("%d ",a[j]);
        }
    }
}

第3个回答  2018-10-24
一、数学定义:
在数学中,定义凡是可以被2整除的,均为偶数。 反之则为奇数。
二、算法分析:
根据数学定义,以及一些C语言的知识,可以得到很多种判断一个整数的奇偶性的方法,举例如下:
1、 最常用最直观的方法。
对2取余,如果为0,表示整除,即为偶数。否则为奇数。
即 n%2==0 则为偶数。 n%2==1 则为奇数。
由于C语言规定逻辑运算中0为假,1为真,于是判断奇偶可以简化为
if(n%2) printf("n为奇数\n");
else printf("n为偶数\n");
2、效率最高的方法。

计算机中都是二进制存储,这样判断奇偶其实判断二进制的最后一位即可。所以可以用更高效的位操作来判断:
if(n&1) printf("n为奇数\n");
else printf("n为偶数\n");
3、其它方法:

根据各种数学推论,以及C语言操作,还有很多其它方法,不过都不常用,仅做了解,举几个例子如下:
n/2*2==n //利用整数除法取整的规则,如果相等,则为偶数。((n>>1)<<1)==n //右移后再左移,如果与原值相等,则为偶数。(n+1)/2 == n/2 //同样利用整数除法取整,相等则为偶数。
类似的还可以写出很多,不过意义并不大,因为不常用,且不高效,实际使用只会增加运行开销和维护难度。
第4个回答  2018-10-23
你说的是多个数的奇数位和偶数位?还是一个数的?请补充。追答

比如取奇数位,是12345中取5、3、1?还是123、1234、547这三个数中取3、1、4、2、7、5。

追问

是一串数字中的第一三五。。。。。二四六。。。。位

是一串数字中的第一三五。。。。。二四六。。。。位

本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网