--------------------------------------------------------------------------------
链接:https://pan.baidu.com/s/1LL4c5Uv2Kyj6H53q9xECjA
提取码:i4x5
--------------------------------------------------------------------------------
基础编程题目集(编程题1-38)
--------------------------------------------------------------------------------
OJ题解系列 目录导航帖
--------------------------------------------------------------------------------
这里是基础编程题目集(编程题1-38)
该部分考察较为基础,基本算法主要包括模拟、数论、二分、排序等,深入一些的算法有约瑟夫环、DFS、高精等
本章可能涉及到的模板有:
003 高精度模板
006 分数类模板
具体模板详见数据结构专题系列
接下来就是题解部分了,每道算法题都标注有对应的算法标签,对于那些易错、较难或是测试点比较特殊的题目会着重标注,本章推荐的题目有:
7-16 求符合给定条件的整数集 (15 分) | 枚举 |
---|---|
7-18 二分法求多项式单根 (20 分) | 二分答案 |
7-28 猴子选大王 (20 分) | 约瑟夫环 |
7-33 有理数加法 (15 分) | 数论 + 分数 |
7-37 整数分解为若干项之和 (20 分) | DFS |
7-38 数列求和-加强版 (20分) | 数论 + 高精 |
--------------------------------------------------------------------------------
算法标签: 模拟
#include
using namespace std;
int main(){
int n;
cin >> n;
double f = n*1.0;
int x1 = int(f/30.48);
int x2 = int((f-x1*30.48)/(30.48/12.00));
cout << x1 << " " << x2 << endl;
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int time,n;
cin >> time >> n;
if(n<0){
n = n % 1440;
n = (n+1440) % 1440;
}
int h = time / 100;
int m = time % 100;
int m1 = n % 60;
m += m1;
if(m>=60){
h++;
m -= 60;
}
int h1 = n/60;
h += h1;
if(h>=24){
h -= 24;
}
if(m<10){
cout << h << "0" << m << endl;
}else{
cout << h << m << endl;
}
return 0;
}
算法标签: 数论
#include
using namespace std;
int main(){
int n1;
cin >> n1;
int n2 = 0;
while(n1){
n2*=10;
n2 += n1%10;
n1/=10;
}
cout << n2 << endl;
return 0;
}
算法标签: 进制
#include
using namespace std;
int main(){
int n;
cin >> n;
if(n<=9){
cout << n << endl;
}else{
cout << n/16 << n%16 << endl;
}
return 0;
}
算法标签: 字符串
#include
using namespace std;
int main(){
cout << "------------------------------------" << endl;
cout << "Province Area(km2) Pop.(10K)" << endl;
cout << "------------------------------------" << endl;
cout << "Anhui 139600.00 6461.00" << endl;
cout << "Beijing 16410.54 1180.70" << endl;
cout << "Chongqing 82400.00 3144.23" << endl;
cout << "Shanghai 6340.50 1360.26" << endl;
cout << "Zhejiang 101800.00 4894.00" << endl;
cout << "------------------------------------" << endl;
return 0;
}
算法标签: 输入/输出
#include
using namespace std;
int main(){
double f1,f2;
int d;
char c;
scanf("%lf %d %c %lf",&f1,&d,&c,&f2);
printf("%c %d %.2lf %.2lf\n",c,d,f1,f2);
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int h,m;
scanf("%d:%d",&h,&m);
if(h==12){
printf("%d:%d PM\n",h,m);
}else if(h>12){
h-=12;
printf("%d:%d PM\n",h,m);
}else{
printf("%d:%d AM\n",h,m);
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int n;
cin >> n;
if(n>60){
printf("Speed: %d - Speeding\n",n);
}else{
printf("Speed: %d - OK\n",n);
}
return 0;
}
算法标签: 排序
#include
using namespace std;
int main(){
int a,b,c;
cin >> a >> b >> c;
if(a==b){
cout << "C";
}else if(b==c){
cout << "A";
}else{
cout << "B";
}
return 0;
}
算法标签: 分支
#include
using namespace std;
int main(){
int y,h;
int cnt;
cin >> y >> h;
if(y>=5){
cnt = 50;
}else{
cnt = 30;
}
int ans = 0;
if(h<=40){
ans += cnt*h;
}else{
ans += cnt*40;
ans += (h-40)*cnt*1.5;
}
printf("%d.00\n",ans);
return 0;
}
算法标签: 分支
#include
using namespace std;
int main(){
double x;
cin >> x;
double y;
if(x<=15){
y = 4.0*x/3.0;
}else{
y = 2.5*x-17.5;
}
printf("%.2lf\n",y);
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int x,y;
char op;
scanf("%d %c %d",&x,&op,&y);
if(op=='+'){
cout << x+y << endl;
}else if(op=='-'){
cout << x-y << endl;
}else if(op=='*'){
cout << x*y << endl;
}else if(op=='/'){
cout << x/y << endl;
}else if(op=='%'){
cout << x%y << endl;
}else{
cout << "ERROR" << endl;
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
double Open,High,Low,Close;
cin >> Open >> High >> Low >> Close;
string s = "";
bool f1=0,f2=0;
if(Open<Close){
s+="R-Hollow";
}else if(fabs(Open-Close)<1e-3){
s+="R-Cross";
}else{
s+="BW-Solid";
}
if(High>Close && fabs(High-Close)>1e-3 && High>Open && fabs(High-Open)>1e-3){
f1 = 1;
}
if(Low<Close && fabs(Low-Close)>1e-3 && Low<Open && fabs(Low-Open)>1e-3){
f2 = 1;
}
if(f1 && f2){
s += " with Lower Shadow and Upper Shadow";
}else if(f1){
s += " with Upper Shadow";
}else if(f2){
s += " with Lower Shadow";
}
cout << s << endl;
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int a,b;
cin >> a >> b;
int cnt = 0;
int sum = 0;
for(int i=a;i<=b;i++){
cnt++;
printf("%5d",i);
if(cnt%5==0){
cout << endl;
}
sum+=i;
}
if(cnt%5!=0){
cout << endl;
}
printf("Sum = %d\n",sum);
return 0;
}
算法标签: 分数
#include
using namespace std;
double fact(int t){
double s = 1.0;
for(int i=1;i<=t;i++){
s*=i;
}
return s;
}
double fact2(int t){
double s = 1.0;
for(int i=1;i<=t;i+=2){
s*=i;
}
return s;
}
int main(){
double f;
cin >> f;
double sum1 = 0.0;
double sum2 = 1.0;
int fenzi = 1;
int fenmu = 3;
while(fabs(sum2-sum1)>=f){
sum1 = sum2;
double s1 = fact(fenzi);
fenzi++;
double s2 = fact2(fenmu);
fenmu+=2;
sum2 += s1/s2;
}
sum2*=2;
printf("%.6lf\n",sum2);
return 0;
}
算法标签: 数论 + 模拟
#include
using namespace std;
bool vis[15];
int main(){
int a;
cin >> a;
int cnt = 0;
int bit = 0;
for(int i=a;i<=a+3;i++){
if(!vis[i] && bit<3){
vis[i] = 1;
bit++;
for(int j=a;j<=a+3;j++){
if(!vis[j] && bit<3){
vis[j] = 1;
bit++;
for(int k=a;k<=a+3;k++){
if(!vis[k] && bit<3){
vis[k] = 1;
bit++;
cnt++;
if(cnt%6!=1){
cout << " " << i << j << k;
}else{
cout << i << j << k;
}
if(cnt%6==0){
cout << endl;
}
bit--;
vis[k] = 0;
}
}
vis[j] = 0;
bit--;
}
}
vis[i] = 0;
bit--;
}
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int N,U,D;
cin >> N >> U >> D;
int cnt = 0;
int sum = 0;
while(sum<N){
if(cnt%2==0){
sum += U;
}else{
sum -= D;
}
cnt++;
}
cout << cnt << endl;
return 0;
}
算法标签: 二分
#include
using namespace std;
double a3,a2,a1,a0;
double cal(double x){
return a3*x*x*x + a2*x*x + a1*x + a0;
}
int main(){
cin >> a3 >> a2 >> a1 >> a0;
double left,right;
cin >> left >> right;
double ans;
if(fabs(cal(left))<1e-5 || fabs(cal(right))<1e-5){
if(cal(left)==0){
ans = left;
}else{
ans = right;
}
}else{
while(left<=right && fabs(right-left)>1e-5){
double mid = (left+right)/2.0;
if(fabs(cal(mid))<1e-5){
ans = mid;
break;
}
if(cal(left)*cal(mid)<0){
right = mid;
}else if(cal(mid)*cal(right)){
left = mid;
}
}
}
printf("%.2lf\n",ans);
return 0;
}
算法标签: 数论 + 模拟
#include
using namespace std;
int main(){
int n;
cin >> n;
int y=-1,f=-1;
for(int i=0;i<=99;i++){
for(int j=i+1;j<=99;j++){
if(199*i+n == 98*j){
y = i;
f = j;
break;
}
}
}
if(y==-1){
cout << "No Solution" << endl;
}else{
cout << y << "." << f << endl;
}
return 0;
}
算法标签: 输入/输出
#include
using namespace std;
int main(){
int N;
cin >> N;
for(int i=1;i<=N;i++){
for(int j=1;j<=i;j++){
printf("%d*%d=%-4d",j,i,i*j);
}
printf("\n");
}
return 0;
}
算法标签: 数论 + 模拟
#include
using namespace std;
int main(){
int N;
cin >> N;
int cnt = 0;
for(int x=1;x<=sqrt(N);x++){
for(int y=x;y<=sqrt(N);y++){
if(x*x+y*y==N){
printf("%d %d\n",x,y);
cnt++;
}
}
}
if(!cnt){
printf("No Solution\n");
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
int main(){
int t;
cin >> t;
int rabbit = 0;
int turtle = 0;
int f = 0;
while(t){
if(t>=10){
if(f){
turtle += 3*10;
f--;
}else{
rabbit += 9*10;
turtle += 3*10;
if(rabbit>turtle){
f = 3;
}
}
t -= 10;
}else{
if(f--){
turtle += 3*t;
}else{
rabbit += 9*t;
turtle += 3*t;
}
t = 0;
}
}
if(rabbit>turtle){
cout << "^_^" << " " << rabbit;
}else if(rabbit==turtle){
cout << "-_-" << " " << rabbit;
}else{
cout << "@_@" << " " << turtle;
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
int a[15];
int main(){
int n;
cin >> n;
if(n==0){
cout << "a" ;
return 0;
}
int cnt = 0;
while(n){
a[cnt++] = n%10;
n/=10;
}
int down;
for(int i=0;i<cnt;i++){
if(a[i]==0){
continue;
}else{
down = i;
break;
}
}
bool f = 0;
for(int i=cnt-1;i>=down;i--){
if(a[i]==0 && f){
}else if(a[i] == 0 && !f){
f = 1;
}else{
if(f){
cout << (char)(0 +'a');
}
f = 0;
cout << (char)(a[i]+'a');
}
if(i==8){
cout << "Y";
f = 0;
}else if(i==4){
cout << "W";
f = 0;
}else if(i%4==3 && !f){
cout << "Q";
}else if(i%4==2 && !f){
cout << "B";
}else if(i%4==1 && !f){
cout << "S";
}
}
return 0;
}
算法标签: 数论,分数
#include
using namespace std;
int gcd(int a,int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
int main(){
int up,down;
scanf("%d/%d",&up,&down);
int g = gcd(up,down);
up/=g;
down/=g;
printf("%d/%d\n",up,down);
return 0;
}
算法标签: 字符串 + 模拟
#include
using namespace std;
char s[15][10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int a[15];
int cnt = 0;
int main(){
int n;
cin >> n;
if(n<0){
cout << "fu ";
}else if(n==0){
cout << "ling" << endl;
return 0;
}
n = abs(n);
while(n){
a[cnt++] = n%10;
n/=10;
}
for(int i=cnt-1;i>=0;i--){
if(i==0){
printf("%s",s[a[i]]);
}else{
printf("%s ",s[a[i]]);
}
}
return 0;
}
算法标签: 字符串
#include
using namespace std;
int main(){
string s;
getline(cin,s);
int cnt = 0;
int count = 0;
for(int i=0;i<s.size();i++){
if((s[i]==' ' || s[i]=='.')&& cnt){
if(count == 0){
printf("%d",cnt);
}else{
printf(" %d",cnt);
}
count++;
cnt = 0;
}else if(s[i]==' ' && !cnt){
}else{
cnt++;
}
}
return 0;
}
算法标签: 排序 + 模拟
#include
using namespace std;
int a[105];
int main(){
int N,K;
cin >> N >> K;
for(int i=0;i<N;i++){
cin >> a[i];
}
for(int i=0;i<K;i++){
for(int j=0;j<N-i-1;j++){
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
}
}
}
for(int i=0;i<N;i++){
if(i==0){
printf("%d",a[i]);
}else{
printf(" %d",a[i]);
}
}
return 0;
}
算法标签: 约瑟夫环
#include
using namespace std;
int dp[1005];
int main(){
dp[1] = 0;
int N;
cin >> N;
for(int i=2;i<=N;i++){
dp[i] = (dp[i-1]+3)%i;
}
cout << dp[N]+1 << endl;
return 0;
}
算法标签: 字符串 + 模拟
#include
using namespace std;
int main(){
string s1,s2;
getline(cin,s1);
getline(cin,s2);
int len = s2.size();
int f = s1.find(s2);
while(f!=-1){
s1 = s1.substr(0,f) + s1.substr(f+len);
f = s1.find(s2);
}
cout << s1 << endl;
return 0;
}
算法标签: 排序 + 模拟
#include
using namespace std;
char a[105][15];
char temp[15];
int main(){
int N,K;
cin >> N >> K;
for(int i=0;i<N;i++){
scanf("%s",a[i]);
}
for(int i=0;i<K;i++){
for(int j=0;j<N-i-1;j++){
if(strcmp(a[j],a[j+1])>0){
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}
for(int i=0;i<N;i++){
printf("%s\n",a[i]);
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
char s[105];
char ans[105];
int main(){
char ch;
int cnt = 0;
while(scanf("%c",&ch) && ch!='\n'){
s[cnt++] = ch;
}
s[cnt] = '\n';
int N;
cin >> N;
int len = strlen(s)-1;
N %= len;
for(int i=0;i<N;i++){
ans[len-N+i] = s[i];
}
for(int i=N;i<len;i++){
ans[i-N] = s[i];
}
ans[len+1] = '\0';
printf("%s\n",ans);
return 0;
}
算法标签: 字符串 + 模拟
#include
using namespace std;
string s[250005];
int main(){
int j = 0;
while((cin >> s[j])){
j++;
}
for(int i=j-1;i>=0;i--){
if(i!=0){
cout << s[i] << " ";
}else{
cout << s[i] <<endl;
}
}
return 0;
}
算法标签: 数论,分数
#include
using namespace std;
struct Fraction{
int up;
int down;
};
int gcd(int a,int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
Fraction reduction(Fraction f){
if(f.up == 0){
f.down = 1;
}
if(f.down < 0 ){
f.up = -f.up;
f.down = -f.down;
}
int g = gcd(max(f.up,f.down),min(f.up,f.down));
f.up /= g;
f.down /=g;
return f;
}
Fraction add(Fraction f1,Fraction f2){
Fraction ans;
int g = gcd(max(f1.down,f2.down),min(f1.down,f2.down));
ans.down = f1.down*f2.down/g;
ans.up = f1.down*f2.up/g + f1.up*f2.down/g;
ans = reduction(ans);
return ans;
}
int main(){
Fraction f1,f2;
scanf("%d/%d",&f1.up,&f1.down);
scanf("%d/%d",&f2.up,&f2.down);
Fraction ans;
ans = add(f1,f2);
if(ans.down == 1){
printf("%d\n",ans.up);
}else{
printf("%d/%d",ans.up,ans.down);
}
return 0;
}
算法标签: 模拟
#include
using namespace std;
struct info{
string name;
string birth;
string sex;
string telephone;
string phone;
};
info in[15];
int main(){
int N;
cin >> N;
for(int i=0;i<N;i++){
cin >> in[i].name >> in[i].birth >> in[i].sex >> in[i].telephone >> in[i].phone;
}
int K;
cin >> K;
for(int i=0;i<K;i++){
int t;
cin >> t;
if(t>=0 && t<N){
cout << in[t].name << " " << in[t].telephone << " " << in[t].phone << " " <<
in[t].sex << " " << in[t].birth << endl;
}else{
cout << "Not Found" << endl;
}
}
return 0;
}
算法标签: 数论,分数
#include
using namespace std;
struct Fraction{
int up;
int down;
};
int gcd(int a,int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
Fraction reduction(Fraction f){
if(f.up == 0){
f.down = 1;
}
if(f.down < 0 ){
f.up = -f.up;
f.down = -f.down;
}
int g = gcd(max(f.up,f.down),min(f.up,f.down));
f.up /= g;
f.down /=g;
return f;
}
Fraction add(Fraction f1,Fraction f2){
Fraction ans;
int g = gcd(max(f1.down,f2.down),min(f1.down,f2.down));
ans.down = f1.down*f2.down/g;
ans.up = f1.down*f2.up/g + f1.up*f2.down/g;
ans = reduction(ans);
return ans;
}
int main(){
Fraction ans;
ans.down = 1;
ans.up = 0;
int N;
cin >> N;
for(int i=0;i<N;i++){
Fraction num;
scanf("%d/%d",&num.up,&num.down);
ans = add(ans,num);
}
ans.down *= N;
ans = reduction(ans);
if(ans.down==1){
printf("%d\n",ans.up);
}else{
printf("%d/%d\n",ans.up,ans.down);
}
return 0;
}
算法标签: 数论,复数
#include
using namespace std;
int main(){
double a1,a2,b1,b2;
scanf("%lf%lf%lf%lf",&a1,&a2,&b1,&b2);
double res1,res2;
res1 = a1 + b1;
res2 = a2 + b2;
printf("(%.1lf%+.1lfi) + (%.1lf%+.1lfi) = ",a1,a2,b1,b2);
if(fabs(res1)<1e-1 && fabs(res2)<1e-1){
printf("0.0\n");
}else if(fabs(res1)<1e-1){
printf("%.1lfi\n",res2);
}else if(fabs(res2)<1e-1){
printf("%.1lf\n",res1);
}else{
printf("%.1lf%+.1lfi\n",res1,res2);
}
res1 = a1 - b1;
res2 = a2 - b2;
printf("(%.1lf%+.1lfi) - (%.1lf%+.1lfi) = ",a1,a2,b1,b2);
if(fabs(res1)<1e-1 && fabs(res2)<1e-1){
printf("0.0\n");
}else if(fabs(res1)<1e-1){
printf("%.1lfi\n",res2);
}else if(fabs(res2)<1e-1){
printf("%.1lf\n",res1);
}else{
printf("%.1lf%+.1lfi\n",res1,res2);
}
res1 = a1 * b1 - a2 * b2;
res2 = a1 * b2 + a2 * b1;
printf("(%.1lf%+.1lfi) * (%.1lf%+.1lfi) = ",a1,a2,b1,b2);
if(fabs(res1)<1e-1 && fabs(res2)<1e-1){
printf("0.0\n");
}else if(fabs(res1)<1e-1){
printf("%.1lfi\n",res2);
}else if(fabs(res2)<1e-1){
printf("%.1lf\n",res1);
}else{
printf("%.1lf%+.1lfi\n",res1,res2);
}
res1 = (a1 * b1 + a2 * b2)/(b1 * b1 + b2 * b2);
res2 = (a2 * b1 - a1 * b2)/(b1 * b1 + b2 * b2);
printf("(%.1lf%+.1lfi) / (%.1lf%+.1lfi) = ",a1,a2,b1,b2);
if(fabs(res1)<1e-1 && fabs(res2)<1e-1){
printf("0.0\n");
}else if(fabs(res1)<1e-1){
printf("%.1lfi\n",res2);
}else if(fabs(res2)<1e-1){
printf("%.1lf\n",res1);
}else{
printf("%.1lf%+.1lfi\n",res1,res2);
}
return 0;
}
算法标签: DFS
#include
using namespace std;
int a[35];
int cnt = 0;
int N;
void dfs(int depth,int num,int index){
if(num==0){
printf("%d=",N);
for(int i=0;i<depth;i++){
if(i==0){
printf("%d",a[i]);
}else{
printf("+%d",a[i]);
}
}
cnt++;
if(cnt%4==0){
printf("\n");
}else{
if(a[0]!=N){
printf(";");
}
}
}
for(int i=index;i<=num;i++){
a[depth] = i;
dfs(depth+1,num-i,i);
}
}
int main(){
cin >> N;
dfs(0,N,1);
return 0;
}
算法标签: 高精加
#include
using namespace std;
const int maxn = 2e5+5;
int a[maxn];
int main(){
int A,N;
cin >> A >> N;
for(int i=0;i<N;i++){
a[i] = (N-i)*A;
}
if(N==0){
printf("0\n");
return 0;
}
int jw = 0;
for(int i=0;i<N;i++){
a[i] = (a[i] + jw);
jw = a[i]/10;
a[i] %=10;
}
int cnt = 0;
while(jw){
a[N+cnt] = jw%10;
jw/=10;
cnt++;
}
for(int i=N+cnt-1;i>=0;i--){
printf("%d",a[i]);
}
printf("\n");
return 0;
}