1.磨铁评级
略
2.库入车倒
#include <stdio.h>
// 第1小题
void reversePrint_13(char *str)
{
for (int i = 12; i >= 0; i--)
{
printf("%c", str[i]);
}
printf("\n");
}
int main()
{
char str[13];
for (int i = 0; i < 13; i++)
{
scanf("%c", &str[i]);
}
reversePrint_13(str);
return 0;
}
// 第2小题
void reversePrint(char *str, int length)
{
for (int i = length - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
printf("\n");
}
int main()
{
char str[1000], c;
int length = 0;
while (1)
{
c = getchar();
if (c == '\n'){
break;
}
else
{
length++;
str[length - 1] = c;
}
}
reversePrint(str, length);
return 0;
}
3.计划生育
#include <stdio.h>
// 递归
// int rabbits(int n)
// {
// if (n <= 2)
// return 1;
// else
// return rabbits(n - 1) + rabbits(n - 2);
// }
// 非递归
int rabbits(int n)
{
if (n <= 2)
return 1;
else
{
int num1 = 1, num2 = 1, index = 3, number = 2;
for (index = 3; index <= n; index++)
{
number = num1 + num2;
num1 = num2;
num2 = number;
}
return number;
}
}
int main()
{
int num = 0, n = 0;
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
scanf("%d", &n);
printf("the number in index %d is %d\n", n, rabbits(n));
}
}
4.指针乱指
#include <stdio.h>
#include <malloc.h>
// 第一小题
void swap(int *num1, int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
//第二小题
void swap_in_array(int *array, int a1, int a2)
{
int *p = array;
int temp;
temp = *(p + (a1 - 1));
*(p + (a1 - 1)) = *(p + (a2 - 1));
*(p + (a2 - 1)) = temp;
}
int main(void)
{
int num1, num2, n, a1, a2;
scanf("%d %d", &num1, &num2);
printf("a is %d and b is %d\n", num1, num2);
swap(&num1, &num2);
printf("a is %d and b is %d\n", num1, num2);
scanf("%d", &n);
int *array = (int *)malloc(n * sizeof(int));
int *p = array;
for (int i = 0; i < n; i++)
{
scanf("%d", p);
p++;
}
p = array;
for (int i = 0; i < n; i++)
{
printf("%d ", *p);
p++;
}
printf("\n");
scanf("%d %d", &a1, &a2);
swap_in_array(array, a1, a2);
p = array;
for (int i = 0; i < n; i++)
{
printf("%d ", *p);
p++;
}
printf("\n");
return 0;
}
5.户口登记
#include <stdio.h>
#include <string.h>
struct person
{
char name[100];
int age;
};
typedef struct person Person;
void change_name(Person* p, char *newName)
{
strcpy(p->name, newName);
}
int main()
{
Person cxk;
printf("pls input your age\n");
scanf("%d", &cxk.age);
printf("pls input your name\n");
scanf("%s", &cxk.name);
printf("my name is %s, age is %d\n", cxk.name, cxk.age);
printf("pls input your new name\n");
char newName[100];
scanf("%s", newName);
change_name(&cxk, newName);
// printf("my name is %s, age is %d\n", cxk.name, cxk.age);
printf("my new name is %s\n", cxk.name);
return 0;
}
6.进制转换
#include <stdio.h>
int hex(char *a)
{
for (int i = 0; i < 2; i++)
{
if ((a[i] >= 'A' && a[i] <= 'F') || (a[i] >= '0' && a[i] <= '9'))
{
if (a[i] >= 'A')
a[i] = a[i] - 'A' + 10;
else
a[i] = a[i] - '0';
}
else
{
return -1;
}
}
return a[0] * 16 + a[1];
}
int main()
{
int times, num = 0;
char a[3];
scanf("%d", ×);
for (int i = 0; i < times; i++)
{
scanf("%s", a);
num = hex(a);
if (num != -1)
{
printf("%d\n", num);
}
else
{
printf("error!\n");
}
}
return 0;
}
暂无评论
要发表评论,您必须先 登录