1.strlen实现
#include <stdio.h>
int MyStrlen(char* p) {
int count = 0;
for (int i = 0; *p != '\0'; i++,p++) {
count++;
}
return count;
}
int main()
{
char p[] = "asdfghjkl";
printf("%d", MyStrlen(p));
return 0;
}
2.strcpy实现
#include <stdio.h>
char* MyStrcpy(char* p1, char* p2) {
char* arr = p1;
for (int i = 0; *p2 != '\0'; i++) {
*p1 = *p2;
p1++;
p2++;
}
return arr;
}
int main()
{
char p1[] = "000000000";
char p2[] = "asdfghjkl";
printf("%s", MyStrcpy(p1, p2));
return 0;
}
3.输入一个整数数组,实现一个函数,来调整该数组中数字的顺序使得数组中所有的奇数位于数组的前半部分,所有偶数位于数组的后半部分。
#include <stdio.h>
void MyArr(int* arr,int n) {
int left = 0;
int right = n - 1;
int tmp = 0;
while (left < right) {
if (arr[left] % 2 != 0) {
left++;
}
if (arr[right] % 2 == 0) {
right--;
}
if (left < right) {
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
}
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int n = sizeof(arr) / sizeof(arr[0]);
MyArr(arr,n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
4.打印菱形
#include<stdio.h>
int main() {
int n = 0;
scanf_s("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
printf(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n - i; j++) {
printf(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
}
5.喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水
#include<stdio.h>
int drink(int m) {
int price = 1;
int count = m / 2;
int bottle = m / 2;
while (bottle > 2) {
count += bottle / 2;
bottle = bottle / 2 + bottle % 2;
}
return count;
}
int main() {
int money = 0;
scanf_s("%d",&money);
printf("%d", drink(money));
return 0;
}
6.求出0~100000之间的所有“水仙花数”并输出。“水仙花数”是指一个n位数,其各位数字的n次方之和确好等于该数本身,如:153=1^3+5^3+3^3,则153是一个“水仙花数”。
#include<stdio.h>
#include<math.h>
int num(int i) {
int count = 0;
while (i) {
i = i / 10;
count++;
}
return count;
}
int main() {
int count = 0;
for (int i = 0; i < 1000000; i++) {
int count = num(i);
int sum = 0;
int temp = i;
while (temp) {
sum += pow(temp % 10, count);
temp /= 10;
}
if (sum == i) {
printf("%d\n", i);
}
}
}
|