注:B->PAT乙级题
B1001 害死人不偿命的(3n+1)猜想 (15分)
#include<cstdio>
int main()
{
int n=0,step=0;
scanf("%d", &n);
while(n != 1)
{
if( n % 2 == 0) n = n / 2;
else n= ( 3 * n + 1 ) / 2;
step++;
}
printf("%d",step);
return 0;
}
B1032 挖掘机技术哪家强 (20分)
#include<cstdio>
int main(){
const int maxn = 100010;
int schID=0,peoNUM=0,peoScore=0,maxID=0,maxScore=0;
int schScore[maxn]={0};
scanf("%d",&peoNUM);
for(int i = 1; i<=peoNUM;i++)
{
scanf("%d%d",&schID,&peoScore);
schScore[schID]+= peoScore;
}
for(int i = 1; i<=peoNUM;i++){
if(maxScore < schScore[i]) {
maxScore = schScore[i];
maxID=i;
}
}
printf("%d %d",maxID,maxScore);
return 0;
}
找x(牛客)
#include<cstdio>
int main(){
const int maxn = 210;
int n=0,x=0;
int number[maxn]={0};
scanf("%d",&n);
for(int i = 0; i<n;i++){
scanf("%d",&number[i]);
}
scanf("%d",&x);
for(int i = 0; i<n;i++){
if(number[i] == x) {
printf("%d",i);
break;
}
else if(i == n-1) printf("-1");
}
return 0;
}
B1036 跟奥巴马一起编程 (15分)
#include<cstdio>
int main(){
int n=0,row=0;
char c;
scanf("%d %c",&n,&c); getchar();
if(n%2 == 0)row=n/2;
else row=n/2+1;
for(int i = 0; i< row;i++){
if(i==0 | i==row-1){
for(int j = 0; j<n ;j++)printf("%c",c);
printf("\n");
} else{
printf("%c",c);
for(int j = 0; j<n-2 ;j++)printf(" ");
printf("%c\n",c);
}
}
return 0;
}
日期差值(牛客)
#include<cstdio>
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}
};
bool isLeap(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 ans = 1; // 记录结果
// 第一个日期没有达到第二个日期时进行循环
while(y1 < y2 || m1 < m2 || d1 < d2) {
d1++;
if(d1 == month[m1][isLeap(y1) + 1]) { // 满当月天数
m1++;
d1 = 1;
}
if(m1 == 13) { // 月份满12个月
y1++;
m1 = 1;
}
ans++;
}
printf("%d\n", ans);
}
return 0;
}
B1022 D进制的A+B (20分)
#include<cstdio>
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/=d;
}while(sum!=0);
for(int i= num-1;i>=0;i--)printf("%d",ans[i]);
return 0;
}
#include<cstdio>
#include<cstring>
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(gets(str)){
if(judge(str)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
B1009 说反话 (20分)
法1
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
char str[80]={0},word[80]={0};
cin.getline(str,81);
int len=strlen(str);
int j=0;
for(int i=len-1;i>=0;i--){
if(str[i] !=' ')word[j++]=str[i];
else{
for(int k=j-1;k>=0;k--){
printf("%c",word[k]);
}
printf(" ");
j=0;
}
}
for(int k=j-1;k>=0;k--){
printf("%c",word[k]);
}
}
法2
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
char str[90];
cin.getline(str,81);
int len=strlen(str);
//printf("%d\n",len);
int r=0,h=0; //r为行,h为列
char ans[90][90]; //ans[0] --ans[n]存放单词
for(int i=0;i<len;i++)
{
if(str[i]!=' ')
{
ans[r][h++]=str[i];
}
else{
ans[r][h]='\0';
r++;
h=0;
}
}
ans[r][h]='\0';
for(int i=r;i>=0;i--)
{
printf("%s",ans[i]);
if(i>0) printf(" ");
}
return 0;
}