├── run.sh ├── .gitignore ├── 1000.c ├── README.md ├── 1004.c ├── 2140.c ├── 1401.c ├── 1799.c ├── 1003.c ├── 1806.c ├── 1799.cpp ├── 2136.c ├── 2309.cpp ├── 1401.cpp ├── 1922.cpp ├── 1504.c ├── 2262.cpp ├── 1007.c ├── 3251.cpp └── 1001.cpp /run.sh: -------------------------------------------------------------------------------- 1 | gcc $1 && cat sample.input | ./a.out 2 | exit 0 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.out 3 | *.in 4 | *.input 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /1000.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | scanf("%d%d", &a, &b); 7 | printf("%d\n", a + b); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Solutions for Stanford programming course [CS97SI](http://web.stanford.edu/class/cs97si/). The exercise problems for this course are from [Peking Online Judge] (http://poj.org/) 2 | -------------------------------------------------------------------------------- /1004.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float sum=0.00, add; 6 | int i; 7 | for (i=0; i<12; i++) { 8 | scanf("%f", &add); 9 | sum += add; 10 | } 11 | printf("$%.2f\n", sum/12); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /2140.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int i,j,n,ways=0; 6 | long int remain; 7 | scanf("%u", &n); 8 | 9 | for (i=n; i!=0; --i) { 10 | remain = n; 11 | for (j=i; remain>0; --j) 12 | remain -= j; 13 | if (remain == 0) 14 | ++ways; 15 | } 16 | 17 | printf("%u\n", ways); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1401.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int count_factors(int, int); 4 | 5 | int main() 6 | { 7 | int n,i,k; 8 | scanf("%d", &n); 9 | for (i=0; i 2 | #include 3 | 4 | float compute_caliber(float, int); 5 | 6 | int main() 7 | { 8 | int n, i, rounds; 9 | float cylinder; 10 | scanf("%d", &n); 11 | for (i=0; i 2 | 3 | double get_next(void); 4 | int run_sum(double); 5 | 6 | int main() 7 | { 8 | double lim; 9 | int n; 10 | while (lim = get_next()) { 11 | n = run_sum(lim); 12 | printf("%d card(s)\n", n); 13 | } 14 | return 0; 15 | } 16 | 17 | int run_sum(double lim) { 18 | int n=1; 19 | double sum=0.0; 20 | for (n=1; ; n++) { 21 | sum += 1.0/(n+1); 22 | if (sum > lim) 23 | break; 24 | } 25 | return n; 26 | } 27 | 28 | double get_next() 29 | { 30 | double next; 31 | scanf("%lf", &next); 32 | return next; 33 | } 34 | -------------------------------------------------------------------------------- /1806.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define abs(x) ((x) >= 0 ? (x) : -1*(x)) 3 | 4 | void print_slices(int); 5 | 6 | int main() 7 | { 8 | int i,n,dim; 9 | scanf("%d", &n); 10 | for (i=0; i dim) printf("."); 27 | else printf("%d", dist); 28 | } printf("\n"); 29 | } 30 | } printf("\n"); 31 | } 32 | -------------------------------------------------------------------------------- /1799.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using std::cout; 13 | using std::cin; 14 | using std::string; 15 | using std::endl; 16 | using std::vector; 17 | 18 | #define PI 3.14159265 19 | 20 | int main(int argc, char *argv[]){ 21 | int N; 22 | cin >> N; 23 | for (int i = 0; i < N; i++){ 24 | float R; 25 | int n; 26 | cin >> R >> n; 27 | cout << "Scenario #" << i+1 << ":" << endl; 28 | //cout << R * sin(PI / n) / (1 + sin(PI / n)) << std::setprecision(3) << endl; 29 | printf("%.3f\n", R * sin(PI / n) / (1 + sin(PI / n))); 30 | cout << endl; 31 | } 32 | } -------------------------------------------------------------------------------- /2136.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int count_characters(int *counts) 5 | { 6 | char ch, depth=0; 7 | while ((ch = getchar()) != EOF) { 8 | if (isupper(ch)) { 9 | counts[ch-'A'] += 1; 10 | if (counts[ch-'A'] > depth) 11 | depth = counts[ch-'A']; 12 | } 13 | } 14 | return depth; 15 | } 16 | 17 | void print_histogram(int *counts, int depth) 18 | { 19 | char i,j,c; 20 | for (i=depth; i>0; i--) { 21 | for (j=0; j<26; ++j) { 22 | if (counts[j] >= i) printf("*"); 23 | else printf(" "); 24 | if (j != 25) printf(" "); 25 | } 26 | printf("\n"); 27 | } 28 | for (i=0; i<26; ++i) { 29 | printf("%c", i + 'A'); 30 | if (i != 25) printf(" "); 31 | } 32 | printf("\n"); 33 | } 34 | 35 | int main() 36 | { 37 | int counts[26] = { 0 }; 38 | int depth = count_characters(counts); 39 | print_histogram(counts, depth); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /2309.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::cout; 14 | using std::cin; 15 | using std::string; 16 | using std::endl; 17 | using std::vector; 18 | 19 | #define PI 3.14159265 20 | 21 | void solve() 22 | { 23 | long n; 24 | cin >> n; 25 | int height = 0; 26 | long n_two_prime_factorials = 2; 27 | while (!(n % n_two_prime_factorials)){ 28 | n_two_prime_factorials *= 2; 29 | height++; 30 | } 31 | printf("%ld %ld\n", (n - (long)(pow(2, height) - 1)), (n + (long)(pow(2, height) - 1))); 32 | //cout << n - (pow(2, height) - 1) << " " << n + (pow(2, height) - 1) << endl; 33 | } 34 | 35 | int main(int argc, char *argv[]){ 36 | long tc; 37 | cin >> tc; 38 | for (long i = 0; i < tc; i++) 39 | { 40 | solve(); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /1401.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::cout; 14 | using std::cin; 15 | using std::string; 16 | using std::endl; 17 | using std::vector; 18 | 19 | #define PI 3.14159265 20 | 21 | void format_output(int i,string answer) 22 | { 23 | cout << answer << endl; 24 | } 25 | 26 | string solve() 27 | { 28 | long N; 29 | cin >> N; 30 | long factor_5 = 5; 31 | long zeros = 0; 32 | while (factor_5 <= N){ 33 | zeros += N / factor_5; 34 | factor_5 *= 5; 35 | } 36 | std::ostringstream convert_stream; 37 | convert_stream << zeros; 38 | return convert_stream.str(); 39 | } 40 | 41 | void format_output(int i,string answer); 42 | string solve(); 43 | int main(int argc, char *argv[]){ 44 | long tc; 45 | cin >> tc; 46 | for (long i = 0; i < tc; i++) 47 | { 48 | format_output(i,solve()); 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /1922.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::string; 12 | using std::endl; 13 | using std::vector; 14 | //Our intent to to know the fastest guy because charley will finish along with the fastest guy. 15 | int main(int argc, char *argv[]){ 16 | cout.setf(std::ios::unitbuf); 17 | while (true){ 18 | int n; 19 | cin >> n; 20 | if (n == 0) 21 | return EXIT_SUCCESS; 22 | int rider_speed,setoff_time; 23 | float rider_time, best_time = INT_MAX; 24 | for (int i = 0; i < n; i++){ 25 | scanf("%d %d", &rider_speed, &setoff_time); 26 | if (setoff_time >= 0){ //we don't need the guys who are ahead of us because they have either raced or we will eventually race them 27 | rider_time = (4.5 * 3600 / rider_speed)+ setoff_time; 28 | best_time = std::min(rider_time, best_time); 29 | } 30 | } 31 | cout << (int)ceil(best_time) < 2 | #include 3 | #include 4 | 5 | #define BUFF_SIZE 50 6 | 7 | void str_reverse(char *); 8 | void setup(void); 9 | void teardown(void); 10 | 11 | char *str_a, *str_b, *sum; 12 | 13 | int main() 14 | { 15 | int i,n; 16 | scanf("%i", &n); 17 | 18 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::cout; 14 | using std::cin; 15 | using std::string; 16 | using std::endl; 17 | using std::vector; 18 | 19 | #define PI 3.14159265 20 | #define INF 1000003 21 | 22 | long not_primes[INF] = { 0 }; 23 | 24 | void generate_primes() 25 | { 26 | int k; 27 | for (long i = 2; i <= (double)sqrt(INF-1); i++){ 28 | if (!not_primes[i]){ 29 | k = 1; 30 | for (long j = i*i; j <= INF-1; j = i*i + i*k){ 31 | not_primes[j] = 1; 32 | k++; 33 | } 34 | } 35 | } 36 | } 37 | 38 | void solve(long n) 39 | { 40 | for (long i = 3; i <= n / 2 + 1; i += 2) 41 | { 42 | if (!not_primes[i]){ 43 | if (!not_primes[n - i]){ 44 | printf("%ld = %ld + %ld\n", n, i, n - i); 45 | return; 46 | } 47 | } 48 | } 49 | cout << "Goldbach's conjecture is wrong." << endl; 50 | } 51 | 52 | 53 | int main(int argc, char *argv[]){ 54 | generate_primes(); 55 | long n; 56 | while (scanf("%ld",&n) && n!=0) 57 | { 58 | solve(n); 59 | } 60 | } -------------------------------------------------------------------------------- /1007.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define BUFFSIZE 100 5 | 6 | typedef struct { 7 | char sequence[BUFFSIZE]; 8 | int inversions; 9 | } Seqinv; 10 | 11 | int count_inversions(char *); 12 | 13 | int main() 14 | { 15 | int i,hole,l,n; 16 | scanf("%d%d", &l, &n); 17 | 18 | char astring[l]; 19 | Seqinv **ptr = malloc(n * sizeof(Seqinv *)); 20 | 21 | hole = 0; 22 | for (i=0; isequence, astring); 27 | current->inversions = count_inversions(astring); 28 | 29 | for (hole=0; holeinversions >= ptr[hole]->inversions) 31 | continue; 32 | else { 33 | Seqinv *temp = ptr[hole]; 34 | ptr[hole] = current; 35 | current = temp; 36 | } 37 | } 38 | ptr[hole] = current; 39 | } 40 | 41 | for (i=0; isequence); 43 | free(ptr[i]); 44 | } 45 | 46 | free(ptr); 47 | return 0; 48 | } 49 | 50 | int count_inversions(char *astring) 51 | { 52 | int i, j, n=0; 53 | for (i=0; ;i++) { 54 | if (!astring[i]) break; 55 | for (j=i+1; ; j++) { 56 | if (!astring[j]) break; 57 | if (astring[i] > astring[j]) n++; 58 | } 59 | } 60 | return n; 61 | } 62 | -------------------------------------------------------------------------------- /3251.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::string; 12 | using std::endl; 13 | using std::vector; 14 | 15 | int main(int argc, char *argv[]){ 16 | int N; 17 | cin >> N; 18 | char farm[100][100]; 19 | for (int i = 0; i < N; i++){ 20 | for (int j = 0; j < N; j++){ 21 | cin >> farm[i][j]; 22 | } 23 | } 24 | 25 | int best_area = 0; 26 | int Js; 27 | for (int i = 0; i < N; i++){ 28 | for (int j = 0; j < N; j++){ 29 | if (farm[i][j] != 'B'){ 30 | for (int a = 1; a < std::min(N - j, N - i); a++){ 31 | for (int b = 0; b < std::min(i + 1, N - j); b++){ 32 | if ((i + a - b < 0) || (j + b + a > N - 1)) 33 | continue; 34 | Js = 0; 35 | Js += (farm[i][j] == 'J') ? 1 : 0; 36 | if (farm[i - b][j + a] == 'B') continue; 37 | Js += (farm[i - b][j + a] == 'J') ? 1 : 0; 38 | if (farm[i + a][j + b] == 'B') continue; 39 | Js += (farm[i + a][j + b] == 'J') ? 1 : 0; 40 | if (farm[i + a - b][j + a + b] == 'B') continue; 41 | Js += (farm[i + a - b][j + a + b] == 'J') ? 1 : 0; 42 | if (Js >= 3 && a*a + b*b > best_area){ 43 | best_area = a*a + b*b; 44 | } 45 | } 46 | } 47 | } 48 | } 49 | } 50 | cout << best_area; 51 | } -------------------------------------------------------------------------------- /1001.cpp: -------------------------------------------------------------------------------- 1 | //Exponentiation problem 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using std::cout; 13 | using std::cin; 14 | using std::string; 15 | using std::vector; 16 | using std::endl; 17 | 18 | #define MAX_LEN 6 19 | int main (int argc, char *argv[]) 20 | { 21 | cout.setf(std::ios::unitbuf); 22 | char r[MAX_LEN]; 23 | int n=0; 24 | while(scanf("%s %d",r,&n) != EOF){ 25 | vector number_without_decimal; 26 | int dot_pos=-1; 27 | for(int i=MAX_LEN-1;i>=0;i--){ 28 | char c=r[i]; 29 | if(c == '.'){ 30 | dot_pos = MAX_LEN-(i+1); 31 | continue; 32 | } 33 | number_without_decimal.push_back(atoi(&c)); 34 | } 35 | dot_pos *= n; 36 | 37 | vector result(number_without_decimal.size()); 38 | //copy the first element in the result 39 | std::copy(number_without_decimal.begin(),number_without_decimal.end(),result.begin()); 40 | while(n-1>0){ 41 | vector product_result(result.size()+number_without_decimal.size()+1,0); 42 | int carry; 43 | int val; 44 | for(vector::size_type j=0;j::size_type i=0; 48 | for(;i0) 61 | result.insert(result.begin()+dot_pos,'.'); 62 | //remove trailing zeroes 63 | int trailing_zeroes_to_erase=0; 64 | for(int i=0;i= dot_pos){ 66 | break; 67 | } 68 | trailing_zeroes_to_erase++; 69 | } 70 | result.erase(result.begin(),result.begin()+trailing_zeroes_to_erase); 71 | //remove trailing dot 72 | if(result[0]=='.') 73 | result.erase(result.begin()); 74 | //remove leading zeroes and print 75 | bool leading_zeros=true; 76 | for(int i=result.size()-1;i>=0;i--){ 77 | if(leading_zeros){ 78 | if(result[i] == 0){ 79 | continue; 80 | } 81 | else{ 82 | leading_zeros=false; 83 | } 84 | } 85 | if(result[i] == '.'){ 86 | printf("%c",result[i]); 87 | continue; 88 | } 89 | printf("%d",result[i]); 90 | } 91 | cout<