?注:局部变量与全局变量的作用域与生命周期!
?
?1.求两个整数的最小公倍数:
求两个整数的最小公倍数
#include <stdio.h>
方法1:暴力求解
int main()
{
int a = 0;
int b = 0;
//输入
scanf("%d %d", &a, &b);
//计算
int m = (a > b) ? (a) : (b);
while (1)
{
if (m % a == 0 && m % b == 0)
{
break;
}
m++;
}
//输出
printf("%d\n", m);
return 0;
}
方法2
int main()
{
int a = 0;
int b = 0;
//输入
scanf("%d %d", &a, &b);
//计算
int i = 1;
while (i * a % b != 0)
{
i++;
}
//输出
printf("%d\n", i * a);
return 0;
}
?2.实现字符串的逆序输出:
例如:输入:i like beijing.? 输出:beijing.?like?i
实现字符串的逆序输出 例如:i like beijing.
#include <assert.h>
#include <string.h>
#include <stdio.h>
void reverse(char* left, char*right)
{
assert(left && right);
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
char arr[100] = { 0 };
//输入
gets(arr);
//逆置
int len = strlen(arr);
//1. 逆置每个单词
char* start = arr;
char* end = start;
while (*end != '\0')
{
while (*end != ' ' && *end != '\0')
{
end++;
}
reverse(start, end - 1);
if (*end == '\0')
start = end;
else
start = end + 1;
end = start;
}
//2. 整个字符串的逆序
reverse(arr, arr+len-1);
//输出
printf("%s\n", arr);
return 0;
}