#1-2 OJ 26 翻转数的和

题目描述

在这里插入图片描述

输入

输入为多组测试数据,每行一组,包含两个整数A、B,两数中间以空格分隔,0<A<100 000 000,0<B<100 000 000。

输出

在这里插入图片描述

样例输入

在这里插入图片描述

样例输出

在这里插入图片描述

源代码

#include<stdio.h> 
int main()
{
	int a, b;
	int f(int a);
	while (scanf_s("%d %d", &a, &b) != EOF)
	{
		a = f(a);
		b = f(b);
		printf("%d\n", a + b);
	}
	return 0;
}
int f(int a)
{
	int t = 0;
	while (a > 0)
	{
		t = t * 10 + a % 10;
		a = a / 10;
	}
	return t;
}

关于这题

在这里插入图片描述

### SWUST OJ 翻煎饼问题的代码实现 以下是基于SWUST OJ平台上的翻煎饼问题的一种C++代码实现方式。该算法通过模拟翻转过程来计算最小翻转#### 实现思路 1. 首先读取输入据并存储在组中。 2. 使用循环逐步处理每一个未排序的最大值,将其移动到正确的位置。 3. 如果当前最大值不在目标位置,则需要执行两次翻转操作:第一次将它移到栈顶,第二次将它放到最终的目标位置。 4. 记录总的翻转,并输出结果。 下面是完整的代码实现: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; void flip(vector<int>& pancakes, int pos) { reverse(pancakes.begin(), pancakes.begin() + pos + 1); } int main() { int n; cin >> n; vector<int> pancakes(n); for (int i = 0; i < n; ++i) { cin >> pancakes[i]; } int count = 0; for (int size = n; size > 1; --size) { int maxIndex = distance(pancakes.begin(), max_element(pancakes.begin(), pancakes.begin() + size)); if (maxIndex != size - 1) { if (maxIndex != 0) { flip(pancakes, maxIndex); count++; } flip(pancakes, size - 1); count++; } } cout << count << endl; return 0; } ``` #### 关键点解析 - **flip函**用于反转指定范围内的煎饼序列[^1]。 - 主程序部分通过`for`循环逐次寻找剩余未排序中的最大值,并判断其是否已经在正确位置上[^2]。 - 若最大值不在顶部也不在其最终应处的位置,则需进行两次翻转;若仅不在最终位置但已位于顶部,则只需一次翻转即可完成调整[^3]。 以上方法能够有效解决翻煎饼问题,并满足题目对于时间复杂度的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值