201503-1-图像旋转
#include<stdio.h>
#include<iostream>
#include <algorithm>
using namespace std;
//图像逆时针旋转90度
const int N = 1005;
int tu[N][N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin>>tu[i][j];
}
}
for (int j = m; j > 0; j--) {
for (int i = 1; i <= n; i++) {
cout<<tu[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
201503-3-节日
#include<stdio.h>
#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int month_day[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31
};
//判断是否为闰月
int get(int x) {
if (x % 400 == 0 || (x % 4 == 0 && x % 100 != 0))
return 1;//闰月时返回1,加到二月的天数中
else return 0;
}
int main() {
int a, b, c;
int y1, y2;
int day = 2;//1850年1月1日是星期二
cin >> a>>b>>c;
cin >> y1 >> y2;
for (int i = 1850; i <= y2; i++) {
for (int j = 1; j < 13; j++) {
//匹配到对应年月
if (i >= y1&&j==a) {
int temp = 0;
temp = (day - 1) % 7;//上个月的最后一天是星期几
int count = 0;
int limit;
if (j == 2) {
limit= month_day[j] + get(i);
}
else {
limit = month_day[j];
}
for (int k = 1; k <= limit; k++) {
if (temp == c-1) {//用上个月最后一天与查找的前一天匹配
count++;
if (count == b ) {//周数相同
printf("%04d/%02d/%02d\n", i, j, k);
break;
}
}
temp = (temp + 1) % 7;
}
if (count < b) puts("none");
}
//统计天数
if (j == 2) {
day += month_day[j] + get(i);
}
else {
day += month_day[j];
}
}
}
return 0;
}
%7会导致0表示星期天
只要是第三题开始就逻辑有点绕,前面针对日期判断闰年,招对应日子,有部分固定模板可以参考
更多方法参考链接
201503-2-数字排序
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
const int N = 1005;
int a[N];
int main() {
int n;
int temp;
cin>>n;
for (int i = 0; i < n; i++) {
cin >> temp;
a[temp]++;
}
//按照由大到小排序
for (int j = 0; j < n; j++) {
int p = 0;
int max = 0;
for (int k = 0; k < N; k++) {//查询目前最多次数
if (a[k] > max) {
max = a[k];
p = k;
}
}
if(a[p]!=0)
printf("%d %d\n", p, max);
a[p] = 0;
}
return 0;
}
补充:C语言中有内置快排库函数
#include<stdlib.h>
qsort(arr,length,size_t,cmp)
//其中arr代表要排序的数组名,length代表数组的长度 ,size_t代表数组元素的大小,cmp定义为如何比较的的函数,cmp函数可如下定义:
int Cmp(const void *a,const void *b){
return *(type *)a - *(type *)b;
//return*(type*)b-*(type*)a,则是代表非递增排序
}
201412-3-集合竞价
#include<stdio.h>
#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//集合竞价
const int N = 5005;
struct recored
{
int action;//buy=1,sell=2
double price;
int num;
int del;//del=1,删除记录
}R[N];
int main() {
string temp;
double p=0.0;
int s=0;
int del_num=0;
int i = 0;//recored_num
while (cin >> temp) {
if (temp == "buy")
{
cin >> p >> s;
R[++i] = { 1,p,s,0 };
}
else if (temp == "sell")
{
cin >> p >> s;
R[++i] = { 2,p,s,0 };
}
else
{
cin >> del_num;
R[del_num].del = 1;
R[++i].del = 1;
}
}
double p_max = 0;
long long count_max = 0;
for (int j = 1; j <= i; j++) {
long long count = 0;
long long sum1 = 0, sum2 = 0;//符合的交易量
if (R[j].del == 0)
{
double p0 = R[j].price;//开盘价
//统计交易量
for (int k = 1; k <= i; k++) {
if (R[k].del == 0) {
if (R[k].action == 1)
{//buy
if (R[k].price >= p0) sum1 += R[k].num;
}
else if (R[k].action == 2)
{//sell
if (R[k].price <= p0) sum2 += R[k].num;
}
}
}
count = min(sum1, sum2);
if (count > count_max || (count == count_max && p0 > p_max)) {
p_max = p0;
count_max = count;
}
}
else continue;
}
printf("%.2lf %lld", p_max, count_max);
return 0;
}
注意:输出的格式%lf %lld
将int 换成long long
float 换成double
201412-2-Z字形扫描
#include<stdio.h>
#include<iostream>
using namespace std;
const int N = 505;
int a[N][N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <=n; j++) {
cin >> a[i][j];
}
}
for (int i = 2; i <= 2 * n; i++) {
if (i % 2 == 0) {
for (int j = i - 1; j; j--) {//偶数项从下向上
if (j > 0 && j <= n && i - j > 0 && i - j <= n)
cout << a[j][i - j]<<" ";
}
}
else if (i % 2 == 1) {//奇数项从上向下
for (int j = 1;j<i; j++) {
if (j > 0 && j <= n && i - j > 0 && i - j <= n)
cout << a[j][i - j] << " ";
}
}
}
return 0;
}
解题关键横纵坐标相加的和
201604-3-路径解析
一看就不想做的类型
一个头两个大
#include<stdio.h>
#include<iostream>
#include<vector>
#include <cstring>
#include <algorithm>
#include<string>
//路径解析
using namespace std;
vector<string> get_cut(string& str) {
vector<string> re;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '/') continue;
int j = i + 1;
while (j < str.size() && str[j] != '/') j++;
re.push_back(str.substr(i, j - i));
i = j;//之前老是不过,忘记写了
}
return re;
}//例如将 /d2/d4/f1分割成 ‘d2’,‘d4’,‘f1’
void walk_more(vector<string> cur, vector<string> s2) {
for (auto s : s2)
{//auto自动变量的声明
if (s == ".")
continue;//本目录
else if (s == "..") {//返回上一级目录
if(cur.size())
cur.pop_back();
}
else
{
cur.push_back(s);
}
}
if (cur.empty())
{
puts("/");
return;
}
else {
for(auto s:cur)
cout << "/" << s;
cout << endl;
}
}
int main() {
int n;
string now;
cin >> n >> now;
vector<string> s1, s2,zero;
s1 = get_cut(now);//把当前目录分割
getchar();
while (n > 0) {
string temp;
getline(cin, temp);//添加头文件#include<string>
s2 = get_cut(temp);
if (temp.size() && temp[0] == '/')
{//绝对路径
walk_more(zero,s2);
}
else
{//相对路径
walk_more(s1,s2);
}
n--;
}
return 0;
}
- str.substr(i, j)可以从str下标为i的位置截取长度为j的字符串
- vector不定长数组,利用pop_back 和 push_back
- str.insert(i, “abc”)可以从str下标为i的位置插入字符串abc,只能插入字符串