- 编写函数:这个函数的返回值value的二进制位模式从左到右翻转后的值。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
unsigned int unsigned_int_reverse_bit(unsigned int soure) {//实参和形参和返回值都要是无符号数
int sn = 2;
unsigned int temp,result=0;
for (int i = 0; i < 32; soure /= sn, ++i) {
temp = soure % sn;//取出sn进制下的最高位
result = result * sn + temp;//创造逆序数
}
return result;
}
int main (){
unsigned int soure;
scanf("%d", &soure);
printf("%u",unsigned_int_reverse_bit(soure));
system("pause");
return 0;
}
位操作方式:
unsigned int reverse(unsigned int data)
{
int tmp1, tmp2;
int i, j;
for (i = 0, j = 31; i < j; i++, j--)
{
tmp1 = !!(data & 1 << i);
tmp2 = !!(data & 1 << j);
data &= ~(1 << i);
data &= ~(1 << j);
data |= tmp1 << j;
data |= tmp2 << i;
}
return data;
}
- 不使用(a+b)/2这种方式,求两个数的平均值。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
double average(double soure1, double soure2) {
while (soure1 != soure2) {
if (soure2){
}
if (soure1 < soure2) {
soure1+=0.5;
soure2-=0.5;
}
else if (soure1 > soure2) {
soure1-=0.5;
soure2+=0.5;
}
}
return soure1;
}
int main() {
double a, b;
scanf("%lf", &a);
scanf("%lf", &b);
printf("%lf",average(a, b));
system("pause");
return 0;
}
- 编程实现:一组数据中只有一个数字出现了一次,其他所有数字都是成对出现的,找出这个数字。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int singledog(int* a) {
int res=0;
for (int i = 0; i < 9; ++i) {
res ^= a[i];
}
return res;
}
int main (){
int a[11] = { 1,1,3,4,2,3,4,6,6,5,5 };
printf("%d\n",singledog(a));
system("pause");
return 0;
}
- 有一个字符数组的内容为:“kishere is god”, 请你将数组的内容改为"god is kishere". 不使用库函数, 只开辟有限个空间。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
char* ReverseLetter(char* source) {
unsigned int i = 0;
unsigned int len = strlen(source) - 1;
char temp;
while (i < len) {
temp = source[i];
source[i] = source[len];
source[len] = temp;
++i;
--len;
}
return source;
}
char* ReverseWord(char* source){
char* PrtOfWord;
char* PrtOfLetter=&source[0];
PrtOfWord = PrtOfLetter;
for (PrtOfLetter = &source[0]; *PrtOfLetter!= '\0';++PrtOfLetter) {
if (*PrtOfLetter==' '){
*PrtOfLetter = '\0';
ReverseLetter(PrtOfWord);
*PrtOfLetter = ' ';
PrtOfWord = PrtOfLetter+1;
}
}
ReverseLetter(PrtOfWord);
return source;
}
char* ReverseSentence(char* source) {
ReverseLetter(source);
ReverseWord(source);
return source;
}
int main (){
char str[] = "kishere is god";
printf("%s\n", ReverseSentence(str));
system("pause");
return 0;
}
这道题好难啊~~~~~~~~,不过还是做出来了,还行吧就是指针啊;字符操作啥的用C真的麻烦。