multiple_total A method that calculates the total value of multiples of argument a that are less than or equal to argument b
def multiple_total(a, b):
if a>0 and b>=0:
i=0
total=0
while a*i <= b:
total += a*i
i += 1
else:
total = 0
return total
FizzBuzz A method that returns a string of conditions given a given integer n
def fizzbuzz(n):
if n % 3 == 0 and n % 5 == 0:
return 'FizzBuzz'
elif n % 3 == 0:
return 'Fizz'
elif n % 5 == 0:
return 'Buzz'
else:
return str(n)#Don't just n
A program that reads the information on moving in and out of the official residence and outputs the number of residents in each room
#include <stdio.h>
int main()
{
int a[100][100];
int i,j,input;
int n,m,b[100],sum=0;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++){
scanf("%d",&b[i]);
}
for(i=0;i<n;i++){
sum=0;
for(j=0;j<m;j++){
sum+=a[i][j]*b[j];
}
printf("%d\n",sum);
}
return 0;
}
A program that takes n cards as input and outputs the missing cards
#include<stdio.h>
int main()
{
int t[4][14];
int n, i,j,c;
char m, d;
for(i=0; i < 4; i++){
for(j=0; j < 14; j++){
t[i][j]=0;
}
}
scanf("%d", &c);
for(i = 0; i < c; i++){
scanf("%c %d", &m, &n);
if(m == 'S'){
t[0][n] = 1;
}
else if(m == 'H'){
t[1][n] = 1;
}
else if(m == 'C'){
t[2][n] = 1;
}
else if(m == 'D'){
t[3][n] = 1;
}
}
for(j=1; j < 14; j++){
if(t[0][j] != 1){
printf("S %d\n", j);
}
}
for(j=1; j < 14; j++){
if(t[1][j] != 1){
printf("H %d\n", j);
}
}
for(j=1; j < 14; j++){
if(t[2][j] != 1){
printf("C %d\n", j);
}
}
for(j=1; j < 14; j++){
if(t[3][j] != 1){
printf("D %d\n", j);
}
}
return 0;
}
-__Init__ (self)
is an initialization function that is automatically called when the class is actually used (the constructor is the part named __init__
).
-It has self
as an argument and represents the entity of the class itself