Home > Back-end >  A piece of code on continuous k hexadecimal output
A piece of code on continuous k hexadecimal output

Time:04-26


This is the topic request, below is the reference code, is really don't understand the truth, and tried to debug, thinking of them still do not understand,
The role of the two functions all understand, but in the face of several output in the main function is not too to understand, especially don't know much about the function of carry and pos,
Definition for bosses.
 

# include & lt; Stdio. H>
# include & lt; String. H>
T=# define exchange (a, b) a, a=b, b=t
Char t;
Void change (char [] a)
{
Int start=0, end;
int len;
Len=strlen (a);
End=len - 1;
While (start{
Exchange (a [start], a [end]);
Start++;
End -;
}
}
Void reprintf (char [], int len)
{
len--;
While (len & gt;=0)
Printf (" % c ", a. [len -]);
printf("\n");
}
Void main ()
{
Char n [128].
Gets (n);
Int j, k;
The scanf (" % d % d ", & amp; K, & amp; m);
int s;
Change (n);
For (s=0; S{
Int carry=1;
Int pos=0;
While (n (pos))
{
N (pos) +=carry;
If (n + k (pos)=='0')
{
N (pos)='0';
Carry=1;
}
The else
{
Carry=0;
}
Pos++;
}
If (carry)
N [pos++]='1';
N (pos)=0;

Reprintf (n, pos);
}
}

CodePudding user response:

Is the input string in turn around first, the purpose is to facilitate its longer carry,
Then add one operation, of course, is to add on the original lowest namely "bits", but now it is in the left side, the subscript the smallest,
Addition on the k into a ah, carry is to carry that 1, add to highs, now is on a downward,
Pos have nothing, you when he is a loop variable I,

CodePudding user response:

You don't understand because carry about a part-time job, every time on integral plus one added to the lowest, this one also expressed by it, anyway, its lowest't below into, idle is idle,

CodePudding user response:

Reference:
 # include & lt; Stdio. H> 
# include & lt; String. H>
T=# define exchange (a, b) a, a=b, b=t
Char t;
Void change (char [] a)
{
Int start=0, end;
int len;
Len=strlen (a);
End=len - 1;
While (start{
Exchange (a [start], a [end]);
Start++;
End -;
}
}
Void reprintf (char [], int len)
{
len--;
While (len & gt;=0)
Printf (" % c ", a. [len -]);
printf("\n");
}
Void main ()
{
Char n [128].
Gets (n);//123456789
Int j, k;//10 3
The scanf (" % d % d ", & amp; K, & amp; m);
int s;
Change (n);//the number of the input string n vertical reverse, low in the top, high in the 987654321
For (s=0; S{
Int carry=1; The number of m/a/n after initial is on the original number of + 1
Int pos=0;//digital vernier from 0 begin
While (n (pos))//from a low of 9, until the end of the string array n=='\ 0' 987654321 '\ 0'
{
N (pos) +=carry;//low 9 to 1
If (n + k (pos)=='0')//if reach the hexadecimal number k value, the k is of type int, n [0] for char, will turn character k + '0'
{
N (pos)='0';//n (pos) zero
Carry=1;//carry 1
}
The else
{
Carry=0;//not carry, carry to 0
}
Pos++;//the next 8
}
If (carry)//if the carry is 1, at the highest position 1
N [pos++]='1';
N (pos)=0;//write string end mark at the end of the n 097654321 '\ 0'

Reprintf (n, pos);//the string n array reverse output 123456790
}
}

//123456789

//10 3
//987654321
//123456790
//123456791
//123456792
//please press any key to continue...
  • Related