This paper contains two types of questions: "Code blank" and "program design."
The blanks require the contestants to fill in the missing parts on the basis of figuring out the working principle of the given code, so that the program logic is correct and complete. The code you fill out is not more than one statement (that is, a semicolon cannot appear).
Programming questions require that the program designed by the player can give the correct output for a given input. Note: The input data used when marking the papers may be different from the instance data given in the test paper. The player's program must be generic and not valid only for the data given in the quiz paper.
1. Code fill in the blanks (score 3 points)
The following program prints out the 0~9 number, please supplement the missing code.
public class MyTest
{
public static void f (int begin, int end)
{
__________________;
SYSTEM.OUT.PRINTLN (begin);
F (begin+1, end);
}
public static void Main (string[] args)
{
f (0,9);
}
}
if (begin>end) return
2. Code fill in the blanks (score 4 points)
If you want to exchange the values of two integer variables A and B, you typically use an intermediate variable for the transition, but you can do it without any other variables. Try to fill in the missing code.
A = a ^ b;
b = _________;
A = _________;
A^b a^b
3. Code fill in the blanks (score 3 points)
Many people have played "shoot seven" games. The rule is: everyone in order from 1 start to count, the number to contain 7 or 7 of multiples to clap or other provisions of the way to express over (such as: 7,14,17,71 can not count), the next person to continue to count the following number. Those who violate the rules are punished. The following program simulates this process, taking 7 of the case output "*", please perfect it.
for (int i=1; i<100; i++)
{
if (i% 7 = 0)
System.out.println ("*");
else if (___________________)
System.out.println ("*");
Else
System.out.println (i);
}
i%10==7| | I/10==7
4. Code fill in the blanks (score 5 points)
The purpose of the following static method is to find out when a date is the first day of the year. Try to perfect them.
If incoming: Year=1980,month=1, Day=1
Then return 1
If incoming: year=1980,month=2, Day=1
Then return 32
public static int Getdayofyear (int-year, int month, int day)
{
int [][]days=_____________________;
int flag = (year%4==0 && year%100!=0) | | year%400 = 0? 1:0;
int sum = day;
for (int i=0;i<month;i++) {
Sum + + days[flag][i];
}
return sum;
}
{{0,31,28,31,30,31,30,31,31,30,31,30},{0,31,29,31,30,31,30,31,31,30,31,30}}}
5. Code fill in the blanks (score 2 points)
The purpose of the following methods is to find all the squares between 1~n. Try to complement all missing code.
When n=10, the output result is:
1 4 9
When n=100, the output result is:
1 4 9 16 25 36 49 64 81
public static void Showsqure (int n)
{
for (int i=0; i<n; i++)
{
for (int j=1; j<=i; j + +)
{
if (i==_____________) System.out.print (i + "");
}
}
System.out.println ();
}
J*j
6. Code fill in the blanks (score 9 points)
(A+B) in the expansion of the N-power of the various coefficients are very regular, for n=2,3,4, respectively: 1 2 1, 1 3 3 1,1 4 6 4 1. These coefficients constitute the famous Yang Hui's triangles:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
The following procedure gives the calculation method of the nth coefficient of the first m layer, and tries to perfect it (m,n from 0).
public static int f (int m, int n)
{
if (m==0) return 1;
if (n==0 | | n==m) return 1;
return __________________________;
}
F (m-1, n-1) +f (m-1, N)
7. Code fill in the blanks (score 4 points)
The following code reverses the position of an element in an array:
For example: The original element is [1,2,3]
is reversed and changed to: [3,2,1]
public static void reverse (int[] arrs)
{
for (int i = 0, j = arrs.length-1 I <_________; i++, j--)
{
int temp = Arrs[i];
Arrs[i] = Arrs[j];
ARRS[J] = temp;
}
}
J
8. Program design (full score 15 points)
Enter an integer from the keyboard (1~20)
The number is the size of the matrix, and the 1,2,3...n*n number is filled in in the form of a clockwise spiral. For example:
Enter the number 2, the program output:
1 2
4 3
Enter the number 3, the program output:
1 2 3
8 9 4
7 6 5
Enter the number 4, the program output:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Import java.io.*;
Import Java.util.Scanner;
public class MyTest
{
private static int[][] store=new int[101][101];
public static void f (int row,int length,int start)
{
int i;
for (i=0;i<length;i++)
{
Store[row][row+i]=start;
start++;
}
for (i=1;i<length;i++)
{
Store[row+i][row+length-1]=start;
start++;
}
for (i=length-2;i>=0;i--)
{
Store[row+length-1][row+i]=start;
start++;
}
for (i=length-2;i>0;i--)
{
Store[row+i][row]=start;
start++;
}
if (length==1| | length==2)
Return
F (row+1, length-2, start);
}
public static void print (int[][] store,int N)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
System.out.printf ("%4d", Store[i][j]);
System.out.println ();
}
}
public static void Main (string[] args) throws IOException
{
Scanner br=new Scanner (system.in);
String Str=br.nextline ();
int N=integer.parseint (str);
f (1,n,1);
Print (store, n);
}
}
9. Program Design (full score 9 points)
Enter a date from the keyboard, formatted as yyyy-m-d
Ask to calculate how many days the date is from October 1, 1949
For example:
User entered: 1949-10-2
Program output: 1
User entered: 1949-11-1
Program output: 31
Import java.io.*;
Import Java.util.Scanner;
public class MyTest
{
public static int getdays (int year,int month,int Day)
{
int temp[][]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
Year= ((year%4==0&&year%100!=0) | | year%400==0)? 1:0;
for (int i=0;i<month-1;i++)
Day+=temp[year][i];
Return day;
}
public static int getdays (int year)
{
Return ((year%4==0&&year%100!=0) | | year%400==0)? 366:365;
}
public static void Main (string[] args) throws IOException
{
Scanner br=new Scanner (system.in);
String str=br.nextline (). Trim ();
int N=str.indexof ('-');
int M=str.lastindexof ('-');
int Year=integer.parseint (str.substring (0,n));
int Month=integer.parseint (str.substring (n+1,m));
int Day=integer.parseint (str.substring (m+1));
int sum=0;
if (year<1949)
{
for (; year<1949;year++)
{
Sum+=getdays (year);
}
Sum+=getdays (1949,10,1);
Sum-=getdays (Year,month,day);
}
else if (year==1949)
{
Sum=math.abs (GetDays (year,month,day)-getdays (1949,10,1));
}
Else
{
for (int i=1949;i<year;i++)
{
Sum+=getdays (year);
}
Sum+=getdays (Year,month,day);
Sum-=getdays (1949,10,1);
}
SYSTEM.OUT.PRINTLN (sum);
}
}
10. Program Design (full score 20 points)
Any 5-digit number, such as: 34256, to disrupt its numbers, rearrange, you can get a maximum number: 65432, a minimum of 23456. For the two-digit difference, get: 41976, repeat this number again (0 in front if less than 5 digits). In this way, numbers fall into a circle (called a digital black hole).
For example, the numbers just fall into: [82962, 75933, 63954, 61974] this circle.
Please write a program that finds 5 digits of all possible loops, and outputs each loop occupy 1 lines. Where the 5 digits are all the same, the Loop circle is [0], which can be considered without consideration. The output format of the loop loops is modeled:
[82962, 75933, 63954, 61974]
The sequence of numbers may not be considered.
Import java.util.*;
public class MyTest
{
public static int getNext (int num)
{
String str=string.valueof (num);
str+= "00000";
Char a[]=str.substring (0,5). ToCharArray ();
Arrays.sort (a);
int t1=0,t2=0;
for (int i=0;i<a.length;i++)
{
t1=10*t1+a[i]-' 0 ';
t2=10*t2+a[a.length-i-1]-' 0 ';
}
return t2-t1;
}
public static int GetIndex (int[] arr,int t)
{
for (int i=0;i<t;i++)
{
if (Arr[t]==arr[i])
return i;
}
return-1;
}
public static void Main (string[] args)
{
Bitset b=new Bitset (100001);
for (int k=0;k<100001;k++)
{
B.set (k);
}
for (int i=10000;i<100000;i++)
{
if (B.get (i))
{
Int[] A=new int[10000];
int j=0;
A[j]=i;
int Temp=getnext (a[j]);
if (B.get (temp))
{
while (temp!=0)
{
j + +;
A[j]=temp;
if ((Temp=getindex (a,j)) >=0)
{
System.out.print ("[");
for (; temp<j-1;temp++)
{
System.out.print (a[temp]+ ",");
B.clear (A[temp]);
}
System.out.println (a[j-1]+ "]");
B.clear (A[j-1]);
Break
}
Temp=getnext (A[j]);
if (!b.get (temp))
Break
}
}
}
}
}
}
11. Program Design (full score 9 points)
When you enter a credit card number, there is no fear of losing the wrong loss. You don't have to worry about it, because not a random credit card number is legal, it has to be validated through the Luhn algorithm.
The process of the checksum:
1. Starting with the last digit of the card number, the inverse will add the odd digits (1, 3, 5, and so on).
2, starting from the last digit card number, reverse the number of even digits, first multiplied by 2 (if the product is two digits, then subtract 9), and then sum.
3, the sum of odd digits plus the sum of even digits, the result should be divisible by 10.
For example, the card number is: 5432123456788881
The odd, even digits (marked in red) Distribution: 5432123456788881
Odd digits and =35
The result of an even digit multiplied by 2 (some minus 9): 1 6 2 6 1 5 7 7, sum = 35.
Finally, the 35+35=70 can be divisible by 10, which determines that the checksum passes.
Please write a program that enters the card number from the keyboard and then determines whether the checksum passes. "Fail" is displayed by displaying: "Success".
For example, user input: 356827027232780
Program output: Successful
Import java.util.*;
public class Test
{
public static void Main (string[] args)
{
int num=0;
Boolean isodd=true;
Scanner scanner=new Scanner (system.in);
String string=scanner.nextline (). Trim ();
Char[] C=string.tochararray ();
for (int i=c.length-1;i>=0;i--)
{
int temp=c[i]-' 0 ';
if (isodd)
{
Num+=temp;
Isodd=false;
}
Else
{
if (temp<5)
Num+=2*temp;
Else
num+= (2*temp-9);
Isodd=true;
}
}
if (num%10==0)
SYSTEM.OUT.PRINTLN ("Success");
Else
System.out.println ("Failed