Luogu P1010 幂次方

【橙题不会做系列】QAQ

是李老师上课的题目……

这题最开始想法是打表做。事实证明这样做也可以(

老师用的是位运算……

这种一步步分解也能想到用递归qwq

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
using namespace std;
void qwq(const int n)
{
	stack<int> pwp;
	int a,m=n,s=0;
	while (m)
	{
		a=n&(1<<s);
		m>>=1;
		if (a)
		{
			pwp.push(s);
		}
		s++;
	}
	if (pwp.top()==0)
	{
		printf("2(0)");
	}
	else if (pwp.top()==1)
	{
		printf("2");
	}
	else if (pwp.top()==2)
	{
		printf("2(2)");
	}
	else
	{
		printf("2(");
		qwq(pwp.top());
		printf(")");
	}
	pwp.pop();
	while (!pwp.empty())
	{
		printf("+");
		if (pwp.top()==0)
		{
			printf("2(0)");
			pwp.pop();
			continue;
		}
		if(pwp.top()==1)
		{
			printf("2");
			pwp.pop();
			continue;
		}
		if (pwp.top()==2)
		{
			printf("2(2)");
			pwp.pop();
			continue;
		}
		printf("2(");
		qwq(pwp.top());
		printf(")");
		pwp.pop();
	}
}
void start()
{
	int n;
	scanf("%d",&n);
	qwq(n);
}
int main()
{
	start();
	return 0;
}
原文地址:https://www.cnblogs.com/Kan-kiz/p/10623066.html