求最小公倍数

思路:假设两个数,5和7,那么最小至少也要7吧,所以先假定最小公倍数是两个数之间较大的,然后看7能不能同时整除5和7,不能就加1继续除
cpp
int GetLCM(int _num1, int _num2)
{
int max = _num1>_num2 ? _num1 : _num2;
while (1)
{
//证明能同时整除
if (!(max%_num1) && !(max%_num2)) return max;
else max++;
}
}
int main()
{
//输入两个正整数
int num1, num2;
scanf("%d %d", &num1, &num2);
printf("%d\n", GetLCM(num1, num2));
return 0;
}
上面的方法效率太低需要优化
思路:a*i % b 看看能不能整除,能就a*i是最小公倍数,不能就i++
cpp
int main()
{
//输入两个数
int a, b;
scanf("%d %d", &a, &b);
//判断
int i = 1;
while (a*i % b) i++;
printf("%d\n", a*i);
return 0;
}
倒置字符串

思路:1. scanf这个函数它读到空格它就不读了,所以我们要这样写scanf("%[^\n]s"),它的意思是往后读一直读到\n。
- 先把整个字符串倒置,再将每个单词倒置
cpp
void reverse(char* start, char* end)
{
char tmp;
while (start < end)
{
tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}
int main() {
char arr[100];
scanf("%[^\n]s", arr);
int len = strlen(arr);
char* start = arr;
char* end = start + (len - 1);
reverse(start, end); //倒置整个数组
start = arr;
end = arr;
while (*end++)
{
while (*end != ' ' && *end != '\0')
{
end++;
}
reverse(start, end - 1);
start = end + 1;
}
printf("%s\n", arr);
return 0;
}











解析:3和2都是整数,所以3/2等于1



