【1001—1050】PAT乙级题解汇总

目录

1001 害死人不偿命的(3n+1)猜想 (15分)

1002 写出这个数 (20分)

1003 我要通过! (20分)

1004 成绩排名 (20分)

1005 继续(3n+1)猜想 (25分)

1006 换个格式输出整数 (15分)

1007 素数对猜想 (20分)

1008 数组元素循环右移问题 (20分)

1009 说反话 (20分)

1010 一元多项式求导 (25分)

1011 A+B 和 C (15分)

1012 数字分类 (20分)

1013 数素数 (20分)

1014 福尔摩斯的约会 (20分)

1015 德才论 (25分)

1016 部分A+B (15分)

1017 A除以B (20分)

1018 锤子剪刀布 (20分)

1019 数字黑洞 (20分)

1020 月饼 (25分)

1021 个位数统计 (15分)

1022 D进制的A+B (20分)

1023 组个最小数 (20分)

1024 科学计数法 (20分)

1025 反转链表 (25分)

1026 程序运行时间 (15分)

1027 打印沙漏 (20分)

1028 人口普查 (20分)

1029 旧键盘 (20分)

1030 完美数列 (25分)

1031 查验身份证 (15分)

1032 挖掘机技术哪家强 (20分)

1033 旧键盘打字 (20分)

1034 有理数四则运算 (20分)

1035 插入与归并 (25分)

1036 跟奥巴马一起编程 (15分)

1037 在霍格沃茨找零钱 (20分)

1038 统计同成绩学生 (20分)

1039 到底买不买 (20分)

1040 有几个PAT (25分)

1041 考试座位号 (15分)

1042 字符统计 (20分)

1043 输出PATest (20分)

1044 火星数字 (20分)

1045 快速排序 (25分)

1046 划拳 (15分)

1047 编程团体赛 (20分)

1048 数字加密 (20分)

1049 数列的片段和 (20分)

1050 螺旋矩阵 (25分)


1001 害死人不偿命的(3n+1)猜想 (15分)

#include 
using namespace std;
typedef long long ll; 
int main()
{    
	int n , step = 0;
	cin >> n;
	while(n != 1)
	{
		if(n % 2)
		n = (n * 3 + 1) / 2;
		else
		n /= 2;
		step ++;
	}
	cout << step;
    return 0;
}

1002 写出这个数 (20分)

