Blog migration-first ACM OJ

Source: Internet
Author: User

It's boring to visit Baidu and know that there's a habit from high school. In the era of middle school, it is generally used to help people with their questions, such as mathematical and physical homework. Of course, there are also methods to teach people to learn and recommend teaching assistants. At the university, few people will know about the high-Number Questions on Baidu, and may not be able to answer questions without reading a book (because there are countless questions in high school, in addition to competition questions, the methods of mathematical physics are almost the same. It is no difficulty to solve the non-competitive questions that Baidu Knows ). Because I learned the C language in the first semester of my freshman year, I went to find some questions about C language. I also learned how to practice C language. I remember someone said that the best way to improve the C language is to go to Baidu to know and answer the C language questions in various forums, so I did this.

 

This afternoon, I went to Baidu to learn about the chaos. I saw someone asking for an ACM.

Source: **************************************** **************************************** **************************************** ****************

Http://acm.uestc.edu.cn/problem.php? Pid = 1, 1023

GPA Calculation

 

Time Limit: 1000 MS Memory Limit: 65535 kB
Solved: 630 Tried: 2240

Description

G. P. A. (Grade Point Average) is the weighted Average of score points and credits.
GPA is generally calculated in a 4-minute (4.00 scale) format. The conversion method is shown in the following table:
Percentage Score Points
90-100 A 4
80-89 B 3
70-79 C 2
60-69 D 1
Less than 60 E 0
For example, the credits and scores of one of the three courses are as follows:
Course A has 4 credits and scores 92 ()
3 credits for B course, score 75 (C)
Class C: 5 credits, score 80 (B)
GPA = (4*4 + 2*3 + 3*5)/(4 + 3 + 5) = 3.08

Input

Enter the first behavior integer N (1 <=n <= 10), indicating that there are N courses.
The following N rows have two integers: C, S (1 <= C <= 5, 0 <= S <= 100), indicating the credits and scores of the course.

Output

The output contains only one decimal point g, indicating the GPA of the student. The result is retained with two decimal places.

Sample Input

3
4 92
3 75
5 80

Sample Output

3.08

Hint

Note: Please strictly abide by the input and output formats given by the question, and do not output any redundant information!
If Java is used, the Main class name of Java must use Main!

Source

Love8909

**************************************** **************************************** **************************************** ************************************

The questioner uses C ++. I wrote it in C ...... Write it in C first, and then read whether it can be written in C ++.

 

At the beginning, I did not see the problem clearly and did not notice that I had to convert the score into credits. I found that the result was 300 + and it was definitely wrong. Let's look at the question and change it back.

