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

1038 statistics with student achievement (20 points)

This question requires students to read N results, will receive a certain number of students to a given fraction of the output.

Input format:
input gives no more than 10 ^ 5 N is a positive integer in the first line, i.e., the total number of students. Then given N line student score percentile integer, separated by a space. The number score given to the last row of the query K (N is a positive integer not exceeding), followed by the K fractions, separated by a space.

Output Format:
In line given in the query sequence is equal to the number of students scoring a specified fraction, separated by a space, but the end of the line may not have the extra space.

Sample input:

10
60 75 90 55 75 99 82 90 75 50
3 75 90 88

Sample output:

3 2 0

Original title link
ideas:

  • Overall is to enter a number, with statistics counting the array, the same results accumulate, accumulate no different
  • End output without spaces
#include <stdio.h>
int main(){
	int n,i,k,p,a[110]={0};
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&k);
		a[k]++; //成绩相同累加
	}
	scanf("%d",&p);
	for(i=0;i<p;i++){
		scanf("%d",&k);
		printf("%d",a[k]);
		if(i<p-1) printf(" "); //末尾输出无空格
	}
	return 0;
} 

1036 programming together with Obama (15 points)

US President Barack Obama not only appeal to everyone to learn programming, and even set an example to write code, write computer code to become the first president in American history. The end of 2014, to celebrate the "Computer Science Education Week" was officially launched, Obama wrote the computer code is very simple: draw a square on the screen. Now you draw it with him!

Input format:
input given square side length N (3≤N≤20) square sides and composition C in a certain line of characters, a space interval.

Output format:
Output the character C is drawn by a given square. But noted that the line spacing is larger than the column spacing, so in order to make the results look more like a square, the number of lines we output is actually 50% of the number of columns (rounded to the nearest integer).

Sample input:

10 a

Sample output:

aaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaa

Original title link
thinking

  • Since the number of rows is half the number of columns, so that when the number is odd column col, row number col / 2 + 1;
    • When the column number is an even number, the number of rows is the row col / 2
    • Thus the output of three parts, the first row, second row line ~ row-1, row ROW
    • The first row is the first row of n output lines a, is circulated using a layer of
    • For each row in the first row-1 needs to output a line a, and outputs col-2 spaces, and then outputs a a.
  • Note: 2 integer by rounding operation it may be resolved by determining whether an odd number.

AC1

#include <stdio.h>
int main(){
	int n,i,m,j; //行、列 
	char c;
	scanf("%d %c",&m,&c);
	if(m%2==1) n=m/2+1; //m为奇数,向上取整
	else n=m/2; //m为偶数 
	for(i=0;i<m;i++){ //第一行的字符 
		printf("%c",c); //m个字符 
	}
	printf("\n");
	for(i=2;i<n;i++){
		printf("%c",c); //每行的第一个字符 
		for(j=0;j<m-2;j++){
			printf(" "); //m-2个空格 
		}
		printf("%c\n",c); //每行的最后一个字符 
	}
	for(i=0;i<m;i++){ //最后一行的字符 
		printf("%c",c); //m个字符 
	}
	return 0;
} 

AC2

#include <stdio.h>
int main(){
    int n,i,j;
    char ar;
    scanf("%d %c",&n,&ar);
    int hang=(n+1)/2; //奇数行输出
    for(i=0;i<hang;i++){
        for(j=0;j<n;j++){ //第一行、第一列、最后一行、最后一列
            if(i==0 || j==0 || i==hang-1 || j==n-1){ 
                printf("%c",ar);
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

1022 D hexadecimal A + B (20 minutes)

Enter two non-negative decimal integers A and B (≤2 ^ 30-1), the output of the A + B D (1 <D≤10) binary number.

Input format:
Input three integers given sequentially in a row A, B and D.

Output Format:
Output D-nary number of A + B.

Sample input:

123 456 8

Sample output:

1103

Original title link
thinking

  • First calculating A + B and the sum, then the other group which is converted to decimal Conormal output
  • The first number of the sum, take the remainder with a hexadecimal number to be converted and stored in an array, then its hexadecimal number to convert the division to take business
  • Reverse output array
#include <stdio.h>
int main(){
	int A,B,m,cmp[100],i; //m为要转换的进制数 
	scanf("%d %d %d",&A,&B,&m);
	int sum=0,num=0;
	sum=A+B;
	do{ //转换为进制数 
		cmp[num++]=sum%m;
		sum=sum/m;
	}while(sum!=0);
	for(i=num-1;i>=0;i--){ //逆序输出数组 
		printf("%d",cmp[i]);
	} 
	printf("\n"); 
	return 0;
} 

1009 ironic (20 points)

Given a word of English, asking you to write a program, all the words of the sentence order reversed output.

Input format:
test input comprising a test case, given the string length does not exceed a total of 80 in a row. String composed of several words and a number of spaces, where the word is English letters (case is case) consisting of string, separated by a space between words, the input end of the sentence to ensure that no extra space.

Output format:
each test case output per line, output sentence after the reverse.

Sample input:

Hello World Here I Come

Sample output:

Come I Here World Hello

Original title link
thinking

  • After the last word output spaces can lead to malformed
  • Used to determine whether the input is complete EOF
#include <stdio.h>
int main(){
	char a[80][80];
	int n=0,i; //n为单词的个数 
	while(scanf("%s",a[n])!=EOF){
		n++; 
	}
	for(i=n-1;i>=0;i--){ //逆序输出字符 
		printf("%s",a[i]);
		if(i>0) printf(" "); //最后一个字符不输出空格 
	}
	return 0;
} 

1011 A + B and C (15 minutes)

Given interval [-2 ^ 31, 2 ^ 31] three integers A, B and C in, please determines whether A + B is greater than C.

Input format:
Enter line 1 gives a positive integer T (≤10), are the number of test cases. T is then given set of test cases, each per line, the order is given A, B and C. Between integer separated by a space.

Output format:
for each test case, the output in line Case #X: true if A + B> C, otherwise the output Case #X: false, where X is the test case number (starting from 1).

Sample input:

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

Sample output:

Case #1: false
Case #2: true
Case #3: true
Case #4: false

Original title link
thinking

  • If A + B> C, then the output Case #% lld: true, which is the current value of% lld of tcase
  • Otherwise, output Case #% lld: false,% lld in which the current value of tcase
#include <stdio.h>
int main(){
	long long T,A,B,C,i; //区间[-231, 231]内的3个整数A、B和C
	scanf("%lld",&T);
	for(i=1;i<=T;i++){
		scanf("%lld %lld %lld",&A,&B,&C);
		long long k=A+B;
		if(k>C){
			printf("Case #%lld: true\n",i);
		}else{
			printf("Case #%lld: false\n",i);
		}
	}
	return 0;
}
Released nine original articles · won praise 1 · views 6570

Guess you like

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