Home > Back-end >  Consult a simple algorithm.
Consult a simple algorithm.

Time:12-19

There are two integers,
The first group: 20,21,22,23... 120
The second group: 0,1,2,3,4... 255

The corresponding relation of two groups of data:
20 corresponds to 0120 corresponds to 255; seven corresponding 255/2=128 (round), so the first set of 21 what value corresponding to the second group? 22 what corresponding value?

Need to enter the first set of a function, an arbitrary value, returns the value of the second group,

CodePudding user response:

B=(a - 20)/(100.0 + 0.001) * 255;

CodePudding user response:

Bother the b rounded, how to write?

CodePudding user response:

 # include & lt; stdio.h> 
#include

Int a, b;

Int main () {
Printf (" please input an arbitrary value in a 20-120: ");
Scanf_s (" % d ", & amp; A);
B=(a - 20)/(100.0 + 0.002) * 255;
//b=((a - 20) + 0.2) * (255/100.0);
printf("%d\n", b);
system("pause");
return 0;
}

Type b is an integer, and automatically remove the decimal point, adding 0.002 * 255 is equivalent to round up the
{
Double a=1.6;
Int b=a + 0.5, c=a;
}
The output is to 2 b, c for 1;

CodePudding user response:

Is this meaning?

 
Int fun (int n) {
Return (int) ((n - 20) * 255.0/(120-20) + 0.5);
}
Int main ()
{
int i;
For (I=20; i<=120; I++) {
Printf (" % d: % d \ n ", I, fun (I));
}
return 0;
}

  • Related