2022.11.18 AM 11:50 by CBJ
來源 : https://zerojudge.tw/ShowProblem?problemid=a058 出題者 : 雄中公假社 標籤 : 條件判斷、數學 難易度 : 1
解題想法 : 分別求出輸入的每個數字num = 0(mod 3), num = 1(mod 3), num = 2(mod 3)的次數即可,使用if就可以完成這個工作。
//C language
//solution link(含註解):
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int x=0,y=0,z=0;
int a[50005];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++){
int mod=a[i]%3;
if(mod==0) x++;
else if(mod==1) y++;
else z++;
}
printf("%d %d %d\n",x,y,z);
return 0;
}

//C++ language
//solution link(含註解):
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin>>n;
int x=0,y=0,z=0;
vector<int>a;
for(int i=0;i<n;i++){
int k;
cin>>k;
a.push_back(k);
}
for(int i=0;i<n;i++){
int mod=a[i]%3;
if(mod==0) x++;
else if(mod==1) y++;
else z++;
}
cout<<x<<" "<<y<<" "<<z<<"\n";
return 0;
}

## Python language
## solution link(含註解):
n=int(input())
a=[];
x=y=z=0
for i in range(n):
a.append(int(input()))
for i in a:
mod=i%3
if mod==0: x+=1
elif mod==1: y+=1
else: z+=1
print(x,y,z)

發佈留言