Then, to determine the score segment, I want to first forcibly convert (int) the score/10. If it is less than 5, it will directly make it equal to 5, and then use the switch, as long as two judgments are calculated at a time, it will be OK (once/10, once if (), once switch (). The time should be later than if () {} else if () {} else if (){}...... Else {} (4 judgments) should be faster.

Because we have to keep two decimal places at last, we have processed the GPA result by rounding the last 3rd rows.

So I submitted the following.

 

# Include <stdio. h> <br/> # include <math. h> <br/> # define N 10 </p> <p> int main (void) <br/> {<br/> int credit [N], grade [N], GP [N]; <br/> int course, count, choice; <br/> int total_grade = 0; <br/> int total_credit = 0; <br/> double GPA; </p> <p> scanf ("% d", & course ); <br/> if (1 <= course & 5> = course) <br/> {<br/> for (count = 0; count <course; count ++) <br/>{< br/> scanf ("% d", & credit [count], & grade [count]); <br/> choice = (int) (grade [count]/10); <br/> if (choice <6) <br/>{< br/> choice = 5; <br/>}< br/> switch (choice) <br/>{< br/> case 5: GP [count] = 0; break; <br/> case 6: GP [count] = 1; break; <br/> case 7: GP [count] = 2; break; <br/> case 8: GP [count] = 3; break; <br/> case 9: GP [count] = 4; break; <br/> case 10: GP [count] = 4; break; <br/>}</p> <p> for (count = 0; count <course; count ++) <br/> {<br/> total_grade + = credit [count] * GP [count]; <br/> total_credit + = credit [count]; <br/>}</p> <p> GPA = (double) total_grade/(double) total_credit; </p> <p> GPA = (int) (GPA * pow (0.5) +)/pow (); <br/> printf ("%. 2lf ", GPA); <br/>}

The result is

149173 icelights 1023 Wrong Answer GCC 1190B 2011-03-0613: 28: 24

 

Then, change it ......

# Include <stdio. h> <br/> # include <math. h> <br/> int main (void) <br/>{< br/> int c [10], g [10], GP [10]; <br/> int co, n,; <br/> int tg = 0; <br/> int tc = 0; <br/> double GPA; <br/> scanf ("% d", & co); <br/> if (1 <= co & 5> = co) <br/> {<br/> for (n = 0; n <co; n ++) <br/>{< br/> scanf ("% d", & c [n], & g ); <br/> if (0 <= g & 59> = g) <br/> GP [n] = 0; <br/> else if (60 <= g & 69> = g) <br/> GP [n] = 1; <br/> else if (70 <= g & 79> = g) <br/> GP [n] = 2; <br/> else if (80 <= g & 89> = g) <br/> GP [n] = 3; <br/> else GP [n] = 4; <br/>}< br/> for (n = 0; n <co; n ++) <br/> {<br/> tg + = c [n] * GP [n]; <br/> tc + = c [n]; <br/>}< br/> GPA = (double) tg/(double) tc; <br/> GPA = (int) (GPA * pow (0.5) +) /pow (10, 2); <br/> printf ("%. 2lf ", GPA); <br/> return 0; <br/>}

 

This has already been submitted four times. This is 5th times, or is it wrong ......

149191 icelights 1023 WrongAnswer GCC 1046B 2011-03-0614: 05: 19
149189 icelights 1023 WrongAnswer GCC 978B 2011-03-0614: 01: 56
149187 icelights 1023 WrongAnswer GCC 1009B 2011-03-0613: 58: 55
149173 icelights 1023 WrongAnswer GCC 1190B 2011-03-0613: 28: 24
149172 icelights 1023 WrongAnswer G ++ 1190B 2011-03-0613: 26: 55 // ignore this. If you do not see the language, submit it.

 

Then, no way. Use C ++ to write it.

 

# Include <iostream> <br/> # include <iomanip> <br/> using namespace std; <br/> int main () {<br/> int n = 0, j = 0; <br/> cin> n; <br/> int a [10], B, c [10]; <br/> (; j <n; j ++) {<br/> cin> a [j]> B; <br/> if (B> = 90 & B <= 100) <br/> c [j] = 4; <br/> else if (B> = 80 & B <= 89) <br/> c [j] = 3; <br/> else if (B> = 70 & B <= 79) <br/> c [j] = 2; <br/> else if (B >=60 & B <= 69) <br/> c [j] = 1; <br/> else c [j] = 0; <br/>}< br/> float p = 0, q = 0; <br/> for (j = 0; j <n; j ++) {<br/> p + = a [j] * c [j]; <br/> q + = a [j]; <br/>}< br/> float GPA = p/q; <br/> cout <setprecision (2) <setiosflags (ios: fixed) <GPA; <br/> return 0; <br/>}

 

The result is passed.
149193 icelights 1023 Accepted G + 478B 4 MS 1212kB 14:09:09

In addition, I have to admire that for almost the same content,. cpp is much smaller than the. c file ......

So I answered on Baidu's website ......

Then continue to think about how to write in C.

The code is compiled, but the program returns 0x4. That is to say, an error is returned ......

The compiler (code: blocks) does not prompt any error or warning. I am too lazy to debug VS2008. So you can find it yourself.

The result is finally found and the array g [10] is defined. However, when using the variable, only g is used. But the funny thing is that it is actually run.

After the change, the program stops running and returns 0, but it is still wrong answer.

Later, I looked at it again. One of my habits led to my cup.

The subject requires a range of courses. As a result, I write a program with the interactive reminder and error correction function in my usual habits, and I am able to judge the input with lots of hands. The result is a cup.

Remove the if. Submitted the following program.

 

# Include <stdio. h> <br/> # include <math. h> </p> <p> int main (void) <br/> {<br/> int c [10], GP [10]; <br/> int co, n, g; <br/> int tg = 0; <br/> int tc = 0; <br/> double GPA; </p> <p> scanf ("% d", & co); <br/> for (n = 0; n <co; n ++) <br/>{< br/> scanf ("% d", & c [n], & g ); <br/> if (0 <= g & 59> = g) <br/> GP [n] = 0; <br/> else if (60 <= g & 69> = g) <br/> GP [n] = 1; <br/> else if (70 <= g & 79> = g) <br/> GP [n] = 2; <br/> else if (80 <= g & 89> = g) <br/> GP [n] = 3; <br/> else GP [n] = 4; <br/>}< br/> for (n = 0; n <co; n ++) <br/> {<br/> tg + = c [n] * GP [n]; <br/> tc + = c [n]; <br/>}< br/> GPA = (double) tg/(double) tc; <br/> GPA = (int) (GPA * pow (0.5) +) /pow (10, 2); <br/> printf ("%. 2lf ", GPA); <br/> return 0; <br/>}

 

The result is:

149200 icelights 1023 Accepted GCC 746B 4 MS 1004kB 14:29:19

Finally passed. However, the. c file is 746B, and the previously written C ++ file is only 478B ...... Cup.

 

Later, I used switch () to rewrite and judge the credit level. The result is as follows:

 

# Include <stdio. h> <br/> # include <math. h> </p> <p> int main (void) <br/> {<br/> int c [10], GP [10]; <br/> int co, n, g, ch; <br/> int tg = 0; <br/> int tc = 0; <br/> double GPA; </p> <p> scanf ("% d", & co); <br/> for (n = 0; n <co; n ++) <br/>{< br/> scanf ("% d", & c [n], & g); <br/> ch = (int) (g/10); <br/> if (ch <6) <br/> {<br/> ch = 5; <br/>}< br/> switch (ch) <br/>{< br/> case 5: GP [n] = 0; break; <br/> case 6: GP [n] = 1; break; <br/> case 7: GP [n] = 2; break; <br/> case 8: GP [n] = 3; break; <br/> case 9: GP [n] = 4; break; <br/> case 10: GP [n] = 4; break; <br/>}< br/> for (n = 0; n <co; n ++) <br/> {<br/> tg + = c [n] * GP [n]; <br/> tc + = c [n]; <br/>}< br/> GPA = (double) tg/(double) tc; <br/> GPA = (int) (GPA * pow (0.5) +) /pow (10, 2); <br/> printf ("%. 2lf ", GPA); <br/> return 0; <br/>}

 

The result is:
149216 icelights 1023 Accepted GCC 907B 4 MS 1004kB 15:53:56

 

However, the file volume is much larger than the multi-if () judgment ...... 907B

 

Thoughts:

 

ACM is really an algorithm test competition. Unlike an interactive program written in general, it does not require verification input and prompts incorrect input. It assumes that the input is compliant with the specifications, so the focus is on algorithm design rather than user interface friendliness (of course I admit, the black and white box written by C won't produce any friendly interface, at least in this popular windows era ).

This reminds me of the emphasis on format and writing when I was doing mathematics, physics, and other science questions in middle school. This may mean that the user interface is friendly. The formula itself is an algorithm.

I hate the XX format when I was in a middle school, but when I went to college and wrote a program, I paid great attention to user interface friendliness. I don't know why there was such a change ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.