3.1:简单模拟
1:B1008 数组元素的循环右移
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 110;
int n, m;
int num[maxn];
void reverse(int s, int e){
// [s, e)
int i = s, j = e-1;
while(i < j ){
swap(num[i], num[j]);
i++;
j--;
}
}
int main(){
scanf("%d %d", &n, &m);
int M = m % n; // 计算出循环右移的位数
for(int i = 0; i < n; i++){
scanf("%d", &num[i]);
}
//下面是处理数据
reverse(0, n);
reverse(0, M);
reverse(M, n);
// 下面是输出
for(int i = 0; i < n - 1; i++){
printf("%d ", num[i]);
}
printf("%d", num[n - 1]);
}
2:A1046 Shortest Distance
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100010;
int n, m, re, total = 0;
int num[maxn];
int main(){
cin >> n;
for(int i = 2; i <= n + 1; i++){
int temp;
cin >> temp;
total += temp;
num[i] = num[i-1] + temp;
}
cin>>m;
for(int i = 0; i < m; i++){
int x, y;
cin>>x>>y;
if(x > y) swap(x, y);
re = num[y] - num[x];
re = re < (total - re) ? re : (total - re);
cout<<re<<endl;
}
return 0;
}
3:A1065 A+B and C
这道题需要留意的地方还是非常多的,首先第一点题目中的数据范围应该是:[-2^63, 2^63);如果是题目中的[-2^63, 2^63],这题就需要使用大整数加法的方式求解,不过经测试确实应该是第一个范围。
其次一点就是这题 A+B 是非常有可能超出 long long的数据范围的,这一个需要些代码的时候做单独判断:
如果 A > 0 且 B > 0 然后 A + B < 0;那么就证明正向溢出,那么 A + B > C
如果 A < 0 且 B < 0 然后 A + B >= 0;那么就证明正向溢出,那么 A + B < C
#include<iostream>
#include<string>
using namespace std;
typedef long long LL;
int T;
int main(){
cin >> T;
for(int i = 1; i <= T; i++){
LL A, B, C;
LL re = A + B;
if(A > 0 && B > 0 && re < 0){
cout<<"Case #"<<i<<": true"<<endl;
}
else if(A < 0 && B < 0 && re >= 0){
cout<<"Case #"<<i<<": false"<<endl;
}
else if(re > C){
cout<<"Case #"<<i<<": true"<<endl;
}
else{
cout<<"Case #"<<i<<": false"<<endl;
}
}
return 0;
}
3.2:查找元素
1:A1006 Sign In and Sign Out
#include<iostream>
#include<string>
using namespace std;
int M;
string inmin, outmax; // 分别保存进来最早和出去最晚的 ID
struct Time{
int h;
int m;
int s;
// 下面是构造函数
Time(){ }
Time(int _h, int _m, int _s) : h(_h), m(_m), s(_s){ }
// 下面是重载运算符
bool operator < (const Time t) const{
if(h == t.h){
if(m == t.m){
return s < t.s;
}
else{
return m < t.h;
}
}
else{
return h < t.h;
}
}
};
int main(){
cin>>M;
Time intime(24, 59, 59), outtime(0, 0, 0);
for(int i = 0; i < M; i++){
string str;
Time tin, tout;
cin >> str;
scanf("%d:%d:%d %d:%d:%d",&tin.h, &tin.m, &tin.s, &tout.h, &tout.m, &tout.s);
if(tin < intime){
intime = tin;
inmin = str;
}
if(outtime < tout){
outtime = tout;
outmax = str;
}
}
cout<<inmin<<" "<<outmax;
return 0;
}
3.3:图形输出
1:A1031 Hello World for U
#include<iostream>
#include<string>
using namespace std;
string str;
int main(){
cin>>str;
int N = str.length();
//计算n1和n2的值
int n1, n2 = (N + 2) / 3;
n2 = n2 > 3 ? n2 : 3;
for(n2; ; n2++){
if((N + 2 - n2) % 2 == 0 && (N + 2 - n2) / 2 <= n2){
n1 = (N + 2 - n2) / 2;
break;
}
}
//下面开始输出
int space = n2 -2;
for(int i = 0; i < n1 - 1; i++){
cout<<str[i];
for(int j = 0; j < space; j++){
cout<<" ";
}
cout<<str[N-1 - i]<<endl;
}
int start = n1 - 1;
int end = N-1 - start;
for(int i = start; i <= end; i++){
cout<<str[i];
}
return 0;
}
3.4:日期处理
1:codeup 1928 日期差值
一般日期的差值有三种求法:1:距离相同日期差值之减 2:使用循环 3:直接计算两个日期之间的差值
下面是判断润年的模板代码
bool isLeap(int year){
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
3.5:进制转化
1:其他进制转化为 十 进制
2:十 进制转换为其他进制
3.6:字符串处理
1:A1001 A+B Format
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int a, b;
string str;
int main(){
cin>>a>>b;
int c = a + b;
if(c < 0){
cout<<"-";
c = -c;
}
string tstr = to_string(c);
reverse(tstr.begin(), tstr.end());
for(int i = 0; i < tstr.length(); i++){
str += tstr[i];
if((i + 1) % 3 == 0){
str += ",";
}
}
reverse(str.begin(), str.end());
if(str[0] == ',') str.erase(str.begin());
cout<<str;
return 0;
}