列表

详情


JAVA1. 类型转换

描述

设计一个方法,将一个小于2147483647的double类型变量以截断取整方式转化为int类型

输入描述

随机double类型变量

输出描述

转化后的int类型变量

示例1

输入:

12.34

输出:

12

示例2

输入:

1.88

输出:

1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C 解法, 执行用时: 2ms, 内存消耗: 304KB, 提交时间: 2021-10-16

#include <stdio.h>
#include <stdlib.h>
int main()
{
    double n;
    scanf("%lf",&n);
    printf("%d",(int)n);
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 388KB, 提交时间: 2021-10-15

#include<iostream>
using namespace std;
int main(){
    
    double d;
    int i;
    cin>>d;
    i=(int)d;
    cout<<i<<endl;
    
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 444KB, 提交时间: 2021-10-16

#include <iostream>

using namespace std;

int main() {
    double x;
    cin >> x;
    int y = (int) x;
    
    cout << y;
    
    return 0;
}

C++ 解法, 执行用时: 5ms, 内存消耗: 460KB, 提交时间: 2021-10-15

#include <cstdio>

int main() {
    double f;
    scanf("%lf", &f);
    printf("%d\n", int(f));
}

C++ 解法, 执行用时: 6ms, 内存消耗: 492KB, 提交时间: 2021-10-15

#include<bits/stdc++.h>
using namespace std;
double n;
int main(){
    cin>>n;
    cout<<int(n);
    return 0;
}

上一题