15-Language Introduction-15-6174 questions

Source: Internet
Author: User

Title Address: http://acm.nyist.net/JudgeOnline/problem.php?pid=57 Description
Let's say you have a four-digit number that each has a different number, you get a after all the numbers from the big to the small, you get B from small to large, and then you replace the number with a-a, and you continue to do so. For example, starting from 1234, you can get 4321-1234=3087, 8730-378=8352, 8532-2358=6174, and then back to its own! Now you need to write a program to determine how many times a four-digit number of such operations can occur, and the number of times the operation is calculated.
For example, input 1234 execution order is 1234->3087->8352->6174->6174, the output is 4.

Input
The first line is N, which represents the n set of test data.
The next n lines each write a four-digit number that each of you has a different number.
Output
How many times does the action described above appear to loop
Sample input
1
1234

Sample output
4 Code: #include <stdio.h>

Calculates num into a number from the big to the small a, the number from small to large B
Returns a-b
static int calcurrentvalue (int num);

Insert sort-from backward forward, last one Max
static int Insertarray (int arr[],int len,int insertnum);

int main ()
{
int readlen = 0;
scanf ("%d", &readlen);
GetChar ();

while (Readlen > 0)
{
int num = 0;
scanf ("%d", &num);
GetChar ();

int count = 0;
int tmpnum = num;
Do
{
num = Tmpnum;
Tmpnum = Calcurrentvalue (tmpnum);
++count;
}while (tmpnum! = num);

printf ("%d\n", count);

--readlen;
}

return 0;
}


static int calcurrentvalue (int num)
{
int arr[4] = {0};

int flag = num;
while (Flag > 0)
{
int tmpnum = flag% 10;
Insertarray (Arr,4,tmpnum);
flag = FLAG/10;
}

int max = arr[3]*1000+arr[2]*100+arr[1]*10+arr[0];
int min = arr[0]*1000+arr[1]*100+arr[2]*10+arr[3];

return max-min;
}

static int Insertarray (int arr[],int len,int insertnum)
{
int index = len-1;
for (; index>=0;--index)
{
if (Arr[index] < Insertnum)
Break
}

if (index >= 0)
{
int j=0;
for (; j<index;++j)
{
ARR[J]=ARR[J+1];
}

Arr[index] = Insertnum;
}
The contents of the examination: 1. How to decompose each bit of an integer. Each time you detach from the last one, you only need 10. 2. Sorting problems, to sort, you need to first store the data, the subject determines that the data is 4 digits, so the use of a deterministic array, and the reality is often the number is indeterminate. If you use the STL library method, it is much easier, you can eliminate the problem of the number of changes and sequencing, direct attention to how to solve the problem itself, this shows the great role of STL. The recommended code:
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace Std;
int main ()
{
Freopen ("1.txt", "R", stdin);
int k;
cin>>k;
while (k--)
{
int n,a[4],n1,n2;
scanf ("%d", &n);
int S=1;
while (n!=6174)
{
a[0]=n%10;
a[3]=n/1000;
a[1]=n/10%10;
a[2]=n/100%10;
Sort (a,a+4);
N1=1000*A[3]+100*A[2]+10*A[1]+A[0];
N2=1000*A[0]+100*A[1]+10*A[2]+A[3];
N=N1-N2;
s++;
}
printf ("%d\n", s);
}
}

15-Language Introduction-15-6174 questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.