pat 1061 乙級
1061 判斷題 (15 分)
判斷題的評判很簡單,本題就要求你寫個簡單的程式幫助老師判題並統計學生們判斷題的得分。
輸入格式:
輸入在第一行給出兩個不超過 100 的正整數 N 和 M,分別是學生人數和判斷題數量。第二行給出 M 個不超過 5 的正整數,是每道題的滿分值。第三行給出每道題對應的正確答案,0 代表“非”,1 代表“是”。隨後 N 行,每行給出一個學生的解答。數字間均以空格分隔。
輸出格式:
按照輸入的順序輸出每個學生的得分,每個分數佔一行。
輸入樣例:
3 6
2 1 3 3 4 5
0 0 1 0 1 1
0 1 1 0 0 1
1 0 1 0 1 0
1 1 0 0 1 1
輸出樣例:
13
11
12
下邊給出思路:先輸入三行數;
在把下邊三行看成一個vector容器,對容器進行迴圈輸入,判斷新輸入的temp
和 judge[j]是否相等
sum=sum+score[j];
在定義sum和score[i]相加
for(int i = 0; i < n; i++) { int total = 0; for(int j = 0; j < m; j++) { int temp; cin>>temp; //scanf("%d", &temp); if(temp == ans[j]) total += score[j]; }
下列給出迴圈,原諒小白無法從腦子計算出迴圈 當i=0 j=0 輸入temp 0 1 1 0 0 1 時 ,if(0==ans[0]) 此時ans[0]=0;
i=1 代表著第一個同學 total=score[0]+total=2+0=2 j++=1;繼續j迴圈
j<m=6;if 1==ans[1] 錯誤, 跳出if迴圈 j++=2
j<m=6 0==ans[2] 錯誤 跳出if迴圈 j++=3
------------------ 1== ans[5]正確 執行 total=score[5] j>m 跳出j迴圈
i=2 同學省略
i=3 同學省略
最後輸出total,下面給出程式碼,個人感覺還是cin好使。。。。
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
// cin>>n>>m;
scanf("%d%d", &n, &m);
vector<int> score(m), ans(m);
for(int i = 0; i < m; i++)
//scanf("%d", &score[i]);
cin>>score[i];
for(int i = 0; i < m; i++)
// scanf("%d", &ans[i]);
cin>>ans[i];
for(int i = 0; i < n; i++) {
int total = 0;
for(int j = 0; j < m; j++) {
int temp;
cin>>temp;
//scanf("%d", &temp);
if(temp == ans[j])
total += score[j];
}
cout<<total<<endl;
// printf("%d\n", total);
}
return 0;
}
//printf("%d\n", total);
cout<<total;
}
return 0;
}