#include 
using namespace std;
typedef long long ll; 
string num[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int sum = 0;
void changeHanzi(int n)
{
	if(n != 0)
	{
		changeHanzi(n / 10);
		if(n == sum)
		{
			cout << num[n % 10];
		} 
		else
		{
			cout << num[n % 10] << " ";
		}
	}
}
int main()
{    
	string s;	
	cin >> s;
	for(int i = 0;i < s.size();i ++)
	{
		sum += s[i] - '0';
	}
	
	changeHanzi(sum);
    return 0;
}

1003 我要通过! (20分)

#include 
using namespace std;
typedef long long ll; 
int main()
{    
	int n , posP,posT,judgeFlag;
	string test;
	cin >> n;
	
	while(n --)
	{
		judgeFlag = true;
		posP  = posT = -1;
		cin >> test;
		for(int i = 0;i < test.size();i ++)
		{
			if(test[i] != 'P' && test[i] != 'A' && test[i] != 'T')
			judgeFlag = false;
			
			if(test[i] == 'P')
			posP = i;
			
			if(test[i] == 'T')
			posT = i;
		}
		
		if(judgeFlag == false)
		{
			cout << "NO" << endl;
			continue;
		}
		
		if(posP == -1 || posT == -1 || posT - posP == 1 || posT - posP < 0)
		{
			cout << "NO" << endl;
			continue;
		}
		
		if(posP * (posT - posP - 1) == test.size() - posT - 1)
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
	}
    return 0;
}

1004 成绩排名 (20分)

#include 
using namespace std;
typedef long long ll;
struct student{
	string name;
	string id;
	int score;
}stu[105];

int n;

int cmp(student a,student b)
{
	return a.score > b.score;
}
int main()
{    
	string name, id;
	int score;
	
	cin >> n;
	
	for(int i = 0;i < n;i ++)
	{
		cin >> stu[i].name >> stu[i].id >> stu[i].score;
	}
	
	sort(stu , stu + n, cmp);
	
	cout << stu[0].name << " " <

1005 继续(3n+1)猜想 (25分)

#include 
using namespace std;
typedef long long ll;
bool visit[105];
int num[105];
int main()
{    
	int n;
	cin >> n;
	for(int i = 0;i < n;i ++)
	{
		cin >> num[i];
		
		int tmp = num[i];
		
		while(tmp != 1)
		{
			if(tmp % 2)
			{
				tmp = (tmp * 3 + 1) / 2;
				if(tmp <= 100)
				visit[tmp] = true;
			}
			else
			{
				tmp /= 2;
				if(tmp <= 100)
				visit[tmp] = true;
			}
		}
	}
	
	int flagSpace = 0;
	
	sort(num,num + n);
	for(int i = n - 1;i >= 0;i --)
	{
		if(visit[num[i]] == false)
		{
			if(flagSpace == 0)
			cout << num[i];
			else
			cout << " " << num[i];
			
			flagSpace = 1;
		}
	}
    return 0;
}

1006 换个格式输出整数 (15分)

#include 
using namespace std;
typedef long long ll;
int main()
{    
	int n;
	int numBai , numShi , numGe;
	numBai = numShi = numGe = 0;
	
	cin >> n;
	
	numBai = n / 100;
	numShi = n / 10 % 10;
	numGe = n % 10;
	
	for(int i = 0;i < numBai;i ++)
	cout << "B";
	
	for(int i = 0;i < numShi;i ++)
	cout << "S";
	
	for(int i = 1;i <= numGe;i ++)
	cout << i;
    return 0;
}

1007 素数对猜想 (20分)

#include 
using namespace std;
typedef long long ll;
bool isPrime(int n)
{
	if(n == 2 || n == 3)
	return true;
	
	if(n % 6 != 1 && n % 6 != 5)
	return false;
	
	int t = sqrt(n);
	for(int i = 2;i <= t;i ++)
	{
		if(n % i == 0 || n % (i + 2) == 0)
		return false;
	}
	
	return true;
}
int main()
{    
	int n, cnt = 0;
	int before = 2;
	cin >> n;
	for(int i = 3;i <= n;i ++)
	{
		if(isPrime(i))
		{
			if(i - before == 2)
			{
				cnt ++;
			}
			before = i;
		}
	}
	cout << cnt;
    return 0;
}

1008 数组元素循环右移问题 (20分)

#include 
using namespace std;
typedef long long ll;
int main()
{    
	int n , move , num[105] , start;
	cin >> n >> move;
	
	start = move % n;
	
	for(int i = 1;i <= n;i ++)
	{
		cin >> num[start];
		start ++;
		start %= n;
	} 
	for(int i = 0;i < n;i ++)
	{
		if(i != 0)
		cout << " " << num[i];
		else
		cout << num[i];
	}
    return 0;
}

1009 说反话 (20分)

#include 
using namespace std;
typedef long long ll;
int main()
{    
	string words , tmp ,res[105];
	int cnt = 0;	
	getline(cin , words);
	stringstream ss;
	ss << words;
	while(ss >> tmp)
	{
		res[cnt ++] = tmp;
	}
	for(int i = cnt - 1;i >= 0;i --)
	{
		if(i != 0)
		cout << res[i] << " ";
		else
		cout << res[i];
	}
    return 0;
}

1010 一元多项式求导 (25分)

#include 
using namespace std;
typedef long long ll;
int main()
{    
	int a , b , flag = 0;
	while(cin >> a >> b)
	{
		if(flag == 0 && b == 0)
		{
			cout << "0 0";
			break;
		}
		if(b != 0)
		{
			if(flag == 1)
			cout << " ";
			
			cout << a * b << " " << b -1 ;	
			
			flag = 1;
		}
		else
		break;
	}
    return 0;
}

1011 A+B 和 C (15分)

#include 
using namespace std;
typedef long long ll;
int main()
{
    ll a, b , c;
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++)
    {
    	printf("Case #%d: ",i);
    	cin >> a >> b >> c;
    	if(a + b > c)
    	cout << "true" << endl;
    	else
    	cout << "false" << endl;
	}
    return 0;
}

1012 数字分类 (20分)

#include 
using namespace std;
typedef long long ll;
int main()
{
	int n, tmp;
   	int a1, a2 , a3 , a5 , flaga2 = 0 , numa4 = 0;
   	double a4 = 0;
   	a1 = a2 = a3 = 0;
   	a5 = -1;
   	
   	cin >> n;
   	for(int i = 1;i <= n;i ++)
   	{
   		cin >> tmp;
   		if(tmp % 5 == 0 && tmp % 2 == 0)
   		a1 += tmp;
   		
   		if(tmp % 5 == 1)
   		{
   			if(flaga2 % 2 == 0)
   			a2 += tmp;
   			else
   			a2 -= tmp;
   			flaga2 ++;
		}
   		
   		if(tmp % 5 == 2)
   		a3 ++;
   		if(tmp % 5 == 3)
   		{
   			a4 += tmp;
   			numa4 ++;
		}
		
		if(tmp % 5 == 4 && tmp > a5)
		a5 = tmp;
	}
   	if(a1 != 0)
   	cout << a1;
   	else
   	cout << "N";
   	if(flaga2 != 0)
   	cout << " " << a2;
   	else
   	cout << " N";
   	if(a3 != 0)
   	cout << " " << a3;
   	else
   	cout << " N";
   	if(numa4 != 0)
   	printf(" %.1lf",(double)(a4 / numa4));
   	else
   	cout << " N";
   	if(a5 != -1)
   	cout << " " << a5;
   	else
   	cout << " N";
    return 0;
}

1013 数素数 (20分)

#include 
using namespace std;
typedef long long ll;
bool isPrime(int n)
{
	if(n == 2 || n == 3)
	return true;
	
	if(n % 6 != 1 && n % 6 != 5)
	return false;
	
	int t = sqrt(n);
	
	for(int i = 2;i <= t;i ++)
	if(n % i == 0 || n % (i + 2) == 0)
	return false;
	
	return true;
}
int main()
{
	int m,n;
	cin >> m >> n;
	int cnt = 0 , cnt2 = 0;
	for(int i = 2; ;i ++)
	{
		if(isPrime(i))
		{
			cnt ++;
			if(cnt >= m)
			{
				if(cnt == n)
				{
					cout << i;
					break;
				}
				cnt2 ++;
				if(cnt2 == 10)
				{
					cout << i << endl;
					cnt2 = 0;
				}
				else
				cout << i << " ";
			}
		}
	}
    return 0;
}

1014 福尔摩斯的约会 (20分)

#include 
using namespace std;
typedef long long ll;
string week[7] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
int main()
{
	string s1,s2,s3,s4;
	cin >> s1 >> s2 >> s3 >> s4;
	
	
	char flagDay = '#' , flagHour;
	int flagh = 0, flagm ,flagPos;
	for(int i = 0;i < min(s1.size(),s2.size());i ++)
	{
		if(s1[i] >= 'A' && s1[i] <= 'G')
		if((s2[i] >= 'A' && s2[i] <= 'G') && s1[i] == s2[i])
		{
				flagDay = s1[i];
				flagPos = i;
				break;
		}
	}
	for(int i = flagPos + 1;i < min(s1.size(),s2.size());i ++)
	{
			if(s1[i] == s2[i])
			{
				if((s1[i] >= '0' && s1[i] <='9') || (s1[i] >= 'A' && s1[i] <= 'N'))
				{
					flagHour = s1[i];
					break;
				}
			}
	}
	for(int i = 0;i < min(s4.size(),s3.size());i ++)
	{
		if(s3[i] == s4[i])
		{
			if((s3[i] >= 'a' && s3[i] <= 'z') || (s3[i] >='A' && s3[i] <= 'Z'))
			{
				flagm = i;
				break;
			}
		}
	}
	cout << week[flagDay - 'A'] << " ";
	int hour;
	if(flagHour >= '0' && flagHour <= '9')
	hour = flagHour - '0';
	else
	hour = flagHour - 'A' + 10;
	printf("%02d:%02d",hour,flagm);
    return 0;
}

1015 德才论 (25分)

#include
using namespace std;
struct ss
{
	int no;
	int de;
	int cai;
	int toal;
};

bool com(ss a, ss b)
{
	if(a.toal!=b.toal)
	{
		return a.toal>b.toal;
	}
	else if(a.de!=b.de)
	{
		return a.de>b.de;
	}
	else
	{
		return a.no a)
{
//	for(auto t=a.begin() ;t!=a.end() ;t++)
//	{
//		cout<<(*t).no<<" "<<(*t).de<<" "<<(*t).cai< a,b,c,d;
	ss aa;
	for(i=0;i=l&&aa.cai>=l)
		{
			f++;
			if(aa.de>=h&&aa.cai>=h)
			{
				a.push_back(aa);
			}
			else if(aa.cai=h)
			{
				b.push_back(aa);
			}
			else if(aa.cai=aa.cai)
			{
				c.push_back(aa);
			}
			else
			{
				d.push_back(aa);
			}
		}

	}
	cout<

1016 部分A+B (15分)

#include
#include
int main()
{
	char A[11],B[11];
	int DA,DB;
	int PA,PB;
	int i = 0,sumA,sumB;
	scanf("%s%d%s%d",A,&DA,B,&DB);
	PA = PB = 0;
	sumA = sumB = 0;
	while(A[i] != '\0')
	{
		if(DA == A[i]-48)
		PA ++;
		i ++;
	}
	i = 0;
	while(B[i] != '\0')
	{
		if(DB == B[i]- 48)
		PB ++;
		i ++;
	}
	while(PA--)
	{
		sumA += DA*pow(10,PA);
	}
	while(PB--)
	{
		sumB += DB*pow(10,PB);
	}
	printf("%d",sumA + sumB);
	return 0;
}

1017 A除以B (20分)

#include
#include
int main()
{
	char a[1001],b[1005];
	int i,len,temp=0,n,R,count=0;
	scanf("%s%d",a,&n);
	len = strlen(a);
	temp = a[0] - '0';
	if(temp >= n)
	{
		b[count] = temp / n +'0';
		count ++;
	}
	for(i = 1;i < len;i++)
	{
		R = temp % n;
		temp = R*10 + a[i] - '0';
		b[count] = temp / n + '0';
		count ++;
	}
	b[count] = '\0';
	R = temp % n;
	if(len == 1 && temp < n)
	printf("0 %d\n",a[0]-'0');
	else
	printf("%s %d\n",b,R);
	return 0;
}

1018 锤子剪刀布 (20分)

#include 

using namespace std;

int main()
{
    int n,i;
    int a1=0,a2=0,a3=0;
    int aJ=0,aC=0,aB=0,bJ=0,bC=0,bB=0;
    char x,y,blank;
    cin>>n;
    for(i=0;i=aC)&&(aB>=aJ))
        cout<<"B";
    else if(aC>=aJ)
        cout<<"C";
    else
        cout<<"J";
    if((bB>=bC)&&(bB>=bJ))
        cout<<" B";
    else if(bC>=bJ)
        cout<<" C";
    else
        cout<<" J";
    system("\npause");
    return 0;
}

1019 数字黑洞 (20分)

#include   
#include   
using namespace std;  
int inc, de;  
void gao(int x){  
    int a[4];  
    a[0] = x / 1000;  
    a[1] = x / 100 % 10;  
    a[2] = x / 10 % 10;  
    a[3] = x % 10;  
    sort(a, a + 4);  
    inc = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];  
    de = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];  
}  
  
