Skip to content

Instantly share code, notes, and snippets.

@alswl
Last active October 12, 2021 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alswl/4e9c60a592996b03cb2e5700286a400d to your computer and use it in GitHub Desktop.
Save alswl/4e9c60a592996b03cb2e5700286a400d to your computer and use it in GitHub Desktop.
app.c for zyy
#include <stdio.h>
int main(int argc, char const *argv[])
{
int count;
double input, output;
// 0. prepare count
scanf("%d", &count);
printf("count: %d\n", count);
int levels[count + 1];
double ratios[count + 1];
count += 1;
// 1. prepare levels and ratio
levels[0] = 0;
ratios[0] = 0;
for (int i = 1; i < count; i++) {
scanf("%d %lf", &levels[i], &ratios[i]);
printf("%d %lf\n", levels[i], ratios[i]);
}
scanf("%lf", &input);
printf("input: %.2f\n", input);
// 2. process
for (int i = 1; i < count - 1; i++) {
// reach the level ceiling, break
if (input < levels[i]) {
output += (input - levels[i - 1]) * ratios[i];
printf("stage 1 %.2f\n", output);
break;
}
// accumulate current level
output += (levels[i] - levels[i - 1]) * ratios[i];
printf("stage 2 %.2f\n", output);
}
// last level
if (input > levels[count - 2]) {
output += (input - levels[count - 1 - 1]) * ratios[count - 1];
printf("stage 3 %.2f\n", output);
}
// 3. output
printf("%.2f", output);
return 0;
}
// gcc app.c
// echo '3\n1000 0.1\n2000 0.2\n-1 0.3\n2500' | ./a.out
// echo '6\n100000 0.1\n200000 0.075\n400000 0.05\n600000 0.03\n1000000 0.015\n-1 0.01\n150000' | ./a.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment