zoukankan      html  css  js  c++  java
  • 错题集

    一、C语言向上、向下取整问题

    math库—— ceil函数是取上整,floor函数是取下整

    ceil,用于返回大于或者等于指定表达式的最小整数,用法是double ceil。返回不小于 value 的下一个整数,value如果有小数部分则进一位;ceil返回的类型仍然是float,因为float值的范围通常比integer要大

    题目地址

    根据邮件的重量和蒜头君是否选择加急计算邮费。计算规则:重量在 1000克以内(包括 1000克),基本费 8 元。

    超过 1000克的部分,每 500克加收超重费 4 元,不足 500 克部分按 500 克计算;

    如果蒜头君选择加急,多收 5 元。

    输入格式

    输入一行,包含一个正整数和一个字符,以一个空格分开,分别表示重量(单位为克,不超过 2⋅1052cdot 10^52105)和是否加急。

    如果字符是'y',说明选择加急;如果字符是'n',说明不加急。

    #include <stdio.h>
    #include <math.h>
    int main(){
        int n,x;
        char ji;
        scanf("%d %c",&n,&ji);
        if(ji == 'y'){
            if(n < 1000){
                printf("13");
            }else{
                x = (int) ceil((double) (n-1000)/500);
                printf("%d", 13+ x * 4);
            }
        }else{
            if(n < 1000){
                printf("8");
            }else{
               x = (int) ceil((double) (n-1000)/500);
                printf("%d", 8+ x * 4);
            }
        }
        return 0;
    }

    题目地址

    蒜头君买了一箱 n 个苹果,很不幸的是买完时箱子里混进了一条虫子。虫子每 x 小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过 y小时你还有多少个完整的苹果?

    C语言

    #include <stdio.h>
    #include <math.h>
    int main(){
     int n,x,y;
     int eat =0;
        scanf("%d %d %d",&n,&x,&y);
        if(y % x >0){
            eat = 1 + (y / x);
        }else{
            eat = y / x;
        }
        if(eat >= n){
            printf("0");
        }else if(eat < n){
            printf("%d",n - eat);
        }
        return 0;
    }

    c++

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main () {
     long long n;
     long double x, y;
     cin >> n >> x >> y;
     if (floor(n - y / x) <= 0) {
            cout << 0;
            return 0;
        }
        cout << floor(n - y / x);
     return 0;
    }
  • 相关阅读:
    韩式英语
    Daily dictation 听课笔记
    words with same pronunciation
    you will need to restart eclipse for the changes to take effect. would you like to restart now?
    glottal stop(britain fountain mountain)
    education 的发音
    第一次用Matlab 的lamada语句
    SVN的switch命令
    String的split
    SVN模型仓库中的资源从一个地方移动到另一个地方的办法(很久才解决)
  • 原文地址:https://www.cnblogs.com/expedition/p/11286782.html
Copyright © 2011-2022 走看看