#include#includedouble mul(int x, int y)
{if (y == 0)
{return 1;
}
else if(y>0)
{return x * pow(x, y - 1);
}
else
{return 1.0 / pow(x, -y);
}
}
int main()
{int n = 0;
int k = 0;
scanf("%d %d", &n, &k);
double ret = mul(n, k);
printf("%.2lf", ret);
return 0;
}
二、輸入一個(gè)非負(fù)整數(shù),返回組成它的數(shù)字之和#includeint DigitSum(int x)
{if (x >9)
return DigitSum(x / 10) + x % 10;
else
return x;
}
int main()
{int num = 0;
scanf("%d", &num);
int ret = DigitSum(num);
printf("%d", ret);
return 0;
}
三、交換完打印
1. 迭代+temp法+數(shù)組#include#includevoid string(char* ch,int sz)
{int left = 0;
int right = sz - 1;
char temp = 0;
while (left<= right)
{temp = ch[left];
ch[left] = ch[right];
ch[right] = temp;
left++;
right--;
}
printf("%s", ch);
}
int main()
{char ch[] = "abcdef";
int sz = strlen(ch);
string(ch,sz); //返回值該用什么接收----不需要接收,地址直接是作用在上面的
return 0;
}
2.用指針#include#includevoid swap(char* ch,int x)
{int left = 0;
int right = x - 1;
while (left<= right)
{char temp = 0;
temp = *(ch + left);
*(ch+left) = *(ch+right);
*(ch + right) = temp;
left++;
right--;
}
}
int main()
{char ch[] = "abcdef";
int sz = strlen(ch);
swap(ch,sz);
printf("%s", ch);
return 0;
}
2.遞歸#include#includevoid swap(char ch[], int x)
{char temp = 0;
temp = *ch;
*ch = *(ch + x - 1);
*(ch + x - 1) = '\0';
if (strlen(ch + 1) >= 2)
{swap(ch + 1,strlen(ch+1));
}
*(ch + x - 1) = temp;
}
int main()
{char ch[] = "abcdef";
int sz = strlen(ch);
swap(ch,sz);
printf("%s", ch);
return 0;
}
四、求二進(jìn)制中一的個(gè)數(shù)
4.1 循環(huán)按位比較法#includeint count_one(int input)
{int i = 0;
int count = 0;
for (i = 0; i< 32; i++)
{if (((input >>i) & 1) == 1)
{ count++;
}
}
return count;
}
int main()
{int input = 0;
scanf("%d", &input);
int ret = count_one(input);
printf("%d", ret);
return 0;
}
注意:
4.2 余2除2法int count_one(unsigned int input)
{int count = 0;
while (input)
{if (input % 2 == 1)
{ count++;
}
input /= 2;
}
return count;
}
int main()
{int input = 0;
scanf("%d", &input);
int ret = count_one(input);
printf("%d", ret);
return 0;
}
4.3 自己退格int count_one(int input)
{int count = 0;
while (input)
{input = input & (input - 1);
count++;
}
return count;
}
int main()
{int input = 0;
scanf("%d", &input);
int ret = count_one(input);
printf("%d", ret);
return 0;
}
五、獲取并分別打印一個(gè)整數(shù)二進(jìn)制序列中所有的偶數(shù)位和奇數(shù)位void Print(int n)
{int i = 0;
printf("奇數(shù)位: ");
for (i = 30; i >=0; i -= 2)
{printf("%d ", (n >>i) & 1);
}
printf("\n");
printf("偶數(shù)位: ");
for (i = 31; i >= 1; i -= 2)
{printf("%d ", (n >>i) & 1);
}
printf("\n");
}
int main()
{int n = 0;
scanf("%d", &n);
Print(n);//打印n的2進(jìn)制中的所有奇數(shù)位和所有的偶數(shù)位
return 0;
}
六、求兩個(gè)整數(shù)m和n的二進(jìn)制表達(dá)中,有多少個(gè)位(bit)不同
6.1 循環(huán)比較#includeint is_different(int m,int n)
{int count = 0;
int i = 0;
for (i = 0; i< 32; i++)
{if (((m >>i) & 1) != ((n >>i) & 1))
{ count++;
}
}
return count;
}
int main()
{int m = 0;
int n = 0;
scanf("%d %d", &m,&n);
int ret = is_different(m,n);
printf("%d", ret);
return 0;
}
6.2 尋找中間媒介#includeint is_different(int m, int n)
{int tmp = m ^ n;
int count = 0;
int i = 0;
for (i = 0; i< 32; i++)
{if (((tmp >>i) & 1) == 1)
{ count++;
}
}
return count;
}
int main()
{int m = 0;
int n = 0;
scanf("%d %d", &m,&n);
int ret = is_different(m,n);
printf("%d", ret);
return 0;
}
6.3 自己退格#includeint is_different(int m, int n)
{int count = 0;
int tmp = m ^ n;
while (tmp)
{tmp = tmp & (tmp - 1);
count++;
}
return count;
}
int main()
{int m = 0;
int n = 0;
scanf("%d %d", &m,&n);
int ret = is_different(m,n);
printf("%d", ret);
return 0;
}
七、獲得月份天數(shù)
7.1 數(shù)組int get_days_of_month(int y, int m)
{int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };
// 1 2 3 4 5 6 7 8 9 10 11 12
int day = days[m];
if ((m == 2) && (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)))
{day++;
}
return day;
}
int main() {int y = 0;
int m = 0;
while (scanf("%d %d", &y, &m) == 2)
{int ret = get_days_of_month(y, m);
printf("%d\n", ret);
}
return 0;
}
7.2 switch語句int get_days_of_month(int y, int m)
{int day = 0;
switch (m)
{case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day = 31;
break;
case 4: case 6: case 9: case 11:
day = 30;
break;
case 2:
{day = 28;
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
day++;
}
break;
}
return day;
}
八、進(jìn)制轉(zhuǎn)換
8.1 遞歸#includevoid is_six_count(int input)
{if(input>5)
{ is_six_count(input/6);
}
printf("%d",input%6);
}
int main()
{int input = 0;
scanf("%d",&input);
is_six_count(input);
return 0;
}
8.2 數(shù)組實(shí)現(xiàn)int main()
{int arr[20] = {0};
int n = 0;
scanf("%d", &n);
int i = 0;
while(n)
{arr[i++] = n%6;
n/=6;
}
for(--i; i>=0; i--)
{printf("%d", arr[i]);
}
return 0;
}
九、刪除指定字符#includevoid is_delect(int* arr, int delect, int sz)
{int i = 0;
int j = 0;
for (i = 0; i< sz; i++)
{if (arr[i] == delect)
{arr[j++] = arr[i];
}
}
for (i = 0; i< j; i++)
{printf("%d", arr[i]);
}
}
int main()
{int num = 0;
scanf("%d", &num);
int i = 0;
int arr[50] = 0;
for (i = 0; i< num; i++)
{scanf("%d", &arr[i]);
}
int delect = 0;
scanf("%d", &delect);
is_delect(arr, delect, num);
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