+ code examples
This commit is contained in:
parent
5c51a53f63
commit
812c11a37f
2 changed files with 109 additions and 4 deletions
|
@ -14,15 +14,116 @@ Demonstrates how to read in input and print output for a USACO problem in multip
|
|||
|
||||
<!-- END DESCRIPTION -->
|
||||
|
||||
Let's begin by solving a few fun problems! The following problems require relatively little programming experience and no algorithmic knowledge.
|
||||
## [Fence Painting](http://usaco.org/index.php?page=viewproblem2&cpid=567)
|
||||
|
||||
Do as many as you want, then move on! You do not have to do all of them.
|
||||
(link to USACO website stuff?)
|
||||
|
||||
<!-- END DESCRIPTION -->
|
||||
(auto newlines)
|
||||
|
||||
### C++
|
||||
|
||||
#### Method 1
|
||||
|
||||
Use [freopen](http://www.cplusplus.com/reference/cstdio/freopen/). If you comment out both of the lines containing `freopen` then the program reads from standard in and writes to standard out as usual.
|
||||
|
||||
```cpp
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
freopen("paint.in","r",stdin); // reuse standard in to read from "paint.in"
|
||||
freopen("paint.out","w",stdout); // reuse standard out to write to "paint.out"
|
||||
vector<bool> cover(100);
|
||||
int a, b, c, d; cin >> a >> b >> c >> d;
|
||||
for (int i = a; i < b; ++i) cover[i] = 1;
|
||||
for (int i = c; i < d; ++i) cover[i] = 1;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < 100; ++i) ans += cover[i];
|
||||
cout << ans;
|
||||
}
|
||||
```
|
||||
|
||||
#### Method 2
|
||||
|
||||
Use [ifstream & ofstream](http://www.cplusplus.com/doc/tutorial/files/).
|
||||
|
||||
```cpp
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ifstream fin("paint.in");
|
||||
ofstream fout("paint.out");
|
||||
vector<bool> cover(100);
|
||||
int a, b, c, d; fin >> a >> b >> c >> d;
|
||||
for (int i = a; i < b; ++i) cover[i] = 1;
|
||||
for (int i = c; i < d; ++i) cover[i] = 1;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < 100; ++i) ans += cover[i];
|
||||
fout << ans;
|
||||
}
|
||||
```
|
||||
|
||||
### Java
|
||||
|
||||
(link?)
|
||||
|
||||
(Scanner?)
|
||||
|
||||
(FastScanner?)
|
||||
|
||||
Class name can be whatever you want.
|
||||
|
||||
```java
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
public class paintSol { must be declared in paintSol.java
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new FileReader("paint.in"));
|
||||
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("paint.out")));
|
||||
int[] cover = new int[100];
|
||||
|
||||
StringTokenizer st = new StringTokenizer(br.readLine());
|
||||
int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken());
|
||||
st = new StringTokenizer(br.readLine());
|
||||
int c = Integer.parseInt(st.nextToken()), d = Integer.parseInt(st.nextToken());
|
||||
|
||||
for (int i = a; i < b; ++i) cover[i] = 1;
|
||||
for (int i = c; i < d; ++i) cover[i] = 1;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < 100; ++i) ans += cover[i];
|
||||
pw.println(ans);
|
||||
pw.close();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Python 3
|
||||
|
||||
See [here](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files).
|
||||
|
||||
```py
|
||||
fin = open("paint.in","r")
|
||||
fout = open("paint.out","w")
|
||||
cover = [0 for i in range(100)]
|
||||
a,b = map(int,fin.readline().split())
|
||||
c,d = map(int,fin.readline().split())
|
||||
for i in range(a,b):
|
||||
cover[i] = 1
|
||||
for i in range(c,d):
|
||||
cover[i] = 1
|
||||
ans = 0
|
||||
for i in range(100):
|
||||
ans += cover[i]
|
||||
fout.write(str(ans))
|
||||
```
|
||||
|
||||
## Problems
|
||||
|
||||
Let's begin by solving a few problems! The following require relatively little programming experience and no algorithmic knowledge.
|
||||
|
||||
Do as many as you want, then move on! You do not have to do all of them.
|
||||
|
||||
- [Promotion Counting](http://usaco.org/index.php?page=viewproblem2&cpid=591)
|
||||
- [Word Processor](http://usaco.org/index.php?page=viewproblem2&cpid=987)
|
||||
- [Fence Painting](http://usaco.org/index.php?page=viewproblem2&cpid=567)
|
||||
- [Square Pasture](http://usaco.org/index.php?page=viewproblem2&cpid=663)
|
||||
|
|
|
@ -5,6 +5,10 @@ author: Benjamin Qi
|
|||
order: 2
|
||||
---
|
||||
|
||||
Contests that I participate in, as well as a few tools.
|
||||
|
||||
<!-- END DESCRIPTION -->
|
||||
|
||||
See [clist.by](https://clist.by/coder/bqi343/) for an extensive list. Most of the contests which I do are a subset of those shown in "Programming Contests.png". A link to my google calendar for contests is available [here.](https://calendar.google.com/calendar?cid=Y2s5ZjdmZDBkNjdmOGFxZ2oxbDVrMHJ1OGtAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ) See the other page for olympiads such as USACO.
|
||||
|
||||
Make sure to [upsolve](https://en.wiktionary.org/wiki/upsolve) after the contest ends! I particularly enjoy problems from AtCoder and CSAcademy. :)
|
||||
|
|
Reference in a new issue