1.每瓶汽水1元,两个空瓶可以置换一瓶汽水,现在有20元,最多能喝多少瓶汽水。
-
#include <stdio.h>
-
int main()
-
{
-
int money = 0;
-
int total = 0;
-
int empty = 0;
-
int last = 0;
-
scanf("%d", &money);
-
total = money;
-
empty = money;
-
while (empty > 1)
-
{
-
total += empty / 2;
-
empty = empty / 2 + empty % 2;
-
}
-
printf("%d\n", total);
-
system("pause");
-
return 0;
-
}
2.判断一个字符串是否为另外一个字符串旋转之后的字符串
第一种:
-
#include <stdio.h>
-
#include <string.h>
-
-
int main()
-
{
-
char str1[] = "abcdef";
-
char str2[] = "abcdef";
-
int len = strlen(str1);
-
int i = 0;
-
for (i = 0; i < len; i++)
-
{
-
char tmp = str1[0];
-
int k = 0;
-
for (k = 0; k < len - 1; k++)
-
{
-
str1[k] = str1[k + 1];
-
}
-
str1[len-1] = tmp;
-
if (strcmp(str1, str2) == 0)
-
{
-
printf("是旋转后的字符串\n");
-
system("pause");
-
return 0;
-
}
-
}
-
printf("不是旋转之后的字符串\n");
-
system("pause");
-
return 0;
-
}
第二种:
-
#include <stdio.h>
-
#include <assert.h>
-
#include <string.h>
-
-
char *my_strncat(char *dest, const char *src, size_t count)
-
{
-
assert(dest);
-
assert(src);
-
char *ret = dest;
-
-
while (*dest)
-
{
-
dest++;
-
}
-
while (count--)
-
{
-
*dest++ = *src++;
-
}
-
return ret;
-
}
-
-
-
int main()
-
{
-
char str1[20] = "abcdef";
-
char str2[] = "cdef";
-
int len1 = strlen(str1);
-
int len2 = strlen(str2);
-
my_strncat(str1, str1, 6);
-
if ((len1 == len2) && (NULL != strstr(str1, str2)))
-
printf("是旋转\n");
-
else
-
printf("不是旋转\n");
-
system("pause");
-
return 0;
-
}
小知识:
-
//#include <stdio.h>
-
//#include <stdlib.h>
-
//
-
//int main()
-
//{
-
// int arr[10];
-
// printf("arr = %p\n", arr);
-
// printf("&arr = %p\n", &arr);
-
// int *p = arr;
-
// int (*q)[10] = &arr;//&arr得到数组的地址,类型是数组指针
-
// printf("p = %p\n", p);
-
// printf("q = %p\n", q);
-
// printf("p+1 = %p\n", p+1);
-
// printf("q+1 = %p\n", q+1);
-
// system("pause");
-
// return 0;
-
//}
-
-
//
-
//#include <stdio.h>
-
//int main()
-
//{
-
// int a[5] = { 1, 2, 3, 4, 5 };
-
// int *ptr = (int *)(&a + 1);
-
// printf("%d,%d", *(a + 1), *(ptr - 1));
-
// system("pause");
-
// return 0;
-
//}
-
-
//int main()
-
//{
-
// char*p = "abcdef";
-
// char arr[] = "abcdef";
-
// printf("%c\n", *(arr + 3));
-
// printf("%c\n", arr[3]);//p[3] === *(p+3)
-
// system("pause");
-
// return 0;
-
//}
-
-
//#include <stdio.h>
-
//extern int num;
-
//extern char p[];
-
//extern void fun();
-
////extern char *arr;//声明成指针
-
//int main()
-
//{
-
// printf("%s\n",(char *)(*(int*)p));
-
// fun();
-
// system("pause");
-
// return 0;
-
//}
-
-
#include <stdio.h>
-
-
char *pc[10];
-
int *pi[10];
-
int(*ppi)[10];
-
char(*ppc)[10];
-
int *(*pppi)[10];
-
char *(*pppc)[10];
-
int main()
-
{
-
ppc = &pc;
-
printf("%d\n", sizeof(pc));//40
-
printf("%d\n", sizeof(pi));//40
-
printf("%d\n", sizeof(ppi));//4
-
printf("%d\n", sizeof(ppc));//4
-
printf("%d\n", sizeof(pppi));//4
-
printf("%d\n", sizeof(pppc));//4
-
printf("%d\n", sizeof(*pc));//4
-
printf("%d\n", sizeof(*pi));//4
-
printf("%d\n", sizeof(*ppi));//40
-
printf("%d\n", sizeof(*pppi));//40
-
printf("%d\n", sizeof(*pppc));//40
-
system("pause");
-
return 0;
-
}
阅读(1211) | 评论(0) | 转发(0) |