開始前有必要說一下,這才第二題就碰到了爛尾題,以自己開始的思路交了n次,錯了n詞,最後才19分,後來看了一下大佬的c++代碼(盡然沒有c的代碼),還好c和c++的差別不是特別大,仔細琢磨一遍後突然發現很多地方可以改進,整理思路在此嘗試終於AC,這才第二題啊。。。。 This time, you ar ...
開始前有必要說一下,這才第二題就碰到了爛尾題,以自己開始的思路交了n次,錯了n詞,最後才19分,後來看了一下大佬的c++代碼(盡然沒有c的代碼),還好c和c++的差別不是特別大,仔細琢磨一遍後突然發現很多地方可以改進,整理思路在此嘗試終於AC,這才第二題啊。。。。
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤1000.
Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
題目分析:
兩行數據A和B,每一行的第一個數字 K 是它的非零項(即a不等於0),接下來的2*K個數字由K個數據N和K個數據a組成,N是指數(exponents),即下標,a是繫數(coefficients),輸入的例子中第一行,2是2個非零數,然後a1=2.4,a0=3.2,第二行,2個非零數,a2=1.5,a1=0.5。現在將A,B兩行下標相等的數字相加,輸出所有非零數個數、數值和其下標,即3個非零數,a2=1.5,a1=2.9(2.4+0.5),a0=3.2。輸出結尾不得有多餘空格。
個人想法:
錯誤想法(正確的在下麵):開始筆者是想劃分兩個數組,分別存放下標N[]、存放數值a[],賦值時,即用a[N[i]]來將一個值存放在迴圈中N數組存儲的下標位置,例如存入4.3,N[1]=6,a[N[1]]=a[6]=4.3,賦值A行全部結束後,用tmp賦值B行時,直接加上即·a[xx]+=tmp,最後遍歷找非零項,記錄個數,輸出非零項就好了,下麵是代碼:
1 #include <stdio.h> 2 #define A 10001; 3 int K;//非0值 4 int N[1001];//存放下標N 5 double a[10001], b[10001]; 6 7 int main() 8 { 9 double tmp; 10 scanf("%d", &K); 11 for (int i = 0; i < K; i++) 12 { 13 scanf("%d%lf", &N[i], &tmp); 14 a[N[i]] = tmp; 15 }//第一行輸入的數 16 scanf("%d", &K); 17 for (int i = 0; i < K; i++) 18 { 19 scanf("%d%lf", &N[i], &tmp); 20 a[N[i]] += tmp; 21 } 22 int p = 0; 23 if (a[0])p = 1; 24 for (int i = 0; i < 10001; i++) 25 { 26 if (a[i] && N[i] != 0) 27 ++p; 28 } 29 printf("%d", p); 30 for (int i = 10000; i >= 0; i--) 31 { 32 if (a[i] != 0) 33 printf(" %d %.1lf", i, a[i]); 34 } 35 printf("\n"); 36 return 0; 37 }
由於自己的這個想法只能拿到19分,這裡就不貼出自己前面錯誤改進後的代碼了。也請給位看官有興趣的幫忙指正我的代碼漏洞。
<----------接下來是滿分代碼------------>
其實代碼總體思路是不變的,但是大佬的代碼更加簡潔、清楚,原先的下標數組N直接變成常量賦值,即a[N],整體過程也不再那麼抽象,甚至用多個數組標記A、B輸入數組和最後的輸出數組。通過代碼更容易看出:
1 #include <stdio.h> 2 #define A 10001; 3 4 int main() 5 { 6 double t;//數值 7 int count=0; 8 int K;//非0值 9 int N;//下標值 10 double a[1001], b[1001],c[1001];//兩個輸入值和最後輸出數組 11 memset(a, 0.0, sizeof(a)); 12 memset(b, 0.0, sizeof(b)); 13 memset(c, 0.0, sizeof(c));//初始化,個人認為設為全局變數就行,不用初始化了 14 scanf("%d", &K); 15 for (; K > 0; K--) { 16 scanf("%d%lf",&N,&t); 17 a[N] = t; 18 } 19 scanf("%d", &K); 20 for (; K > 0; K--) { 21 scanf("%d%lf", &N, &t); 22 b[N] = t; 23 }//A、B的數值輸入 24 for (int i=0; i<1001; i++) { 25 if (a[i] != 0 || b[i] != 0) {//遍歷所有範圍,如果輸入數組存在非0項,則放入輸出數組 26 c[i] = a[i] + b[i]; 27 if(c[i]!=0) 28 count++; 29 } 30 } 31 printf("%d", count);//輸出非0項數的數目 32 for (int i =1000 ; i >=0; i--) { 33 if (c[i] != 0) 34 printf(" %d %.1lf", i, c[i]); 35 } 36 37 return 0; 38 }
滿分通過。
總結:
由於一些測試點看不到,所有原先最後那沒通過還是不清楚,看了幾次也看不出來哪裡有遺漏的地方,暫時也只能這樣了,功夫不到家,差的太遠。還有照例英語。