一个乘以2的数字给出了它的排列,我的代码没有给出任何这样的排列。 [英] A number which when multiplied by 2 gives its permutation,my code isn't giving any such .

查看:107
本文介绍了一个乘以2的数字给出了它的排列,我的代码没有给出任何这样的排列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<math.h>
int numdig(int x)
{
    int i=0;
    for(i=0;i<100;i++)
    {
        x=x/10;
        if(x==0)
            break;
    }
return (i+1);
}
void sort(int a[],int n)
{
    int i=0,j,temp;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
               temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
}
int numcheck(int x)
{   int count=0;
    int i=0,j,y;
    int a[50],b[50];
    for(i=0;i<50;i++)
    {
        a[i]=x%10;
        x=x/10;
        if(x==0) break;
    }
    j=i; y=2*x;
     for(i=0;i<50;i++)
    {
        b[i]=y%10;
        y=y/10;
    }
    sort(a,j+1);sort(b,j+1);
    for(i=0;i<j;i++)
    {
        if(a[i]==b[i]) count++;
    }
    if(count==j+1) return 1;
    else return 0;
}
int main()
{
  long long int a,b,i; int t,k;
    printf("enter the number of cases\n");
    scanf("%d",&t);

    for(k=0;k<t;k++)
    {
printf("enter the number which are upper bound and lower bound\n");

    scanf("%d",&a);
    scanf("%d",&b);
    for(i=a;i<b;i++)
    {
        if(numdig(i)!=numdig(2i))
            break;
        else if (numcheck(i)==1)
        printf("this is one of the required number : %d",i);
        else continue;
    }
    }
    return 0;

}

推荐答案

首先考虑回文是什么:它是一个反转的词或短语与开始时的情况相同:例如TACO CAT。

所以你要找的是像12345这样的数字,当加倍等于它自己的反转:54321(显然,那个不起作用)。



所以首先写一个函数来返回一个数字的回文:传递一个整数(12345,比方说)和它返回它的反向(这次是54321)。然后你可以用它来比较,看看它是否是相同的数字。



由于这是你的作业,我不会给你代码。但是我会给你一两个线索:

1)如果使用10,模数运算符%将提取最低有效十进制数字。

2)如果你使用10,则整数除法运算符/将丢弃最低有效数字。

3)循环直到值为零真的,非常容易......



试一试:如果遇到问题并且无法解决问题,请随时询问。
Start by thinking about what a Palindrome is: it's a word or phrase that when reversed is the same as it was to start with: "TACO CAT" for example.
So what you are looking for is a number like "12345" that when doubled equals it's own reverse: "54321" (clearly, that one doesn't work).

So start by writing a function to return the palindrome of a number: pass it an integer (12345, say) and it returns it's reverse (54321 this time). You can then use that to compare and see if it's the same number.

Since this is your homework, I won't give you the code. But I'll give you a clue or two:
1) The modulus operator "%" will extract the least significant decimal digit if you use 10.
2) The integer divide operator "/" will "throw away" the least significant digit if you use 10.
3) Looping until a value is zero is really, really, easy...

Give it a try: if you meet a problem and you can't solve it, feel free to ask about it.


您的想法是很好,但实施不太好。

以下代码(请注意:我不认为它是正确的:-))生产

Your ideas are good, however the implementation is not so good.
The following code ( please note: I don't claim it is correct :-) ) produces
found 125874 (251748)



作为第一个这样的数字。




as first of such numbers.

#include<stdio.h>

void sort(int a[],int n)
{
  int i, j, temp;

  for(i=0;i<n-1;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if ( a[i] > a[j] )
      {
        temp = a[i];
        a[i] = a[j];
        a[j]=temp;
      }
    }
  }
}

int make_array(int x, int a[], int size)
{
  int i;

  for (i = 0; i < size; ++i)
  {
    a[i] = x % 10;
    x /= 10;
    if ( x == 0 ) break;
  }
  return (i +1);
}

int numcheck(int x)
{
  int i;
  int sa, sa2;
  int a[50], a2[50];

  sa  =  make_array( x, a, 50);
  sa2 =  make_array( x * 2, a2, 50);
  if ( sa != sa2) return 0;

  sort( a, sa);
  sort( a2, sa);

  for(i=0; i<sa; i++)
  {
    if ( a[i] != a2[i] ) break;
  }
  if (i != sa) return 0;
  return 1;
}

int main()
{
  int a,b,i; int t,k;
  printf("enter the number of cases\n");
  scanf("%d",&t);

  for(k=0;k<t;k++)
  {
    printf("enter the number which are upper bound and lower bound\n");

    scanf("%d",&a);
    scanf("%d",&b);
    for(i=a;i<b;i++)
    {
      if ( numcheck( i ) == 1)  printf("found %d (%d) \n", i, 2*i);
    }
  }
  return 0;
}


这篇关于一个乘以2的数字给出了它的排列,我的代码没有给出任何这样的排列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