在 Beecrowd 平台上修复小数点后的计算百分比错误。

huangapple go评论64阅读模式
英文:

How to fix decimal error after the dot on Beecrowd platform for calculating percentages?

问题

当我将此代码放入“Beecrowd”平台时,生成小数点后面的数字时出现错误。

我想要结果以百分比表示:

Percentual de coelhos: 32.62 %
Percentual de ratos: 33.48 %
...

我已经从`float`更改为`double`,但仍然不起作用。
英文:

For some reason when I put this code into the "Beecrowd" platform it shows as an error when generating decimals after the dot

#include <stdio.h>

double pct_cobaias(int x, int y);

int main() {
    
    int contador, cobaia;
    char tipo;
    int soma_cobaia, soma_coelho, soma_rato, soma_sapo;
    double pct_coelho, pct_rato, pct_sapo;

    scanf("%d", &contador);

    soma_cobaia = 0;
    soma_coelho = 0;
    soma_rato = 0;
    soma_sapo =  0;

    while (contador > 0)
    {
        scanf("%d %c", &cobaia, &tipo);

        if (cobaia >= 1 && cobaia <= 15)
        {
            if (tipo == 'C' || tipo == 'R' || tipo == 'S')
            {
                soma_cobaia += cobaia;

                if (tipo == 'C')
                {
                    soma_coelho += cobaia;
                }

                if (tipo == 'R')
                {
                    soma_rato += cobaia;
                }

                if (tipo == 'S')
                {
                    soma_sapo += cobaia;
                }
            }
        }

        contador--;
    }

    pct_coelho = pct_cobaias(soma_coelho, soma_cobaia);
    pct_rato = pct_cobaias(soma_rato, soma_cobaia);
    pct_sapo = pct_cobaias(soma_sapo, soma_cobaia);

    printf("Total: %d cobaias\n", soma_cobaia);

    printf("Total de coelhos: %d\n", soma_coelho);
    printf("Total de ratos: %d\n", soma_rato);
    printf("Total de sapos: %d\n", soma_sapo);

    printf("Percental de coelhos: %.2lf %\n", pct_coelho);
    printf("Percental de ratos: %.2lf %\n", pct_rato);
    printf("Percental de sapos: %.2lf %\n", pct_sapo);

    return 0;
}

double pct_cobaias(int x, int y) {

    double pct;

    pct = (x * 100) / y;

    return pct;
}

I would like the result in percent:

Percentual de coelhos: 32.62 %
Percentual de ratos: 33.48 %
...

I already changed from float to double and it still doesn't work

答案1

得分: 1

你可以使用以下翻译好的代码部分:

你可以使用以下代码来计算百分比:`pct = (x * 100.0) / y;`,但是请注意,`x`  `y` 的类型是 `int`,所以编译器会使用整数运算,结果也将是整数。你可以通过这样修复这个问题:

```c
pct = (x * 100.0) / y;

另外,请注意,在 printf 的格式字符串中,你必须使用 %% 来输出 % 字符。

这是修改后的代码版本:

#include <stdio.h>

int main(void) {
    
    int contador, cobaia;
    char tipo;
    double pct_coelho, pct_rato, pct_sapo;

    int soma_cobaia = 0;
    int soma_coelho = 0;
    int soma_rato = 0;
    int soma_sapo =  0;

    if (scanf("%d", &contador) != 1)
        return 1;

    while (contador > 0) {

        if (scanf("%d %c", &cobaia, &tipo) != 2)
            break;

        if (cobaia >= 1 && cobaia <= 15) {
            switch (tipo) {
              case 'C':
                soma_coelho += cobaia;
                soma_cobaia += cobaia;
                break;
              case 'R':
                soma_rato += cobaia;
                soma_cobaia += cobaia;
                break;
              case 'S':
                soma_sapo += cobaia;
                soma_cobaia += cobaia;
                break;
              default:
                printf("未知产品类型\n");
                break;
            }
        } else {
            printf("无效数量:%d\n", cobaia);
        }
        contador--;
    }

    pct_coelho = soma_coelho * 100.0 / soma_cobaia;
    pct_rato = soma_rato * 100.0 / soma_cobaia;
    pct_sapo = soma_sapo * 100.0 / soma_cobaia;

    printf("总共: %d 仓鼠\n", soma_cobaia);

    printf("总共的兔子: %d\n", soma_coelho);
    printf("总共的老鼠: %d\n", soma_rato);
    printf("总共的青蛙: %d\n", soma_sapo);

    printf("百分比的兔子: %.2f %%\n", pct_coelho);
    printf("百分比的老鼠: %.2f %%\n", pct_rato);
    printf("百分比的青蛙: %.2f %%\n", pct_sapo);

    return 0;
}

希望这对你有所帮助!

英文:

You compute the percentage with pct = (x * 100) / y; but x and y have type int so the compiler uses integer arithmetics and the result is an integer. You can fix this problem by writing:

   pct = (x * 100.0) / y;

Also note that you must use %% in the printf format string to output a % character.

Here is a modified version:

#include &lt;stdio.h&gt;
int main(void) {
int contador, cobaia;
char tipo;
double pct_coelho, pct_rato, pct_sapo;
int soma_cobaia = 0;
int soma_coelho = 0;
int soma_rato = 0;
int soma_sapo =  0;
if (scanf(&quot;%d&quot;, &amp;contador) != 1)
return 1;
while (contador &gt; 0) {
if (scanf(&quot;%d %c&quot;, &amp;cobaia, &amp;tipo) != 2)
break;
if (cobaia &gt;= 1 &amp;&amp; cobaia &lt;= 15) {
switch (tipo) {
case &#39;C&#39;:
soma_coelho += cobaia;
soma_cobaia += cobaia;
break;
case &#39;R&#39;:
soma_rato += cobaia;
soma_cobaia += cobaia;
break;
case &#39;S&#39;:
soma_sapo += cobaia;
soma_cobaia += cobaia;
break;
default:
printf(&quot;unknown product type\n&quot;);
break;
}
} else {
printf(&quot;invalid amount: %d\n&quot;, cobaia);
}
contador--;
}
pct_coelho = soma_coelho * 100.0 / soma_cobaia;
pct_rato = soma_rato * 100.0 / soma_cobaia;
pct_sapo = soma_sapo * 100.0 / soma_cobaia;
printf(&quot;Total: %d cobaias\n&quot;, soma_cobaia);
printf(&quot;Total de coelhos: %d\n&quot;, soma_coelho);
printf(&quot;Total de ratos: %d\n&quot;, soma_rato);
printf(&quot;Total de sapos: %d\n&quot;, soma_sapo);
printf(&quot;Percental de coelhos: %.2f %%\n&quot;, pct_coelho);
printf(&quot;Percental de ratos: %.2f %%\n&quot;, pct_rato);
printf(&quot;Percental de sapos: %.2f %%\n&quot;, pct_sapo);
return 0;
}

huangapple
  • 本文由 发表于 2023年5月29日 23:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定