算法笔记系列:3 入门篇(1)入门模拟
3.1 简单模拟
【PAT B1001】害死人不偿命的(3n+1)猜想
# include<stdio.h>
int main(){
int n,step;
step=0;
scanf("%d",&n);
while (n!=1){
if (n%2==0){ //当if else内只有一行语句可以不用大括号,更简洁
n=n/2;
}else{
n=(3*n+1)/2;
}
step+=1;
}
printf("%d",step);
return 0;
}
【PAT B1032】 挖掘机技术哪家强
# include<stdio.h>
int main(){
int N,num,score,maxn=1,max_score=-1;//这里注意初始赋值,一定要考虑只有一个人还拿0分的情况
int school[100000]={0};
scanf("%d",&N);
for (int i=1;i<=N;i++){
scanf("%d%d",&num,&score);
school[num]+=score;
if (school[num]>max_score){
max_score=school[num];
maxn=num;
}
}
printf("%d %d\n",maxn,max_score);
return 0;
}
3.2 查找元素
【code up 1934】找x
注意code up的题是多点测试
# include<stdio.h>
const int maxn=210;
int a[maxn];
int main(){
int n,x;
while (scanf("%d",&n)!=EOF){
for (int i=0;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&x);
int k;
for (k=0;k<n;k++){
if (a[k]==x){
printf("%d\n",k);
break;
}
}
if (k==n){
printf("-1\n");
}
}
return 0;
}
多点测试一定要注意输出要有换行符
以及注意哪些变量需要重置的放在while里面
# include<stdio.h>
int main(){
int num_list[200];
int n,x;
while (scanf("%d",&n)!=EOF){
int index=-1;
for (int i=0;i<n;i++){
scanf("%d",&num_list[i]);
}
scanf("%d",&x);
for (int i=0;i<n;i++){
if (num_list[i]==x){
index=i;
break;
}
}
printf("%d\n",index);
}
return 0;
}
3.3 图形输出
【PAT B1036】跟奥巴马一起编程
# include<stdio.h>
int main(){
int row,col;
char C;
scanf("%d %c",&col,&C);//注意空格也可以是字符,这边格式要一样
if (col%2==0) row=col/2;
else row=col/2+1;
for (int i=1;i<=row;i++){
if (i==1 or i==row){
for (int k=1;k<=col;k++){
printf("%c",C);
}
}else{
printf("%c",C);
for (int k=1;k<=(col-2);k++){
printf(" ");
}
printf("%c",C);
}
printf("\n");
}
return 0;
}
3.4 日期处理
【codeup 1928】日期差值
- 闰年:能被4整除且不能被100整除或者能被400整除。二月份29天
- 平年:不是闰年即为平年。二月份28天
# include<stdio.h>
int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},
{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
int whole_year[2]={365,366};
bool isrun(int year){
return (year%4==0 && year%100!=0)||(year%400==0);
}
int main(){
int time1,y1,m1,d1;
int time2,y2,m2,d2;
while (scanf("%d%d",&time1,&time2)!=EOF){
if (time1>time2){
int temp=time1;
time1=time2;
time2=temp;
}
y1=time1/10000,m1=time1%10000/100,d1=time1%100;
y2=time2/10000,m2=time2%10000/100,d2=time2%100;
int delay=0;
for (int i=y1;i<=y2;i++){
if (i==y1){ //第一年加后面的日子
delay+=month[m1][isrun(i)]-d1+1;
for (int m=m1+1;m<=12;m++){
delay+=month[m][isrun(i)];
}
}
if (i==y2){ //最后一年加前面的日子
for (int m=1;m<=m2-1;m++){
delay+=month[m][isrun(i)];
}
delay+=d2;
}
if (i>y1&&i<y2) delay+=whole_year[isrun(i)];
if (y1==y2) delay-=whole_year[isrun(i)];//如果在同一年那么delay多加了这一年的天数
}
printf("%d\n",delay);
}
return 0;
}
花里胡哨容易错,上面思路并不推荐,还是书上慢慢加不容易错!!!
3.5 进制转换
十进制转化为Q进制:除基取余法 先取余再取基
【PAT B1022】D进制的A+B
# include<stdio.h>
int main(){
int A,B,D;
scanf("%d%d%d",&A,&B,&D);
int sum=A+B;
int ans[31],num=0;
do{
ans[num++]=sum%D;
sum=sum/D;
}while (sum!=0);
for (int i=num-1;i>=0;i--){
printf("%d",ans[i]);
}
return 0;
}
3.6 字符串处理
输入字符串不建议用gets会数组越界的,比较危险,有的编译器过不了
建议用cin.getline
【codeup 5901】回文串
# include<cstdio>
# include<cstring>
# include<iostream>
using namespace std;
const int maxn=256;
bool judge(char str[]){
int len=strlen(str);
for (int i=0;i<len/2;i++){
if (str[i]!=str[len-i-1]){
return false;
}
}
return true;
}
int main(){
char str[maxn];
while (cin.getline(str,sizeof(str)))
{
bool flag=judge(str);
if (flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
【PAT B1009】说反话
scanf %s 读入的时候以空格和换行作为读入结束的标志
scanf %c 是可以读入空格和换行的
# include <cstdio>
int main(){
int num=0;
char str[90][90];
while (scanf("%s",str[num])!=EOF){
num++;
}
for (int i=num-1;i>=0;i--){
printf("%s",str[i]);
if (i>0) printf(" ");
}
return 0;
}