zoj 3327 Friend Number 贪心

Friend Number



Time Limit: 1 Second      Memory Limit: 32768 KB



Given a positive integer x, let P(x) denotes the product of all x‘s digits. Two integers x and y are friend numbers if P(x) = P(y). Here comes the problem: Given a positive integer x, of
course it has a lot of friend numbers, find the smallest one which is greater than x.

Input

There are multiple test cases. The first line of input is an integer T (0 < T < 230) indicating the number of test cases. Then T test cases follow. Each case is an integer x (0 < x <= 101000). You may
assume that x has no leading zero.

Output

For each test case, output the result integer in a single line. You should not output a number with leading zero.

Sample Input

3
12
19
222

Sample Output

21
33
241

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3327

题意:找出比n大的 最小的数,要求每个位上的数字 乘积 与n每个位上的数字 乘积要相等。

参考了别人的题解

做法:如果有0 ,特殊处理。分为末尾只有一个0,不是这种情况。

1、如果只有个位一个0,就从十位上往高位找,找不是9的,找到加1,结束。找的过程遇到9,都改为0。如果都是9,就在数字最左边加一个1。

2、如果有0,且不是上诉情况,那就直接从个位往高位走,遇到9,变0,遇到非9 ,加1,结束。

3、如果没有0的话,把从个位开始,素数分解,并记录所有的素数。分解完一位,就判断下分解出来的素数能不能组成比 这一位大的 个位数,能的话取最小的这个数,设这个位为id。如123  ,个位分解完,得素数3,十位分解完得素数2,这个时候可以用分解的素数有2,3,2*3=6,要比2大且最小,所以选3。 所以十位改为3,素数3的个数减1。 接下来从个位,不停地放 已经得到的素数能组成最大的数,直到id的后一位为止。

如果一直没找到id位,就直接在原来的数前面加个‘1’ 输出即可。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char str[1010];

int ji[10];
int prim[4]={2,3,5,7};
int ling(char str[])
{
	int flag=0;
	for(int i=0;str[i];i++)
	{
		if(str[i]=='0')
			flag++;
	}
	return flag;
}
void add(char ch)
{
	ch-='0';
	if(ch==4)
		ji[2]+=2;
	else if(ch==6)
	{
		ji[2]++;
		ji[3]++;
	}
	else if(ch==8)
		ji[2]+=3;
	else if(ch==9)
		ji[3]+=2;
	else
		ji[ch]++;
}

int ok(char ch)
{
	ch-='0';
	if(ch==1)
		return 1;
	if(ch==4)
	{
		if(ji[2]>=2)
			return 1;
		return 0;
	}
	else if(ch==6)
	{
		if(ji[2]>=1&&ji[3]>=1)
			return 1;
		return 0;
	}
	else if(ch==8)
	{
		if(ji[2]>=3)
			return 1;
		return 0;
	}
	else if(ch==9)
	{
		if(ji[3]>=2)
			return 1;
		return 0;
	}
	else
	{
		if(ji[ch])
			return 1;
		return 0;
	}
	return 1;
}

void dec(char ch)
{
	ch-='0';
	for(int i=0;i<=3;i++)
	{
		while(prim[i]&&ch%prim[i]==0)
		{
			ji[prim[i]]--;
			ch/=prim[i];
		}
	}
}

void change(char str[],int len,int id)
{
	for(int i=len-1;i>id;i--)
	{
		for(char j='9';j>='1';j--)
		{
			if(ok(j))
			{
				dec(j);
				str[i]=j;
				break;
			}
		}
	}
}
void deal(char str[],int len)
{
	memset(ji,0,sizeof ji);
	int flag=0;
	for(int i=len-1;i>=0;i--)
	{
		add(str[i]);
		for(char j=str[i]+1;j<='9';j++)
		{
			if(ok(j))
			{
				dec(j);
				str[i]=j;
				change(str,len,i);
				flag=1;
				break;
			}
		}
		if(flag)
			break;
	}
	if(flag==0)
	{
		printf("1");
		change(str,len,-1);
	}
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%s",str);
		int len=strlen(str);
		int cnt=ling(str);
		if(cnt==1&&str[len-1]=='0')//只有末尾一个零
		{
			int flag=0;
			for(int i=len-2;i>=0;i--)
			{
				if(str[i]!='9')
				{
					str[i]++;
					flag=1;
					break;
				}
				else
					str[i]='0';
			}
			if(flag==0)
				printf("1");
		}
		else if(cnt>=1)
		{
			for(int i=len-1;i>=0;i--)
			{
				if(str[i]!='9')
				{
					str[i]++;
					break;
				}
				else
					str[i]='0';
			}
		}
		else
			deal(str,len);

		printf("%s\n",str);

	}
	return 0;
}
时间: 2024-07-29 14:24:14

zoj 3327 Friend Number 贪心的相关文章

ZOJ 3327 Friend Number(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3327 Friend Number Time Limit: 1 Second      Memory Limit: 32768 KB Given a positive integer x, let P(x) denotes the product of all x's digits. Two integers x and y are friend numbers

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ 3233 Lucky Number

Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 323364-bit integer IO format: %lld      Java class name: Main Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number

FZU OJ 2111 Min Number (贪心)

Problem 2111 Min Number Accept: 586    Submit: 1139 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choo

ZOJ 2702 Unrhymable Rhymes 贪心

贪心,能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge An amateur poet Willy is going to write his first abstract poem. Since abstract art does not give much care to the meaning of the poem, Willy is plan

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some

ZOJ 3622 Magic Number 打表找规律

A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3622 Appoint description: Description A positive number y is called magic number if for every positive integer x it satisfies that

Min Number 贪心,把最小的数放在最前

题目链接 Problem 2111 Min Number Accept: 499    Submit: 949 Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to c

POJ 1862 &amp; ZOJ 1543 Stripies(贪心 | 优先队列)

题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=543 Description Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian -