PAT (Basic Level) Practice (Chinese) Beginners (1) C

1001 killed attractiveness of the (3n + 1) guess (15 points) C language

Kharazi (Callatz) guess:

For any positive integer n, if it is even, then it halved; if it is odd, the (3n + 1) cut by half. This has been repeatedly cut, finally got to get n = 1 in a step. Kharazi at the 1950 World Congress of Mathematicians announced this conjecture, was the legendary Yale University teachers and students Qidong Yuan, desperately want to prove this seemingly silly naive proposition, students inadvertently result so much noise studies, one only card (3n +1), so some people say this is a conspiracy, Kharazi was deliberately delaying the progress of American mathematics teaching and research ......

Our topic today is not evidence Minka Raz conjecture, but on any given positive integer not exceeding 1,000 n, simply count the number, how many steps (cut a few) to get n = 1 need?

Input format:
Each test comprises a test input, i.e., the value given positive integer n.

Output format:
output calculation from the n-1 to the number of steps required.

Sample input:

3

Sample output:

5

Original title link

Ideas:
reading the title given n, then a determination is repeated while loop segment n is 1

  • If n is 1, the loop is exited;
  • ruguon is not 1, it is determined whether n is even and, if even, so that in addition to 2 n; n is the order (3 * n +) / 2, so that after the counter count by 1;
  • When the loop exits, count value is the answer.
#include <stdio.h>
int main(){
	int n,count=0;
	scanf("%d",&n); //输入n 
	while(n!=1){ //循环判断n是否为1 
		if(n%2==0) n=n/2; //如果是偶数 
		else n=(3*n+1)/2; //如果是奇数 
		count++; //计数器加1 
	}
	printf("%d\n",count);
	return 0;
} 

Which strong Excavator 1032 (20 minutes)

To illustrate the facts Excavator Which in the end strong, PAT excavator organized a skills competition. Now you that the statistics of the strongest technical schools based on results of the competition.

Input format:
input line 1 is given no more than 10 ^ 5 positive integer N, i.e. the number of entries. Then N lines, each and every participant is given the information and results, including the school they represent numbers (starting with 1 numbered consecutively), and game scores (percentile), separated by a space.

Output format:
given the highest total score line number school, and out of, separated by a space. The only answer is to ensure that the subject is not tied.

Sample input:

6
3 65
2 80
1 100
2 70
3 40
3 0

Sample output:

2 150

Original title link
ideas:

  • Order array school [10010] each school score recorded, the initial value of 0. For each read school The Schild Score score corresponding thereto, so that school [schID] + = score.
  • Let the variable k recorded the highest number out of school, variable MAX recording the highest total score, the initial value of -1. Because schools are consecutively numbered, so the US drama Number 1 ~ N, k and MAX can be constantly updated.
#include <stdio.h>
int school[100010]={0}; //记录每个学校的总分 
int main(){
	int n,i,schID,score;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&schID,&score); //学校ID、分数 
		school[schID] += score; //学习schID的总分增加score 
	} 
	int k=1,MAX= -1; //总得分最高的学校的编号、及其总分 
	for(i=0;i<=n;i++){ //从所有学校中选出总分最高的一个 
		if(school[i]>MAX){
			MAX = school[i];
			k=i;
		}
	} 
	printf("%d %d\n",k,MAX); //输出总得分最高的学校的编号、及其总分 
	return 0;
}

Classification number 1012 (20 minutes)

Given to a series of positive integers, press the demand for digital classification, and outputs the following five numbers:

  • A1 = the number can be divisible by 5 and all even;
  • A 2 = 5 will be a digital remainders after division by a sum interleaved order given, i.e., calculates n1-n 2 + n 3-n 4 ⋯;
  • A 3 = 5 is more than the number of the other the number 2;
  • A 4 = the average number of addition is more than 5 digits after 3, accurate to one decimal place;
  • A 5 = more than the maximum number of the number 4 after 5 addition.

Input format:
Each input comprises a test. Each test case is given a first positive integer of not more than 1000 N, and then gives the N does not exceed 1000 to be classified positive integer. Between numbers separated by a space.

Output format:
a given positive integer N, is calculated according to the subject in claim 1 ~ A 5 and A sequentially outputting in a row. Between numbers separated by spaces, but the end of the line may not have the extra space.

If a certain type wherein the number does not exist, the output in the corresponding position N.

Sample Input 1:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

Output Sample 1:

30 11 2 9.7 9

Sample Input 2:

8 1 2 4 5 6 7 9 16

Output Sample 2:

N 11 2 N 9

Original title link
ideas:

  • It is not difficult, but very pit.
  • Beginning with a wrong example, look at the pit only to find users to share,
  • and a2 staggered to 0, does not mean that does not exist, so still have to count the number of a2.
