Skip to content

Commit 8106f78

Browse files
author
李治国
authored
Merge pull request #25 from zguolee/LiZhiguo
更新6.1 -6.3
2 parents cc63fea + 255911e commit 8106f78

File tree

7 files changed

+73
-78
lines changed

7 files changed

+73
-78
lines changed
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
#include<iostream>
22
#include<string>
3-
#include<vector>
4-
3+
#include<stack>
54
using namespace std;
65

7-
//×Ö·û´®³ý·¨
8-
string divide(string str,int x) {
9-
int remainder=0;//±£ÁôÓàÊý
6+
string Divide(string str,int x) {
7+
int remainder=0;
108
for(int i=0; i<str.size(); i++) {
11-
int current=str[i]-'0'+remainder*10;
9+
int current=remainder*10+str[i]-'0';
1210
str[i]=current/x+'0';
1311
remainder=current%x;
1412
}
15-
int index=0;
16-
while(str[index]=='0')index++;
17-
return str.substr(index);
13+
int pos=0;
14+
while(str[pos]=='0') {
15+
pos++;
16+
}
17+
return str.substr(pos);
1818
}
1919

2020
int main() {
2121
string str;
22-
vector<int> binary;
23-
int last;
2422
while(getline(cin,str)) {
23+
stack<int> binary;
2524
while(str.size()!=0) {
26-
last=str[str.size()-1]-'0';
27-
binary.push_back(last%2);
28-
str=divide(str,2);
25+
int last=str[str.size()-1]-'0';
26+
binary.push(last%2);
27+
str=Divide(str,2);
2928
}
30-
for(int i=binary.size()-1; i>=0; i--) {
31-
cout<<binary[i];
29+
while(!binary.empty()) {
30+
printf("%d",binary.top());
31+
binary.pop();
3232
}
33-
cout<<endl;
33+
printf("\n");
3434
}
35-
3635
return 0;
3736
}

第6章数学问题/6.1进制转换/6-3十进制与二进制.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ using namespace std;
88
**/
99

1010
//字符串除法
11-
string divide(string str,int x) {
12-
int remainder=0;//保留余数
11+
string Divide(string str,int x) {
12+
int remainder=0;
1313
for(int i=0; i<str.size(); i++) {
14-
int current=str[i]-'0'+remainder*10;
14+
int current=remainder*10+str[i]-'0';
1515
str[i]=current/x+'0';
1616
remainder=current%x;
1717
}
18-
int index=0;
19-
while(str[index]=='0')index++;
20-
return str.substr(index);
18+
int pos=0;
19+
while(str[pos]=='0') {
20+
pos++;
21+
}
22+
return str.substr(pos);
2123
}
22-
2324
//字符串乘法
24-
string mutiple(string str,int x) {
25-
int carry=0;//保存进位
25+
string Multiple(string str, int x) {
26+
int carry=0;
2627
for(int i=str.size()-1; i>=0; i--) {
27-
int current=x*(str[i]-'0')+carry;
28+
int current=(str[i]-'0')*x+carry;
2829
str[i]=current%10+'0';
2930
carry=current/10;
3031
}
@@ -33,9 +34,8 @@ string mutiple(string str,int x) {
3334
}
3435
return str;
3536
}
36-
3737
//字符串加法
38-
string add(string str,int x) {
38+
string Add(string str, int x) {
3939
int carry=x;
4040
for(int i=str.size()-1; i>=0; i--) {
4141
int current=(str[i]-'0')+carry;
@@ -53,20 +53,16 @@ int main() {
5353
while(getline(cin,str)) {
5454
vector<int> binary;
5555
while(str.size()!=0) {
56-
//最低位计算
5756
int last=str[str.size()-1]-'0';
58-
//取模运算
5957
binary.push_back(last%2);
60-
//整除运算
61-
str=divide(str,2);
58+
str=Divide(str,2);
6259
}
6360
string answer="0";
6461
for(int i=0; i<binary.size(); i++) {
65-
answer=mutiple(answer,2);//乘法运算
66-
answer=add(answer,binary[i]);//加法运算
62+
answer=Multiple(answer,2);
63+
answer=Add(answer,binary[i]);
6764
}
6865
cout<<answer<<endl;
6966
}
70-
7167
return 0;
7268
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include<iostream>
2-
32
using namespace std;
4-
53
int GCD(int a,int b) {
64
if(b==0) {
75
return a;
@@ -11,8 +9,8 @@ int GCD(int a,int b) {
119
}
1210
int main() {
1311
int a,b;
14-
while(~(scanf("%d%d",&a,&b))) {
15-
cout<<GCD(a,b)<<endl;
12+
while(scanf("%d%d",&a,&b)!=EOF) {
13+
printf("%d\n",GCD(a,b));
1614
}
1715
return 0;
1816
}

第6章数学问题/6.2最大公约数与最小公倍数/6-6最小公倍数.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ int GCD(int a,int b) {
1111
}
1212
int main() {
1313
int a,b;
14-
while(~(scanf("%d%d",&a,&b))) {
15-
cout<<(a*b)/GCD(a,b)<<endl;
14+
while(scanf("%d%d",&a,&b)!=EOF) {
15+
printf("%d\n",(a*b)/GCD(a,b));
1616
}
1717
return 0;
1818
}

第6章数学问题/6.3质数/6-7素数判定.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33

44
using namespace std;
55

6-
bool judge(int x){
7-
if(x<2){
6+
bool judge(int x) {
7+
if(x<2) {
88
return false;
9-
}
10-
for(int i=2;i<=sqrt(x);i++){
11-
if(x%i==0){
12-
return false;
9+
} else {
10+
for(int i=2; i<=sqrt(x); i++) {
11+
if(x%i==0) {
12+
return false;
13+
}
1314
}
15+
return true;
1416
}
15-
return true;
1617
}
1718

18-
int main(){
19+
int main() {
1920
int n;
20-
while(~scanf("%d",&n)){
21-
if(judge(n)){
21+
while(scanf("%d",&n)!=EOF) {
22+
if(judge(n)) {
2223
cout<<"yes"<<endl;
23-
}else{
24+
} else {
2425
cout<<"no"<<endl;
2526
}
2627
}

第6章数学问题/6.3质数/6-8素数.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,37 @@ using namespace std;
99
*/
1010

1111
const int MAXN=10001;
12-
//零初始化器,全部初始化为false
1312
bool isPrime[MAXN];
14-
vector<int> Prime;
13+
vector<int> prime;
1514

16-
void initial() {
17-
fill_n(isPrime,MAXN,true);
15+
void Init() {
16+
for(int i=0; i<MAXN; i++) {
17+
isPrime[i]=true;
18+
}
1819
isPrime[0]=false;
1920
isPrime[1]=false;
20-
for(int i=2; i<MAXN; ++i) {
21+
for(int i=2; i<MAXN; i++) {
2122
if(!isPrime[i])continue;
22-
Prime.push_back(i);
23+
prime.push_back(i);
2324
for(int j=i*i; j<MAXN; j+=i) {
2425
isPrime[j]=false;
2526
}
2627
}
27-
return ;
2828
}
2929

3030
int main() {
31-
initial();
31+
Init();
3232
int n;
33-
while(~scanf("%d",&n)) {
34-
int count=0;
35-
for(int i=0; i<Prime.size()&&Prime[i]<n; i++) {
36-
if(Prime[i]%10==1) {
37-
count++;
38-
cout<<Prime[i]<<" ";
33+
while(scanf("%d",&n)!=EOF) {
34+
bool out=false;
35+
for(int i=0; i<prime.size()&&prime[i]<n; i++) {
36+
if(prime[i]%10==1) {
37+
out=true;
38+
printf("%d ",prime[i]);
3939
}
4040
}
41-
if(!count)cout<<"-1"<<endl;
41+
if(!out)printf("-1");
42+
printf("\n");
4243
}
4344
return 0;
4445
}

第6章数学问题/6.3质数/test6-6Prime Number.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ using namespace std;
1111
const int MAXN=100000;
1212
const int MAXK=10000;
1313
bool isPrime[MAXN];
14-
vector<int> Prime;
14+
vector<int> prime;
1515

16-
void init() {
17-
fill_n(isPrime,MAXN,true);
16+
void Init() {
17+
for(int i=0; i<MAXN; i++) {
18+
isPrime[i]=true;
19+
}
1820
isPrime[0]=false;
1921
isPrime[1]=false;
2022
for(int i=2; i<MAXN; i++) {
2123
if(!isPrime[i])continue;
22-
Prime.push_back(i);
24+
prime.push_back(i);
2325
for(int j=i*i; j<MAXN; j+=i) {
2426
isPrime[j]=false;
2527
}
26-
if(Prime.size()==MAXK)break;
28+
if(prime.size()==MAXK)break;
2729
}
28-
return ;
2930
}
30-
3131
int main() {
32-
init();
32+
Init();
3333
int n;
3434
while(scanf("%d",&n)!=EOF) {
35-
cout<<Prime[n-1]<<endl;
35+
cout<<prime[n-1]<<endl;
3636
}
3737
return 0;
3838
}

0 commit comments

Comments
 (0)