蓝桥杯.颠倒的价牌(暴力枚举)

Question:

Result:9088

Solve:

将所有能翻转的、且是翻转前后差值范围在题目要求内的数字存起来,然后去一一比对

Code:

#include<iostream>
using namespace std;
int cntu = 0,cntd = 0;  //记录符合条件的价牌个数
struct Node{
	int init; //价牌原来的数字
	int dist; //颠倒前后的差价
}up[7000],down[7000];
int main(void)
{
	//1 2 5 6 8 9 0
	//1 5 2 9 8 6 0
  //遍历四位数寻找可颠倒价牌
	for(int i = 1000; i <= 9999; i++){
		int temp = i,ans = 0;
		if(temp%10==0 || temp%10==3 || temp%10==4 || temp%10==7) continue;
		//这里用了逆序取数,因为我感觉用字符串也简单不到哪去
		while(temp){
			if(temp%10 == 1){ ans*=10; ans+=1; }
			else if(temp%10 == 2){ ans*=10; ans+=5; }
			else if(temp%10 == 5){ ans*=10; ans+=2; }
			else if(temp%10 == 6){ ans*=10; ans+=9; }
			else if(temp%10 == 8){ ans*=10; ans+=8; }
			else if(temp%10 == 9){ ans*=10; ans+=6; }
			else if(temp%10 == 0){ ans*=10; }
			//不可颠倒,标记变为0
      else{ ans = 0; break; }
			temp /= 10;
		}
 
		if(ans){
			if(ans - i > 800 && ans - i <900){
				up[++cntu].init = i;
				up[cntu].dist = ans - i;
			}	
			else if(i - ans > 200 && i - ans < 300){
				down[++cntd].init = i;
				down[cntd].dist = i - ans;
			}
		}
		else continue;
	}
 
  //双重循环寻找558
	for(int i = 1; i <= cntu; i++)
	for(int j = 1; j <= cntd; j++)
		if(up[i].dist - down[j].dist == 558){
			cout <<down[j].init;
			return 0;
		}	
}

声明:图片均来源于蓝桥杯官网,以个人刷题整理为目的,如若侵权,请联系删除~

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UmVfX1BvaW50

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值