#include <stdio.h>
int main(){
	int N,i;
	while(scanf("%d",&N)!=EOF){
		int a1=0,a2=0,a21=0,a3=0,a4=0,a41=0,a5=0,num,re = 1;
		for(i=0;i<N;i++){
			scanf("%d",&num);
			if(num%10==0) a1+=num; //能被5整除的数字中所有偶数的和
			if(num%5==1){ //将被5除后余1的数字按给出顺序进行交错求和 
                a21++;
				if(a21%2==1) a2 += num; //奇数个相加 
				else a2 -= num; //偶数个相减 
			}
			if(num%5==2) a3++;
			if(num%5==3){
				a4 += num; //A4求合 
				a41++; //计数 
			}
			if(num%5==4){
				if(num>a5) a5=num;
			}
		}
		if(a1) {printf("%d ",a1);} else {printf("N ");}
    	if(a21) {printf("%d ",a2);} else {printf("N ");} //判断a21,坑点!!
    	if(a3) {printf("%d ",a3);} else {printf("N ");}
    	if(a41) {printf("%.1lf ",(double)a4/a41);} else {printf("N ");}
    	if(a5) {printf("%d\n",a5);} else {printf("N\n");}
	}
	return 0;
}

1016 Part A + B (15 minutes)

A positive integer is "D A (as an integer) part" is defined as a new integer P A. A D A composition all For example: Given A = 3862767, D A = 6, then "portion 6 'is P A A 66 is because there are 2 A 6.

Now given A, D A, B, D B, write a program to calculate P A + P B.

Input format:
input sequence is given in row A, D A, B, D B, separated by a space, where 0 <A, B <10 ^ 10.

Output Format:
output value P A + P B is in a row.

Sample Input 1:

3862767 6 13530293 3   

Output Sample 1:

399

Sample Input 2:

3862767 1 13530293 8

Output Sample 2:

0

Original title link
ideas

  • The A, B is defined as a string, through at strlen (), get the length of the string
  • Traversal strings, find the same characters, converted to digital, the final sum
#include <stdio.h>
int main(){
	char a[100],b[100];
	int d1,d2,i,p1=0,p2=0;
	scanf("%s %d %s %d",a,&d1,b,&d2); 
	for(i=0;i<strlen(a);i++){
		if(a[i]==d1+'0') 
			p1=p1*10+d1;
	}
	for(i=0;i<strlen(b);i++){
		if(b[i]==d2+'0') 
			p2=p2*10+d2;
	}
	printf("%d\n",p1+p2);
	return 0;
} 

Hammer 1018 scissors (20 minutes)

We should be able to play "hammer and scissors" game: they both give gesture, as shown in Figure outcome of the rules:
01

Now given the two men clash records, statistics of both wins, flat, negative number, and the two sides are what give the greatest chance of winning gesture.

Input format:
Enter line 1 gives a positive integer N (≤10 ^ 5), i.e., both the number of confrontation. Then N rows, each row gives information confrontation, i.e., the gesture and B sides at the same time is given. C stands for "hammer", the representative J "scissors", B stands for "cloth", the first letter represents a party, on behalf of the second party, there is an intermediate space.

Output Format:
output the first and second lines are given A, B wins, flat, negative number, separated by an inter-digital space. Line 3 shows two letters represent A, B wins the highest number of gestures, there is an intermediate space. If the solution is not unique, the minimum solution alphabetically output.

Sample input:

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

Sample output:

5 3 2
2 3 5
B B

Original title link
ideas

  • When this problem is not difficult, but that the statistical output, level, be careful win
#include <stdio.h>
char max(int B,int C,int J){
	if(B>=C && B>=J) return 'B';
	if(C>B && C>=J) return 'C';
	return 'J';
}
int main(){
	int n,i;
	int a=0,b=0,c=0,a1=0,b1=0,c1=0,d=0;
	char T,F;
	scanf("%d",&n); //双方交锋的次数
	for(i=0;i<n;i++){
		scanf(" %c %c",&T,&F); //甲、乙双方同时给出的的手势,请注意每%c之前的空格 
		if(T=='B'&&F=='C')	a++;
        if(T=='C'&&F=='J')	b++;
        if(T=='J'&&F=='B')	c++;
        if(T=='B'&&F=='J')	c1++;
        if(T=='C'&&F=='B')	a1++;
        if(T=='J'&&F=='C')	b1++;
        if(T==F)	d++;
	}
	printf("%d %d %d\n",a+b+c,d,a1+b1+c1); //甲的胜、平、负次数
	printf("%d %d %d\n",a1+b1+c1,d,a+b+c); //乙的胜、平、负次数
	printf("%c %c",max(a,b,c),max(a1,b1,c1)); //甲、乙获胜次数最多的手势 
	return 0;
}
Released five original articles · won praise 0 · Views 1684

Guess you like

Origin blog.csdn.net/m0_46153949/article/details/104002969