Article directory
Introduction to the pow function:
topic:
In the skew binary representation, the value xk of the kth bit represents xk * (2k+1-1). The possible numbers on each bit are 0 or 1, the last non-zero bit can be 2, for example, 10120(skew) = 1 * (25-1) + 0 * (24-1) + 1 * (23- 1) + 2 * (22-1) +0 * (21-1) = 31 + 0 + 7 + 6 + 0 = 44. The first ten skew numbers are 0, 1, 2, 10, 11, 12, 20 , 100, 101, and 102.
Input format:
The input consists of one or more lines, each line containing an integer n. If n = 0 it means the end of input, otherwise n is a skew number.
Output format:
For each input, output its decimal representation. After converting to decimal, n does not exceed 231-1 = 2 147 483 647.
Input sample:
10120
200000000000000000000000000000
10
1000000000000000000000000000000
11
100
11111000001110000101101102000
0
Sample output:
44
2147483646
3
2147483647
4
7
1041110737
Problem solving code:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char a[1000000]={};
long sum=0;
while(scanf("%s",a)!=EOF)
{
if(strlen(a)==1&&a[0]=='0') break;
for(int i=0;i<strlen(a);i++)
sum+=(a[i]-48)*(pow(2,strlen(a)-i)-1);
printf("%ld\n",sum);
a[1000000]={};
sum=0;
}
return 0;
}
Introduction to the pow function:
sum+=(a[i]-48)*(pow(2,strlen(a)-i)-1);
1. Introduction: The prototype in TC2.0 is extern float pow(float x, float y);
In VC6.0, the prototype is double pow( double x, double y );
2. Header file: #include <math.h>
3. Function function: calculate the yth power of x
4. Return value: double type, int, float will give a warning
Note: When x is negative and y is a decimal, or x is 0 and y is less than or equal to 0, an error will occur
If the function parameter does not match the required type, it can be cast to achieve the purpose