Thought: while loop until the until n = 1, also defines a sentinel, whenever a while loop will pay more sentinel once, the value of the final input sentinel
#include <the iostream>
the using namespace STD;
int main () {
int n; // define positive integer n
int COUNT = 0 ; // define sentinel
CIN >> n; // C ++ input stream, the input value of n
the while (n ! = . 1 ) { // the while loop termination condition storage vivo
IF (n-% 2 == 0 )
n- = n-/ 2 ; // even number except 2
the else
n- = ( . 3 * n-+ . 1 ) / 2 ; // odd number 3 by adding a divide by 2
++ COUNT ;
}
COUT << COUNT; // output stream
}
2.PAT B1032: Excavator strong Which
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 formats :
Input is given in the first line does not exceed a 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 formats:
Given the highest total score in a row, the number of schools, and their total score, 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
Resolution:
First, the total number of required standard input stream input cin excavator n, which is stored in an array, should be noted that they represent include school [number (numbered consecutively starting from 1)], indicating the need to initialize the array starting from 1 to n, should be set so that the array of n + 1. After the for loop, is circulated again and again, at the same time in the circulation cin input, the input is performed simultaneously, wherein the maximum value to find, for each value shall be the sum of a school -> [School: array standard; school scores: an array of values].
How to find the maximum value it? There are several methods can be obtained, most simply speaking, a start setting max = 0 and assuming the largest, and successively comparing the for loop, if the array value is larger than the value of max, the value recorded in this subscript and assign it to the max in order to ensure that the final max is the maximum value, the resulting index is the school, school to get the max value is the result.
# include<iostream>
# include<algorithm>
# include<stdio.h>
# include<string>
using namespace std;
int main(){
int n,h,cj,max=0,maxh;
cin>>n;
int a[n+1];
for(int i=1;i<=n;i++){
a[i]=0;
}
for(int j=1;j<=n;j++){
cin>>h>>cj;
a[h]=a[h]+cj;
if(max<a[h]){
max=a[h];
maxh=h;
}
}
cout<<maxh<<" "<<max;
return 0;
}
3.PAT B1036: programmed together with Obama
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 formats:
In the given input row square side length N ( . 3) consisting of a square side and one of the character C, a space interval.
Output formats:
C output from the given character drawn 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
Analysis: rounding, essentially that is (n + 1) / 2:
If n is an even number (n + 1) / 2 The apparent division (n + 1) / 2 = n / 2
If n is an odd number, compared with (n + 1) / 2
When the output of the first line and the last line of all the outputs, the second time for 2 ~ n-1 a line end to end input a, input intermediate space, used for double-loop solution
#include <iostream>
using namespace std;
int main()
{
int N;
char c;
cin>>N>>c;
for(int i = 0; i < (N + 1) / 2; i++)//四舍五入的本质
{
for(int j = 0; j < N; j++)
{
if(i == 0 || i == (N - 1) / 2 || j == 0 || j == N - 1)//i == 0 and i == (n-1) / 2 represent the first and last lines of all characters output
COUT << C; // remaining only the first and last line of the character needs to output
the else
COUT < < ' ' ;
}
COUT << endl; // wrap
}
return 0 ;
}