B1026.程序运行时间
1.四舍五入的写法。利用小数部分加上0.5则除法进1的性质。在被除数的基础上加上0.5乘以除数即可。例如题中的0.5*100 =50,则只要在被除数上加50就可以实现四舍五入。
2.int 2的31次方,10的9次方数量级
longlong 2的63次方,大概是10的19次方
/*B1026*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
//int 2的31次方,10的9次方数量级
//longlong 2的63次方,大概是10的19次方
int main()
{
int c1, c2;
cin >> c1 >> c2;
int result = (c2 - c1+50)/100;//四舍五入,如果小数部分大于0.5则进位了,50由0.5*100得出
int hour = result / 3600;
result = result % 3600;
int minute = result / 60;
int second = result % 60;
printf("%02d:%02d:%02d", hour, minute, second);
return 0;
}
B1027.打印沙漏
1.中央对齐这个功能是可以根据每行数据的规律实现的。调试打印变量多要注意。
2.程序核心是找出合适的外场数量即可。
/*B1027*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
int Shulie[100];
int main()
{
Shulie[0] = 1;
for(int i = 1,j = 3;Shulie[i-1]<1005;i++,j+=2)//初始化数列数组
Shulie[i] = Shulie[i - 1] + 2 * j;
int N;
char OutPut;
cin >> N>>OutPut;
int i = 0;
for (; i < 100; i++)
{
if (N < Shulie[i]) break;
}
int FinalNum = Shulie[i - 1];//找出真实的总数
int ExtraNum = N - FinalNum;
int TmpSum = 0;
int j = 1;
for (;TmpSum < (FinalNum + 1) / 2; j += 2)
{
TmpSum += j;
}
j = j - 2;
int WaiChen = j;//找出了最外层个数
while (j>0)//递减打印
{
int x = j;
for (int z = (WaiChen - x) / 2; z > 0; z--)
cout << " ";
for (; x > 0; x--) {
cout << OutPut;
}
cout << "\n";
j -= 2;
}
j = 1;
while (j<WaiChen)//递减打印
{
j += 2;
int x = j;
for (int z = (WaiChen - x) / 2; z > 0; z--)
cout << " ";
for (; x > 0; x--) {
cout << OutPut;
}
cout << "\n";
}
cout << ExtraNum;
return 0;
}
B1028.人口普查
1.要习惯***字符串是可以直接比较大小的***,比转换成Int来要方便的多。
2.自己写的时候过于依赖容器和sort。要注意如果***只是为了找最小最大值,完全可以用赋值来做***,更方便。
3.柳神的代码相比自己的优势太大。
/*B1028*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
int main()
{
int n, cnt = 0;
cin >> n;
string name, birth, maxname, minname;
string maxbirth ="1814/09/06" , minbirth = "2014/09/06";//反着定初始值
for (int i = 0; i < n; i++)
{
cin >> name >> birth;
if (birth >= "1814/09/06" && birth <= "2014/09/06")
{
cnt++;
if (birth >= maxbirth)
{
maxbirth = birth;
maxname = name;
}
if (birth <= minbirth)
{
minbirth = birth;
minname = name;
}
}
}
if(cnt!= 0)
cout << cnt << " " << minname << " " << maxname;
return 0;
}
B1029.旧键盘(参考柳神用法)
1.string.find()函数的用法,返回一个int索引值。当str.find(某char) == string::nops代表没找到。
2.toupper()小写转大写,原本是大写不变。 tolower()是反过来的。
3.字符串的增加直接用加号就可以了 str+=char;
/*B1029*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
int main()
{
string s1, s2,ans;
cin >> s1>> s2;
for (int i = 0; i < s1.length(); i++)
{
if (s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos)
ans += toupper(s1[i]);
}
for (int i = 0; i < ans.length(); i++)
cout << ans[i];
return 0;
}
B1030.完美数列 13min
自己做的思路和柳神完全一样,出了一个超时点。
/*B1030*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int N, p;
cin >> N >> p;
vector<int> vec;
for (int i = 0; i < N; i++)
{
int x;
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
int MaxCnt = 0,cnt = 0;
for (int i = 0; i < N; i++)
{
int mp = vec[i] * p;
for (int j = i; j < N; j++)
{
if (vec[j] <= mp)
{
cnt = j - i + 1;
}
else
break;
}
if (cnt > MaxCnt)
MaxCnt = cnt;
}
cout << MaxCnt;
return 0;
}
B1031.检查身份证 21min
1.注意char的数字不等于int的同数字,编码不同,所以效验数组M里面所有的字符都要打单引号。
2.检查是否是数字直接 char>=‘0’ && char<= ‘9’ 即可,和检验大小写字母异曲同工。
/*B1030*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int Right[17] = {
7,9,10,5,8,4,2,1,6,3,7,9,10,5,