CF12B Correct Solution?

CF12B Correct Solution?,第1张

CF12B Correct Solution? 题目描述

One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and giving each other interesting problems to solve. When it was Alice's turn, she told the number nn to Bob and said:

—Shuffle the digits in this number in order to obtain the smallest possible number without leading zeroes.

—No problem! — said Bob and immediately gave her an answer.

Alice said a random number, so she doesn't know whether Bob's answer is correct. Help her to find this out, because impatient brother is waiting for the verdict.

输入格式

The first line contains one integer nn ( 0<=n<=10^{9}0<=n<=109 ) without leading zeroes. The second lines contains one integer mm ( 0<=m<=10^{9}0<=m<=109 ) — Bob's answer, possibly with leading zeroes.

输出格式

Print OK if Bob's answer is correct and WRONG_ANSWER otherwise.

题意翻译 题目描述(舍弃各种乱七八糟的故事)

给定一个数NN,要求把NN的各个数位打乱,要求组成一个可能的,最小的数(最小数有可能含有前导00)。现在已经有一个“最小数”,请你判断这个“最小数”是不是最小数。

第一行输入n不含前导0。
第二行输入的假定的最小数可能含有前导0。 题目要求排序后的最小数不含前导0。

输入格式

两行。 第一行是给定的数NN。 第二行是假定的NN重组的“最小数”。

输出格式

一行。 如果输入的最小数就是NN重组的最小数,输出“OK”。 否则输出“WRONG_ANSWER”。

Translated by LiM_817

输入输出样例

输入 #1复制

3310
1033

输出 #1复制

OK

输入 #2复制

4
5

输出 #2复制

WRONG_ANSWER

写题过程:开始用int型数组,采用最开始的拆分数位然后冒泡排序的方法弄的,然后改了好几个地方,改成快排也不对

这是错误代码:

#include
int a[100000];

void Quick_Sort(int left,int right)
{
    if(left>=right)
	{
		return;
	}
	int num=a[(left+right)/2];
	int l=left,r=right;
	
	do
	{
		while(a[r]>num) r--;
		while(a[l]=l)
		{
			int t=a[r];
			a[r]=a[l];
			a[l]=t;
			r--,l++;
		}
	}while(l<=r);
	if(left0)
	{
		a[i]=m%10;
		i++;
		n++;
		m/=10;
	}
	Quick_Sort(1,n);
	for(int i=1;i<=n;i++)
	{
		printf("%d ",a[i]);
	}
	printf("n");
	
	for(int i=1;i<=n;i++)
	{
		if(a[i]!=0)
		{
			int t=a[1];
			a[1]=a[i];
			a[i]=t;
			break;
		}
	}
	for(int i=1;i<=n;i++)
	{
		printf("%d ",a[i]);
	}
	printf("n");
	
	for(int i=1;i<=n;i++)
	{
		mmin=mmin*10+a[i];
		//printf("a[%d]=%d min=%dn",i,a[i],mmin);
	}
	//printf("%dn",mmin);
	if(min==mmin) printf("OK");
	else printf("WRONG_ANSWER");
	
	return 0;
}

 其实结果是没什么问题,看CSDN的正确题解思路都是一样的,但是人家是用char型数组输入,直接用字符就省去了拆分数位和排序那些麻烦的步骤,直接用sort函数就可以了

下面是AC代码:

#include 
#include 
#include 
using namespace std;
 
int main()
{
    char a[15],b[15];
    gets(a);
    gets(b);
    
    int len = strlen(a);
	sort(a,a+len);
	
    for(int i = 0;i < len;i ++)
    {
        if(a[i] != '0')
        {
			char t;
			t = a[0];
			a[0] = a[i];
			a[i] = t;  
        	break;
        }
    }
    
    if(strcmp(a,b) == 0)
    {
        printf("OK");
	}
	else
	{
        printf("WRONG_ANSWER");
	}
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5714386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存