标签:
1:
甲乙两队进行比赛,甲队有a、b、c三人,乙队有x、y、 z三人,有人想知道比赛对手,a说不和x比赛,c说不和x,z比赛,编程找三对赛手名单。
1 #include <stdio.h> 2 3 int main() 4 { 5 char x = ‘a‘,y,z; 6 for(x = ‘a‘; (x<=‘c‘);x++) 7 { 8 for(y = ‘a‘;y<=‘c‘;y++) 9 { 10 for(z = ‘a‘; z <= ‘c‘; z++) 11 { 12 if( x!=y && x!=z && y!=z && x!=‘a‘ && x!=‘c‘ && z!= ‘c‘ ) 13 { 14 printf("x -> %c\n",x); 15 printf("y -> %c\n",y); 16 printf("z -> %c\n",z); 17 } 18 19 } 20 } 21 } 22 return 0; 23 }
2.编写一个函数,对一个无符号短整型数,取它的偶数位(即从左边起第2、4、6… 位)与奇数位(即从左边起第1 、3、5…)分别组成新的无符短整数并通过形参传回调用参数。
原型:
void Split(unsigned short a,unsigned short * pOdd,unsigned short * pEven);
其中pOdd代表奇数位,pEven代表偶数位。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 6 /*巧用atoi()和itoa()函数*/ 7 void Split(unsigned short a, unsigned short *pOdd, unsigned short *pEven) 8 { 9 char s[20]; 10 char odd[20]; 11 char even[20]; 12 itoa(a,s,10); 13 int i; 14 //strrev(s); 15 for(i=0;i<strlen(s);i++) 16 { 17 if(i%2 == 0)/*偶数位*/ 18 { 19 even[i/2] = s[i] ; 20 even[i/2+1] = ‘\0‘; 21 } 22 else 23 { 24 odd[i/2] = s[i]; 25 odd[i/2+1]=‘\0‘; 26 } 27 } 28 //unsigned short sodd,seven; 29 *pOdd = atoi(odd); 30 *pEven = atoi(even); 31 } 32 33 int main() 34 { 35 unsigned short a = 12345; 36 unsigned short *pOdd = (unsigned short *)malloc(sizeof(unsigned short)); 37 unsigned short *pEven = (unsigned short*)malloc(sizeof(unsigned short)); 38 Split(a,pOdd,pEven); 39 printf("%u\t%u\n",*pOdd,*pEven); 40 return 0; 41 }
标签:
原文地址:http://www.cnblogs.com/yongjiuzhizhen/p/4308496.html