開始前有必要說一下,這才第二題就碰到了爛尾題,以自己開始的思路交了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 }

滿分通過,
總結:
由于一些測驗點看不到,所有原先最后那沒通過還是不清楚,看了幾次也看不出來哪里有遺漏的地方,暫時也只能這樣了,功夫不到家,差的太遠,還有照例英語,



轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518722.html
標籤:其他
