Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

茹靖涵 level 1 已完成 #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions p01/p01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
int main() {
HANDLE hOut;
CONSOLE_SCREEN_BUFFER_INFO a;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &a);
int length_of_console = a.dwSize.X;
int flag = 0; // symbolize whether 'a' has reached the two sides
while(1){
for(int i=0;i<length_of_console;i++){
for (int j=0;j<i;j++)cout << " ";
cout << "A";
Sleep(30);
if (i == length_of_console - 1) flag = 1; // so that 'a' could turn left
if (i == 0) flag = 0;
if (flag) i -= 2;
system("cls");
}
}
system("pause");
return 0;
}

17 changes: 17 additions & 0 deletions p02/p02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include<cstdio>
using namespace std;
bool isPrime(int x){ // simple version
if (x == 1) return false;
for(int i = 2; i * i <= x; i++) if (x % i == 0) return false;
return true;
}
int main() {
int n;
cin >> n;
bool ans = isPrime(n);
Copy link
Owner

@luckymark luckymark Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实ans这个中间变量可以去掉,读起来反而更自然

if (ans) printf("%d is a prime.\n",n);
else printf("%d is not a prime.\n",n);
system("pause");
return 0;
}
14 changes: 14 additions & 0 deletions p03_1/p03-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
using namespace std;
const int lifetime = 84; // His lifetime could be divided by 12 and 7,so by common sense,his lifetime is 84.
const int childhood = 14;
const int youth = 7;
const int bachelor = 12;
const int marriage = 5;
int main() {
int father = childhood + youth + bachelor + marriage;
int son = 0;
int time = lifetime - 4 - father;
if (time == lifetime / 2) cout << 80 << endl;
return 0;
}
22 changes: 22 additions & 0 deletions p03_2/p03-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
using namespace std;
const int max_lifetime = 800; // the life span of the legendary "PengZu"
int main() {
for(int i=0;i<max_lifetime;i++){ // i is the lifetime of Diophantus
if(i % 6 != 0 || i % 12 != 0 || i%7 != 0) continue;
int childhood = i / 6;
int youth = i / 12;
int bachelor = i / 7;
int marriage = 5;
int father = childhood + youth + bachelor + marriage;
int son = 0;
for(int j = 1;j <= i-father;j++){
if(son == i/2 && father + son + 4 == i) {
cout << i << endl;
break;
} else son++;
}
}
system("pause");
return 0;
}
17 changes: 17 additions & 0 deletions p04/p04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;
int digit[5]; // record every digit of the number
int main() {
for(int i = 100;i < 1000;i++){
int tmp = i;
for(int j = 1;j <= 3;j++){
digit[j] = tmp % 10;
tmp /= 10;
}
int i0 = digit[1] * digit[1] * digit[1];
int i1 = digit[2] * digit[2] * digit[2];
int i2 = digit[3] * digit[3] * digit[3];
if(i == i0 + i1 + i2) cout << i << endl;
}
return 0;
}
20 changes: 20 additions & 0 deletions p05_1/p05_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <time.h>
using namespace std;
int isPrime(int n){ // ordinary algorithm
if(n == 1) return 0;
for (int i = 2;i * i <= n;i++){
if (n % i == 0) return 0;
}
return 1;
}
int main() {
int begintime,endtime;
begintime = clock();
for(int i = 2;i <= 1000;i++){
if(isPrime(i)) cout << i << ' ';
}
endtime = clock();
cout << "Runtime is " << endtime - begintime << "ms" << endl;
return 0;
}
15 changes: 15 additions & 0 deletions p05_2/p05_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
#include <time.h>
int main() {
int begintime,endtime;
begintime = clock();
printf("2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 1\n"
"63 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 3\n"
"37 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 5\n"
"21 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 7\n"
"19 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 9\n"
"29 937 941 947 953 967 971 977 983 991 997"); // Apparently this is the most efficient algorithm
endtime = clock();
printf("Runtime is %dms",endtime - begintime);
return 0;
}
18 changes: 18 additions & 0 deletions p05_3/p05_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <string.h>
#include <time.h>
using namespace std;
int main() { // Use"ShaiFa"
int begintime,endtime;
begintime = clock();
int f[1005]; // identify whether the number is a prime
memset(f,1,sizeof f);
for(int i = 2;i < 1000;i++){
if(f[i])
for(int j = 2; j * i <= 1000; j++) f[i*j] = 0;
}
for(int i = 2;i < 1000;i++) if (f[i]) cout << i << ' ';
endtime = clock();
cout << "Runtime is " << endtime - begintime << "ms" << endl;
return 0;
}
26 changes: 26 additions & 0 deletions p06/p06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <stdio.h>
using namespace std;
int isPrime(int x){
for (int i = 2; i * i <= x; i++){
if (x % i == 0) return 0;
}
return 1;
}
int main() {
int prime[30];
int cnt = 0;
for (int n = 2; n < 100; n++){
if(isPrime(n)) prime[++cnt] = n;
}
for(int x = 4 ; x <= 100; x += 2){
for(int i = 1; i <= cnt; i++){
if (isPrime(x - prime[i])){ // could use binary search in prime[] to optimize
printf("%d = %d + %d\n",x,prime[i],x-prime[i]);
break;
}
}
}
system("pause");
return 0;
}
27 changes: 27 additions & 0 deletions p07/p07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <string>
using namespace std;
string a;
char b[1000005];
void encrypt(string s) {
for (int i = 0; i < s.length(); i++){
b[i] = s[i] + 3;
cout << b[i];
}
cout << endl;
}
void decrept(){
for (int i = 0; i < a.length(); i++){
char tmp = b[i] - 3;
cout << tmp;
}
cout << endl;
}
int main() {
cin >> a;
encrypt(a);
decrept();
system("pause");
return 0;
}

73 changes: 73 additions & 0 deletions p08/p08.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int n;
stack<int> a,b,c;

void output(int n, int x, char a, char b){
printf("%d : %d from %c to %c\n",n,x,a,b);
}

char get_top(char x){
int tmp;
if(x == 'a'){
tmp = a.top();
} else if(x == 'b'){
tmp = b.top();
} else{
tmp = c.top();
}
return tmp;
}

void reverse(char x, char y){
int tmp;
if(x == 'a' && y == 'b'){
tmp = a.top();
a.pop();
b.push(tmp);
}else if(x == 'a' && y == 'c'){
tmp = a.top();
a.pop();
c.push(tmp);
}else if(x == 'b' && y == 'a'){
tmp = b.top();
b.pop();
a.push(tmp);
}else if(x == 'b' && y == 'c'){
tmp = b.top();
b.pop();
c.push(tmp);
}else if(x == 'c' && y == 'a'){
tmp = c.top();
c.pop();
a.push(tmp);
}else if(x == 'c' && y == 'b'){
tmp = c.top();
c.pop();
b.push(tmp);
}
}

void solve(int n, char x, char y, char z){
if(n == 1){
output(n,get_top(x),x,z);
reverse(x,z);
return;
}
solve(n-1, x,z,y);
output(n,get_top(x),x,z);
reverse(x,z);
solve(n-1,y,x,z);
}
int main(){
char x = 'a',y = 'b',z = 'c';
cin >> n;
for(int i = n; i >= 1; i --){
a.push(i);
}
solve(n,x,y,z);//
system("pause");
return 0;
}
Loading