USACO 1.2
主要说一下格式。
头部必打 /*
ID: beihai2013
PROG: (题目名字,题目有描述)
LANG: C++
*/
然后用文件流打开输入输出。
1.1.1
a+b
/*
ID: beihai2013
PROG: test
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream fout ("test.out");
ifstream fin ("test.in");
int a, b;
fin >> a >> b;
fout << a+b << endl;
return 0;
}
1.1.2
简单计算
/*
ID: beihai2013
PROG: ride
LANG: C++
*/
include
include
include
include
include
include
include
using namespace std;
const int MAXN = 1000;
char s1[MAXN], s2[MAXN];
int main()
{
// scanf(“%s %s”, s1, s2);
freopen(“ride.in”, “r”, stdin);
freopen(“ride.out”, “w”, stdout);
cin >> s1 >> s2;
int ans1 = 1, ans2 = 1;
for(int i = 0 ; i < (int)strlen(s1) ; i++)
ans1 = (ans1 * (s1[i] - ‘A’ + 1)) % 47;
for(int i = 0 ; i < (int)strlen(s2) ; i++)
ans2 = (ans2 * (s2[i] - ‘A’ + 1)) % 47;
if(ans1 == ans2) cout << “GO” << endl;
else cout << “STAY” << endl;
//scanf("%s", s1);
return 0;
}
1.1.5
简单模拟。所有人的钱总和为0,一开始所有人的钱数量也为0。每次一个人会分钱给另外几个人,不能均分的钱就自己留着。
/*
ID: beihai2013
PROG: gift1
LANG: C++
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 20;
char name[MAXN];
char p[MAXN][MAXN];
int val[MAXN];
map<string,int>mm;
int main()
{
freopen("gift1.in", "r", stdin);
freopen("gift1.out", "w", stdout);
int n;
scanf("%d", &n);
for(int i = 1 ; i <= n ; i++){
scanf("%s", p[i]);
mm[p[i]] = i;
val[i] = 0;
}
while(scanf("%s", name) != EOF){
int sum, m;
int u = mm[name];
scanf("%d%d", &sum, &m);
if(m == 0){
continue;
}
for(int i = 0 ; i < m ; i++){
scanf("%s", name);
int v = mm[name];
val[v] += sum / m;
}
val[u] -= sum - sum % m;
}
for(int i = 1 ; i <= n ; i++){
printf("%s %d\n", p[i], val[i]);
}
return 0;
}
1.1.6
愈来愈难了……
模拟时间,输出从1900年起,第N年的每个月第13日分别为星期6712345的数量
题目容易看错,注意给出的条件是1900.1.1为星期一,所以还要转化一下。
然后要把12月的天数放前面,因为遍历一年的时候是遍历这一年的1-12月,1月是从上一年的12月变化来的。
/*
ID: beihai2013
TASK: friday
LANG: C++
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 400 + 5;
int ans[MAXN][10];
int mon[20] = {0, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
int day(int year, int month)
{
if(month == 3){
if(year % 400 == 0) return 29;
else if(year % 100 != 0 && year % 4 == 0) return 29;
}
return mon[month];
}
void init()
{
memset(ans, 0, sizeof(ans));
int now = 5;
for(int i = 1 ; i < MAXN ; i++){
int year = 1900 + i - 1;
for(int j = 1 ; j <= 7 ; j++) ans[i][j] = ans[i - 1][j];
for(int j = 1 ; j <= 12 ; j++){
now = (now + day(year, j)) % 7;
if(now == 0) now = 7;
ans[i][now]++;
// printf("year = %d, month = %d, now = %d, day(year,j) = %d\n", year, j, now, day(year, j));
// system("pause");
}
}
}
int main()
{
freopen("friday.in", "r", stdin);
freopen("friday.out", "w", stdout);
init();
int n;
scanf("%d", &n);
for(int i = 1 ; i <= 7 ; i++){
printf("%d", ans[n][i]);
if(i == 7) printf("\n");
else printf(" ");
}
return 0;
}
1.1.7
神坑模拟。刚开始想用O(N)做然后就各种做不出来。
用O(N^2)做的,对每个点找能达到的最左端和最右端。但是还要分w和非w的情况讨论。
/*
ID: beihai2013
TASK: beads
LANG: C++
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 1e3;
char str[MAXN];
int l[MAXN], r[MAXN];
int main()
{
freopen("beads.in", "r", stdin);
freopen("beads.out", "w", stdout);
int n;
scanf("%d", &n);
scanf("%s", str);
for(int i = n ; i < 2 * n ; i++) str[i] = str[i - n];
str[n * 2] = '\0';
for(int i = 0 ; i < 2 * n ; i++){
if(str[i] != 'w'){
int now = i;
while(now >= 0 && (str[now] == 'w' || str[now] == str[i])) now--;
l[i] = i - now;
now = i;
while(now < 2 * n && (str[now] == 'w' || str[now] == str[i])) now++;
r[i] = now - i;
}
else{
int now = i;
while(now >= 0 && str[now] == 'w') now--;
if(now >= 0){
char temp = str[now];
while(now >= 0 && (str[now] == 'w' || str[now] == temp)) now--;
l[i] = i - now;
}
else l[i] = i - now;
now = i;
while(now < 2 * n && str[now] == 'w') now++;
if(now < 2 * n){
char temp = str[now];
while(now < 2 * n && (str[now] == 'w' || str[now] == temp)) now++;
r[i] = now - i;
}
else r[i] = -i + now;
}
}
int ans = 0;
for(int i = 0 ; i < 2 * n ; i++) ans = max(ans, l[i] + r[i + 1]);
ans = min(ans, n);
printf("%d\n", ans);
return 0;
}