int main(){  
    int n, s;  
    scanf("%d", &n);  
    if ((n / 1000 == n / 100 % 10) && (n / 1000 == n / 10 % 10) && (n / 1000 == n % 10)){  
        printf("%04d - %04d = 0000\n", n, n);  
        return 0;  
    }  
    do{  
        gao(n);  
        s = de - inc;  
        printf("%04d - %04d = %04d\n", de, inc, s);  
        n = s;  
    } while (s != 6174);  
    return 0;  
} 

1020 月饼 (25分)

#include
#include
#include
using namespace std;
struct yb{
	double sum;
	double sell;
	double rate;
};
struct yb a[1005];
int cmp(yb a1,yb a2)
{
	return a1.rate > a2.rate;
}
int main()
{
	int N,D;
	cin>>N>>D;
	for(int i=0;i>a[i].sum;
	for(int j=0;j>a[j].sell;
	for(int k=0;k a[t].sum)
	{
		sum += a[t].sell;
		D -= a[t].sum;
	}
	else
	{
		sum += a[t].rate * D;
		D = 0;
	}
	printf("%.2lf\n",sum);
	return 0;
} 

1021 个位数统计 (15分)

#include
#include
int main()
{
	char a[1001];
	int b[10]={0};
	int i,len,j;
	scanf("%s",a);
	len = strlen(a);
	for(i = 0;i < len;i ++)
	{
		for(j = 0;j <= 9;j ++)
		{
			if(a[i]-'0' == j)
			b[j] ++;
		}
	}
	for(i = 0;i < 10;i ++)
	{
		if(b[i] != 0)
		{
			printf("%d:%d\n",i,b[i]);
		}
	}
	return 0;
}

1022 D进制的A+B (20分)

#include
#include
#include
void dec2n(int x,int n)
{
	if(x>=n)
	dec2n(x/n,n);
	if(x%n>9)
	printf("%c",x%n+55);
	else 
	printf("%d",x%n);

}
int main()
{
	int x,y,n;	//将10进制数x转换为n进制数 
	scanf("%d%d%d",&x,&y,&n);
	dec2n(x+y,n); 
	return 0;
}

1023 组个最小数 (20分)

#include
#include
#include
int main()
{
	int a[10],i;
	for(i = 0;i < 10;i ++)
	scanf("%d",&a[i]);
	for(i = 1;i < 10;i ++)
	{
		if(a[i])
		{
			printf("%d",i);
			a[i] --;
			break;
		}
	}
	for(i = 0;i < 10;i ++)
	{
		while(a[i])
		{
			printf("%d",i);
			a[i] --;
		}
	}
	return 0;
} 

1024 科学计数法 (20分)

#include
#include
int main()
{
	char cc[10000];
	int i,len,flag1,sum=0,flag2,sum2=0;
	gets(cc);
	len = strlen(cc);
	if(cc[0] == '-')
	printf("-");
	for(i = 1;i < len;i ++)
	{
		if(cc[i] == 'E')
		{
			flag1 = i;
			break;
		}
	}
	flag2 = flag1 + 2;
	for(i = flag2;i < len; i++)
	{
		sum = sum * 10 + cc[i] - '0';
	}
	if(sum == 0)
	{
		for(i = 1;i < flag1;i ++)
		printf("%c",cc[i]);
	}
	else if(cc[flag1 + 1] == '+')
	{
		for(i = 1;i < flag1;i ++)
		{
			if(cc[i] != '.')
			{
				printf("%c",cc[i]);
			}
			if(i == sum +2 && sum + 2 < flag1 - 1)
			printf(".");
		}
		for(i = 0;i < sum - flag1 + 3;i ++)
		printf("0");
	}
	else if(cc[flag1 + 1] == '-')
	{
		printf("0.");
		for(i = 0;i < sum-1;i ++)
		{
			printf("0");
		}
		for(i = 1;i < flag1;i ++)
		{
			if(cc[i] != '.')
			printf("%c",cc[i]);
		}
	}
	printf("\n");
	return 0;
}

1025 反转链表 (25分)

#include 
#include 
using namespace std;
int main() {
    int first, k, n, temp;
    cin >> first >> n >> k;
    int data[100005], next[100005], list[100005];
    for (int i = 0; i < n; i++) {
        cin >> temp;
        cin >> data[temp] >> next[temp];
    }
    int sum = 0;//不一定所有的输入的结点都是有用的,加个计数器
    while (first != -1) {
        list[sum++] = first;
        first = next[first];
    }
    for (int i = 0; i < (sum - sum % k); i += k)
        reverse(begin(list) + i, begin(list) + i + k);
    for (int i = 0; i < sum - 1; i++)
        printf("%05d %d %05d\n", list[i], data[list[i]], list[i + 1]);
    printf("%05d %d -1", list[sum - 1], data[list[sum - 1]]);
    return 0;
}

1026 程序运行时间 (15分)

#include
#include
int main() 
{
    int a, b;
    scanf("%d%d", &a, &b);
    int t = floor((b - a) / 100.0 + 0.5);
    printf("%02d:%02d:%02d\n", t / 3600, t % 3600 / 60, t % 60);
    return 0;
}

1027 打印沙漏 (20分)

#include  
#include  
int main()  
{  
    int n,m,i,j,t;  
    char sign;  
    scanf("%d %c",&n,&sign);  
    n-=1;  
    m=1; 
    while(n-2*(m+2)>=0)  
    {  
        m+=2;  
        n-=2*m;  
          
    }  
    t=m/2;  
    for(i=0;i

1028 人口普查 (20分)

#include
#include 
#include 
#include
#include
#include
using namespace std;
typedef struct per{
	char name[6];
	int year;
	int month;
	int day;
	int flag;
}person;
person a[100005];
struct ruel{
	bool operator()(const per &a,const per &b)
	{
		if(a.year != b.year)
		return a.year>n;
	int count=0;
	for(int i=0;i 1814 || (a[i].year == 1814 &&a[i].month > 9) || (a[i].year == 1814 && a[i].month == 9 && a[i].day >= 6)))
           {
           	a[i].flag = 1;
           	count ++;
		   }
		   else
		   a[i].flag = 0;
	}
	sort(a,a+n,ruel());
	int minn,maxn;
	for(int j=0;j=0;j--)
	{
		if(a[j].flag == 1)
		{
			minn = j;
			break;
		}
	}
	if(count == 0)
	printf("0\n");
	else
	printf("%d %s %s\n",count,a[maxn].name,a[minn].name);
	return 0;
}

1029 旧键盘 (20分)

#include
#include  
int main()
{
	char a[80]={0},b[80]={0},c[80]={0};
	int i=0,lena=0,lenb=0,j=0,k=0,q=0,flag=0;
	scanf("%s%s",a,b);
	lena = strlen(a);
	lenb = strlen(b);
	for(i = 0;i < lena;i ++)
	{
		if(a[i] == b[j] && j < lenb)
		{
			j ++;
		}
		else
		{
			flag = 0;
			if(a[i] >= 'a' && a[i] <= 'z')
			{
				a[i] =a[i] - 32;
			}
			for(k = 0;k < q;k ++)
			{
				if(a[i] == c[k])
				flag = 1;
			}
			if(flag == 0)
			{
				c[q++] = a[i];
			}
		}
	}
	for(i = 0;i < q;i ++)
	printf("%c",c[i]);
	printf("\n");
}

1030 完美数列 (25分)

#include   
#include   
#include   
#include   
using namespace std;  
  
int main()  
{  
    long long n,max=0;  
    double p;  
    cin>>n>>p;  
    double a[n];  
    for(int i=0;i>a[i];  
    }  
    sort(a,a+n);  
    for(int i=0;imax)  
                max=j-i+1;  
        }  
    }  
    cout<

1031 查验身份证 (15分)

#include
int main()
{
	char a[100][19],e[11]={'1','0','X','9','8','7','6','5','4','3','2'};
	int b[17],c[100]={0},d[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	int i,n,j,sum=0,k,temp=0;
	scanf("%d",&n);
	for(i = 0;i < n;i ++)
	scanf("%s",a[i]);
	for(i = 0;i < n;i ++)
	{
		sum = 0;
		for(j = 0;j <17;j ++)
		{
			b[j] = a[i][j]-'0';
			sum += d[j]*b[j];
		}
		sum = sum % 11;
		if(a[i][17] == e[sum])
		{
			for(k = 0;k < 17;k ++)
			{
				if(a[i][k] >= '0' &&a[i][k] <= '9' )
				c[i] += 1;
				else
				break;
			}
		}
	}
	for(i = 0;i < n;i ++)
	{
		if(c[i] == 17)
		temp ++;
	}
	if(temp == n)
	printf("All passed\n");
	else
	{
		for(i = 0;i < n;i ++)
		{
			if(c[i] != 17)
			printf("%s\n",a[i]);
		}
	}
	return 0;
} 

1032 挖掘机技术哪家强 (20分)

#include
int main()
{
	int n,a[100001]={0},temp;
	int i,score,max,flag;
	scanf("%d",&n);
	for(i = 1;i <= n;i ++)
	{
		scanf("%d%d",&temp,&score);
		a[temp] += score; 
	}
	max = a[1];
	flag = 1;
	for(i = 2;i <= n;i ++)
	{
		if(a[i] > max)
		{
			max = a[i];
			flag = i;		
		}
	}
	printf("%d %d\n",flag,max);
	return 0;
}

1033 旧键盘打字 (20分)

#include 
using namespace std;
int main() {
    string a, b;
    getline(cin, a);//为了防止第一行是空的,不能用cin >> a,用getline(cin, a)
    getline(cin, b);
    if (a.length() == 0) {
        cout << b;
        return 0;
    }
    int flag = 0;
    int lena = a.length();
    int lenb = b.length();
    //检验是否有大写
    for (int i = 0; i < lena; i++) {
        if (a[i] == '+') {
            flag = 1;
            a[i] = '#';
        }
    }
    //去除大写字母
    if(flag) {
        for (int i = 0; i < lenb; i++) {
            if (b[i] >= 'A' && b[i] <= 'Z') {
                b[i] = '#';
            }
        }
    }
    //去除其它字母
    for (int i = 0; i < lenb; i++) {
        for (int j = 0; j < lena; j++) {
            if (a[j] == b[i]) {
                b[i] = '#';
            }
            if (a[j] >= 'A' && a[j] <= 'Z' && b[i] == a[j] + 32) {
                b[i] = '#';
            }
        }
    }
    int flag1 = 0;
    //print
    for (int i = 0; i < lenb; i++) {
        if (b[i] != '#') {
            cout << b[i];
            flag1 = 1;
        }
    }
    if (flag1 == 0)
        cout << endl;
    return 0;
}

1034 有理数四则运算 (20分)

#include
#include
long long a1,b1,a2,b2,a3,b3,t;
long long gcd(long long a,long long b)//求最大公约数 
{
	return b==0?a:gcd(b,a%b);
}
void fun(long long int m, long long int n) {
    long long int x,y,t1,t2,t3;
	int flag1 = 0;
    int flag2 = 0;
    if (n == 0) {
        printf("Inf");
        return ;
    }
    if (m == 0) {
        printf("0");
        return ;
    }
    
    if (m < 0) {
        m = 0 - m;
        flag1 = 1;
    }
    if (n < 0) {
        n = 0 - n;
        flag2 = 1;
    }
    int flag = 0;
    if (flag1 == 1 && flag2 == 1) {
        flag = 0;
    } else if (flag1 == 1 || flag2 == 1) {
        flag = 1;
    }
    if (m == n) {
        if (flag == 1)
            printf("(-1)");
        else
             printf("-1");
        return;
    }
    
    x = m % n;
    y = m / n;
    if (x == 0) {
        if (flag == 0)
            printf("%lld",y);
        else
            printf("(-%lld)",y);
        return ;
    } else {
        t1 = m - y * n;
        t2 = n;
        t3 = gcd(t1, t2);
        t1 = t1 / t3;
        t2 = t2 / t3;
        if (flag == 1) {
            printf("(-");
            if (y != 0)
                printf("%lld %lld/%lld",y,t1,t2);
            else
                printf("%lld/%lld",t1,t2);
            printf(")");
        } else {
            if (y != 0)
                printf("%lld %lld/%lld",y,t1,t2);
            else
                printf("%lld/%lld",t1,t2);
				
        }
    }
}
void add()
{
	fun(a1,b1);
	printf(" + ");
	fun(a2,b2);
	printf(" = ");
	a3 = a1*b2 + a2*b1;
	b3 = b1 * b2;
	t = fabs(gcd(a3,b3));
	a3 /= t;
	b3 /= t;
	fun(a3,b3);
	printf("\n");
	
}
void jian()
{
	fun(a1,b1);
	printf(" - ");
	fun(a2,b2);
	printf(" = ");
	a3 = a1*b2 - a2*b1;
	b3 = b1 * b2;
	t = fabs(gcd(a3,b3));
	a3 /= t;
	b3 /= t;
	fun(a3,b3);
	printf("\n");
	
}
void cheng()
{
	fun(a1,b1);
	printf(" * ");
	fun(a2,b2);
	printf(" = ");
	a3 = a1 * a2;
	b3 = b1 * b2;
	t = fabs(gcd(a3,b3));
	a3 /= t;
	b3 /= t;
	fun(a3,b3);
	printf("\n");
	
}
void chu()
{
	fun(a1,b1);
	printf(" / ");
	fun(a2,b2);
	printf(" = ");
	a3 = a1 * b2;
	b3 = b1 * a2;
	t = fabs(gcd(a3,b3));
	a3 /= t;
	b3 /= t;
	fun(a3,b3);
	printf("\n");
	
}
int main()
{
	scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
	add();
	jian();
	cheng();
	chu();
	return 0;
}

1035 插入与归并 (25分)

#include
using namespace std;
const int N = 105;
int a[N], b[N], n, i, j;
int main()
{
    cin >> n;
    for(i = 0;i < n;++ i)
        cin >> a[i];
    for(i = 0;i < n;++ i)
        cin >> b[i];

    for(i = 0;i < n - 1 && b[i] <= b[i + 1]; ++ i);
    for(j = i + 1;a[j] == b[j] && j < n; ++ j);

    if (j == n)
    {
        cout << "Insertion Sort" << endl;
        sort(a,a + i + 2);
    }
    else
    {
        cout << "Merge Sort" << endl;
        int k = 1, flag = 1;
        while(flag)
        {
            flag = 0;
            for(i = 0;i < n;++ i)
            {
                if (a[i] != b[i])
                    flag = 1;
            }
            k = k << 1;
            for(i = 0;i < n / k;++ i)
            {
                sort(a + i * k, a + (i + 1) * k);
            }
            sort(a + n / k * k, a + n);
        }
    }
    for(i = 0;i < n;++ i)
    {
        if(i)
            cout << " ";
        cout << a[i];
    }
	return 0;
}
/*
10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6
*/

1036 跟奥巴马一起编程 (15分)

#include
int main()
{
	int n,i,j,row,space;
	char cc;
	scanf("%d %c",&n,&cc);
	if(n % 2 == 0)
	row = n / 2;
	else
	row = n / 2 + 1 ;
	for(i = 1;i <= row;i ++)
	{
		space = n -2;
		if(i == 1)
		{
			for(j = 1;j <= n;j ++)
			printf("%c",cc);
			printf("\n");
		}
		else if(i == row)
		{
			for(j = 1;j <= n;j ++)
			printf("%c",cc);
		}
		else
		{
			printf("%c",cc);
			for(j = 1;j <= n - 2;j ++)
			printf(" ");
			printf("%c",cc);
			printf("\n");
		}
	}
	return 0;
}

1037 在霍格沃茨找零钱 (20分)

#include
int main()
{
	int pay[3],soll[3],fina[3];
	int i,sum1,sum2;
	scanf("%d.%d.%d",&soll[0],&soll[1],&soll[2]);
	scanf("%d.%d.%d",&pay[0],&pay[1],&pay[2]);
	sum1 = soll[0] * 17 * 29 + soll[1] * 29 + soll[2];
	sum2 = pay[0] * 17 * 29 +pay[1] * 29 + pay[2];
	if(sum1 <= sum2)
	{
		if(pay[2] - soll[2] >= 0)
		fina[2] = pay[2] - soll[2];
		else
		{
			fina[2] = pay[2] + 29 - soll[2];
			pay[1] --;
		}
		if(pay[1] - soll[1] >= 0)
		fina[1] = pay[1] - soll[1];
		else
		{
			fina[1] = pay[1] + 17 - soll[1];
			pay[0] --;
		}
		fina[0] = pay[0] - soll[0];
		printf("%d.%d.%d\n",fina[0],fina[1],fina[2]);
	}
	else
	{
		if(soll[2] - pay[2] >= 0)
		fina[2] = soll[2] - pay[2];
		else
		{
			fina[2] = soll[2] + 29 - pay[2];
			soll[1] --;
		}
		if(soll[1] - pay[1] >= 0)
		fina[1] = soll[1] - pay[1];
		else
		{
			fina[1] = soll[1] + 17 - pay[1];
			soll[0] --;
		}
		fina[0] = soll[0] - pay[0];
		printf("-%d.%d.%d\n",fina[0],fina[1],fina[2]);
	}
	return 0;
}

1038 统计同成绩学生 (20分)

#include
int main()
{
	int n,k,stu[100000],score[100000],num[100000]={0},a[101]={0};
	int i,j;
	scanf("%d",&n);
	for(i = 0;i < n;i ++)
	{
		scanf("%d",&stu[i]);
		a[stu[i]] ++;
	}
	scanf("%d",&k);
	for(i = 0;i < k;i ++)
	scanf("%d",&score[i]);
	for(i = 0;i < k;i ++)
	{
		num[i] = a[score[i]];
	}
	for(i = 0;i < k;i ++)
	{
		if(i == k-1)
		printf("%d\n",num[i]);
		else
		printf("%d ",num[i]);
	}
	return 0;
}

1039 到底买不买 (20分)

#include
#include
int main()
{
	char red[1000],soll[1000];
	int i,j,notfound,count,flag;
	memset(red,'+',sizeof(red));
	memset(soll,'+',sizeof(soll));
	scanf("%s%s",soll,red);
	notfound = count = flag = 0;
	for(i = 0;i 

1040 有几个PAT (25分)

#include
#include
int main()
{
	int n,count,i,flagP,flagT;
	char a[100000];
	scanf("%s",a);
	n = strlen(a);
	flagP = flagT = count = 0;
	for(i = 0;i < n;i ++)
	if(a[i] == 'T')
	flagT ++;
	for(i = 0;i < n;i ++)
	{
		if(a[i] == 'P')
		flagP ++;
		if(a[i] == 'T')
		flagT --;
		if(a[i] == 'A')
		count = (count + (flagP *flagT) % 1000000007)% 1000000007;
	}
	printf("%d\n",count);
	return 0;
 } 

1041 考试座位号 (15分)

#include
#include
#include
using namespace std;
typedef struct{
	char num[16];
	int tryexam;
	int trueexam;
}text;
text a[1005];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i>a[i].num>>a[i].tryexam>>a[i].trueexam;
	int m;
	cin>>m;
	int nm[1005];
	for(int i=0;i>nm[i];
	for(int i=0;i

1042 字符统计 (20分)

#include
#include
int main()
{
	char a[1001],cc;
	int i,b[26]={0},max;
	gets(a);
	for(i = 0;i < strlen(a);i ++)
	{
		if(a[i] >= 'a' && a[i] <= 'z')
		{
			b[a[i] - 'a'] ++;
		}
		if(a[i] >= 'A' && a[i] <= 'Z')
		{
			b[a[i] - 'A'] ++;
		}
	}
	max = 0;
	for(i = 1;i < 26;i ++)
	{
		if(b[max] < b[i])
		max = i;
	}
	cc = 'a'+ max;
	printf("%c %d\n",cc,b[max]);
	return 0;
}

1043 输出PATest (20分)

#include
#include
int main()
{
	char a[10005],b[10005];
	int i,count=0,flag=0;
	scanf("%s",a);
	for(i = 0;i < strlen(a);i++)
	{
		if(a[i] != 'P' && a[i] != 'A' && a[i] != 'T' && a[i] != 'e' && a[i] != 's' && a[i] != 't')
		{
			a[i] = '#';
		}
	}
	while(1)
	{
		for(i = 0;i < strlen(a);i ++)
		{
			if(a[i] == 'P')
			{
				b[count++] = a[i];
				a[i] = '#';
				break;
			}
		}
		for(i = 0;i < strlen(a);i ++)
		{
			if(a[i] == 'A')
			{
				b[count++] = a[i];
				a[i] = '#';
				break;
			}
		}
		for(i = 0;i < strlen(a);i ++)
		{
			if(a[i] == 'T')
			{
				b[count++] = a[i];
				a[i] = '#';
				break;
			}
		}
		for(i = 0;i < strlen(a);i ++)
		{
			if(a[i] == 'e')
			{
				b[count++] = a[i];
				a[i] = '#';
				break;
			}
		}
		for(i = 0;i < strlen(a);i ++)
		{
			if(a[i] == 's')
			{
				b[count++] = a[i];
				a[i] = '#';
				break;
			}
		}
		for(i = 0;i < strlen(a);i ++)
		{
			if(a[i] == 't')
			{
				b[count++] = a[i];
				a[i] = '#';
				break;
			}
		}
		for(i = 0,flag = 0;i < strlen(a);i ++)
		if(a[i] == '#')
		flag ++;
		if(flag == strlen(a))
		break;
	}
	b[count] = '\0';
	printf("%s",b);
	return 0;
}

1044 火星数字 (20分)

#include
using namespace std;
const int N = 105;
string s1[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string s2[13]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
map m;
int main()
{
    for(int i = 0;i <= 12;++ i)
    {
        m[s1[i]] = i;
        if (i)
        {
           m[s2[i]] = i * 13;
        }
    }
    int n;
    string in;
    cin >> n;
    getchar();
    while(n --)
    {
        getline(cin, in);
        if (in[0] >= '0' && in[0] <= '9')
        {
            int a;
            stringstream ss(in);
            ss >> a;
            if (a / 13)
            {
                if (a % 13 == 0)
                    cout << s2[a / 13] << endl;
                else
                    cout << s2[a / 13] << " " << s1[a % 13] << endl;
            }
            else
                 cout << s1[a % 13] << endl;
        }
        else
        {
            vector v;
            istringstream istr(in);
            string tmp;
            while(!istr.eof())
            {
                istr >> tmp;
                v.push_back(tmp);
            }
            int sum = 0;
            for(string i : v)
            {
                sum += m[i];
            }
            cout << sum << endl;
        }
    }
	return 0;
}

1045 快速排序 (25分)

#include
#include
#include
#include
#include
using namespace std;
int a[100005],b[100005],r[100005];
int main()
{
	int cnt=0;
	int n;
	cin>>n;
	for(int i=0;i>a[i];
		b[i] = a[i];
	}
	sort(b,b+n);
	int max=0;
	for(int i=0;i max)
		r[cnt++] = a[i];
		if(a[i] > max)
		max = a[i];
	}
	printf("%d\n",cnt);
	for(int i=0;i

1046 划拳 (15分)

#include
int main()
{
	int a[100][4];
	int n,i,j,cntjia,cntyi,flagjia,flagyi,sum;
	scanf("%d",&n);
	for(i = 0;i < n;i ++)
	{
		for(j = 0;j < 4;j ++)
		scanf("%d",&a[i][j]);
	}
	cntjia = cntyi =0;
	for(i= 0;i < n;i ++)
	{
		flagjia = flagyi = 0;
		sum = a[i][0] + a[i][2];
		if(a[i][1] == sum)
		flagjia = 1;
		if(a[i][3] == sum)
		flagyi = 1;
		if(flagjia == 1 && flagyi == 0)
		cntyi ++;
		else if(flagjia == 0 && flagyi == 1)
		cntjia ++;
		
	}
	printf("%d %d\n",cntjia,cntyi);
	return 0;
 } 

1047 编程团体赛 (20分)

#include
typedef struct t{
	int team;
	int id;
	int score;
}Team;
int main()
{
	Team a[10000];
	int b[1001]={0};
	int i,n,max,flag;
	scanf("%d",&n);
	for(i = 0;i < n;i ++)
	scanf("%d-%d %d",&a[i].team,&a[i].id,&a[i].score);
	for(i = 0;i < n;i ++)
	{
		b[a[i].team] += a[i].score;
	}
	max = b[1];
	flag = 1;
	for(i = 2;i <= 1000;i ++)
	{
		if(b[i] > max)
		{
			max = b[i];
			flag = i;
		}
	}
	printf("%d %d\n",flag,max);
	return 0;
}

1048 数字加密 (20分)

#include
using namespace std;
int main()
{
    string s1,s2;
    cin >> s1 >> s2;
    int l1 = s1.size() - 1, l2 = s2.size() - 1;
    reverse(s1.begin(),s1.end());
    reverse(s2.begin(),s2.end());
    if(l1 > l2)
        s2.append(l1 - l2, '0');
    else
        s1.append(l2 - l1, '0');
    string s3 = "";
    char str[14] = {"0123456789JQK"};
    for(int i = 0;i < s1.size();++ i)
    {
        if (i&1)
        {
            int temp = s2[i] - s1[i];
            if (temp < 0) temp = temp + 10;
            s3 += str[temp];
        }
        else
        {
            s3 += str[(s1[i] - '0' + s2[i] - '0') % 13];
        }
    }
    reverse(s3.begin(),s3.end());
    cout << s3;
	return 0;
}

1049 数列的片段和 (20分)

#include
#include
#include
#include
using namespace std;
int main()
{
	int n;
	cin>>n;
	double sum=0,t;
	for(int i=0;i>t;
		sum += t*(n-i)*(i+1);
	}
	printf("%.2lf\n",sum);
	return 0;
}

1050 螺旋矩阵 (25分)

#include
#include
#include
#include
using namespace std;
int a[10005][10005];
int num[10005];
int main()
{
	int N,m,n;
	cin>>N;
	int flag=0;
	n = sqrt(N);
  if(n*n == N)
  flag = 1;
  while(1)
  {
    if(flag==1)
    m = n;
    if(n*m == N)
    break;
    else
    {
      if(m*n > N)
      n--;
      else
      m++;
    }
  }
	/*以上解决:满足条件:m*n等于N;m>=n;且m-n取所有可能值中的最小值。*/
	//m行 n列 
	for(int i=0;i>num[i];
	sort(num,num+N,greater());
	int x,y,tot;
	a[0][0]=num[0];
    tot=x=y=0;
    while(tot < m * n-1)
     {
         while(y + 1 < n && ! a[x][y + 1])
             a[x][++y] =num[++tot];
         while(x + 1 < m && !a[x + 1][y])
             a[++x][y] = num[++tot];
         while(y - 1 >= 0 && !a[x][y - 1])
             a[x][--y] =num[++tot];
		while(x - 1 >= 0 && !a[x - 1][y])
             a[--x][y] = num[++tot];   
     }
	for(int i=0;i

 

你可能感兴趣的:(PAT乙级)