Rounding Up Numbers in a Far Away Kingdom: A C Programming Challenge

The Story:

In a far away kingdom, the arrival of the ACMers brought about a peculiar problem. These newcomers loved high-precision numbers, causing chaos for the kingdom's merchants. To simplify matters, the King decreed a new set of rounding rules:

  1. If a number's integer part does not end in 9 and its decimal part is less than 0.5: The rounded number is the same as the integer part.2. If a number's integer part does not end in 9 and its decimal part is 0.5 or greater: The rounded number is the integer part plus 1.3. If a number's integer part ends in 9: Consult Vasilisa the Wise for the complicated rounding operation.

Your Challenge:

You are tasked with creating a C program that implements these rounding rules.

Input:

The program should accept a single number as input, including its decimal part (e.g., '123.456', '99.9', '0.7'). The number's length should not exceed 1000 characters.

Output:

The program should output the rounded number without leading zeros. If the integer part of the input number ends in '9', the output should be 'GOTO Vasilisa.'

**Example C Code:**c#include <stdio.h>#include <string.h>

void roundNumber(char *number) { int length = strlen(number); int lastDigit = number[length-1] - '0';

if (lastDigit != 9) {        if (number[length-2] == '.' && lastDigit >= 5) {            number[length-3] += 1;         } else if (number[length-2] != '.' && lastDigit >= 5) {            number[length-2] = '0';            number[length-3] += 1;        }        number[length-2] = '�';    } else {        printf('GOTO Vasilisa.

'); return; }

printf('%s

', number);}

int main() { char number[1001];

// Read the number to round up    scanf('%s', number);

// Perform rounding    roundNumber(number);

return 0;}

Explanation:

  1. The roundNumber function analyzes the input number character by character.2. It handles rounding based on the last digit and the presence of a decimal point.3. The 'GOTO Vasilisa.' output is triggered when the integer part ends in '9'.

Try It Out!

This challenge provides a fun way to practice C programming concepts, including string manipulation, conditional statements, and problem-solving. Experiment with different input numbers and see how the program handles them according to the King's unique rounding rules!

Rounding Up Numbers in a Far Away Kingdom: A C Programming Challenge

原文地址: https://www.cveoy.top/t/topic/SoJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录