PAT乙级真题(2)个位数统计 (15) 程序运行时间(15)

PAT乙级真题(2)个位数统计 (15) 程序运行时间(15)

题目描述

给定一个k位整数N = dk-110k-1 + … + d1101 + d0 (0<=di<=9, i=0,…,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。

题目链接

输入描述:

每个输入包含1个测试用例,即一个不超过1000位的正整数N。

输出描述:

对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。

输入例子:

100311

输出例子:

0:2
1:3
3:1

思路:

长度1000,估计longlong型也不够用,直接字符串接收,然后对应数字数组的位置++,然后按照格式要求输出即可.

代码:


#include <iostream>
#include<string>
using namespace std;
int main()
{
	string s;
	cin >> s;
	int num[10] = { 0 };
	for (int i = 0; i <s.length(); i++)
	{
		num[s[i] - '0']++;
	}
	for (int i = 0; i <10; i++)
	{
		if (num[i]!=0)
			cout << i << ":" << num[i] << endl;
		else {}
	}
	return 0;
}

题目描述

要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数f的运行时间,我们只要在调用f之前先调用clock(),获得一个时钟打点数C1;在f执行完成后再调用clock(),获得另一个时钟打点数C2;两次获得的时钟打点数之差(C2-C1)就是f运行所消耗的时钟打点数,再除以常数CLK_TCK,就得到了以秒为单位的运行时间。这里不妨简单假设常数CLK_TCK为100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

题目链接

输入描述:

输入在一行中顺序给出2个整数C1和C2。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且取值在[0, 107]

输出描述:

在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”(即2位的“时:分:秒”)格式输出;不足1秒的时间四舍五入到秒。

输入例子:

123 4577973

输出例子:

12:42:59

思路:

先翻译下题:

给你 c1 c2 两个数字,c2-c1 之后的数字 除以 100 ,四舍五入,最后的数字就是 总共的秒数,然后把这个秒数转换为 小时:分:秒的形式

容易被卡的点:

测试用例:
0 100

对应输出应该为:

00:00:01

你的输出为:

00:0:1

代码:


#include <iostream>
#include <string>
using namespace std;
int main()
{
	long c1 = 0, c2 = 0;
	long res;
	cin >> c1 >> c2;
	
		res = (c2 - c1);
		//cout << res << endl;
		res = res / 10;
		long temp = res % 10;
		if (temp > 4)
		{
			res = (res / 10) + 1;
		}
		else
		{
			res = res / 10;
		}
		int h = 0;
		int f = 0;
		int s = 0;
		h = res / 3600;
		res = res - h * 3600;
		f = res / 60;
		res = res - f * 60;
		s = res;
		printf("%02d:%02d:%02d", h, f, s);
		
		/*if(h==0&&f==0&&s==0)
			cout << "00:00:00";
		else
		{
			if (h > 10)
				if (res == 0)
					cout << h << ":" << f << ":00" << endl;
				else
					cout << h << ":" << f << ":" << s << endl;
			else
				if (res == 0)
					cout << h << ":" << f << ":00" << endl;
				else
					cout << "0" << h << ":" << f << ":" << s << endl;
		}*/
		//  比较麻烦的输出方法
		
	return 0;
}

对于这种 最后数字不足两位的需要加0,可以用printf 中的 %02d.

优化:

如何将浮点数 f 四舍五入为整形数 利用 (int)(f + 0.5) 来实现

链接:https://www.nowcoder.com/questionTerminal/fabbb85fc2d342418bcacdf0148b6130
来源:牛客网

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int k,c1,c2,i,a,b,c;
 cin >> c1 >> c2;
    const int ctk = 100;
    k = (int)(1.0 * (c2 - c1) / ctk + 0.5);//四舍五入的关键
 a = k / 3600;
 b = (k % 3600) / 60;
 c = k % 60;
 printf("%02d:%02d:%02d",a,b,c);
    return 0;
}
// 如何将浮点数 f 四舍五入为整形数  利用 (int)(f + 0.5) 来实现 

按照大佬的写法优化自己的版本:


#include <iostream>
#include <string>
using namespace std;
int main()
{
		long c1 = 0, c2 = 0;
		long res;
		cin >> c1 >> c2;
		res = (c2 - c1);
		res = (int)(1.0 * (c2 - c1) / 100 + 0.5);
		int h = 0;
		int f = 0;
		int s = 0;
		h = res / 3600;
		f = (res % 3600) / 60;
		s = res% 60;
		printf("%02d:%02d:%02d", h, f, s);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值