nyoj 96-n-1位数 (strlen, atoi, ceil)

本文详细解析了一道算法题,题目要求输入一个大于10且小于等于1000000的整数,求出该整数去掉最高位后的剩余部分。通过C/C++代码实现,首先读取测试数据组数,然后逐行读取测试数据,计算并输出对应的n-1位数。特别地,当除最高位外其余位均为0时,输出0。

96-n-1位数


内存限制:64MB 时间限制:3000ms 特判: No
通过数:30 提交数:47 难度:1

题目描述:

已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数。

输入描述:

第一行为M,表示测试数据组数。
接下来M行,每行包含一个测试数据。

输出描述:

输出M行,每行为对应行的n-1位数(忽略前缀0)。如果除了最高位外,其余位都为0,则输出0。

样例输入:

复制
4
1023
5923
923
1000

样例输出:

23
923
23
0

C/C++ AC:
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <stack>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <climits>
11 
12 using namespace std;
13 int n, m;
14 
15 int main()
16 {
17     cin >>n;
18     while (n --)
19     {
20         char str[10];
21         cin >>str;
22         int str_leng = strlen(str);
23         int int_str = atoi(str); // 字符串 ---> 整数
24         int temp = ceil(pow(10, str_leng - 1)); // ceil 向上取整
25         cout <<int_str % temp <<endl;
26     }
27 }

 

转载于:https://www.cnblogs.com/GetcharZp/p/9320807.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值