BASIC-4 数列特征
问题描述:
BASIC-4 数列特征
C语言代码:
#include <stdio.h>
int main(){
int a;
scanf("%d",&a);
int arr[a];
int max,min,sum=0;
int i;
for(i=0;i<a;i++){
scanf("%d",&arr[i]);
}
max=arr[0];
min =arr[0];
for(i=0;i<a;i++){
if(max<=arr[i]){
max=arr[i];
}
if(min>=arr[i]){
min=arr[i];
}
sum=arr[i]+sum;
}
printf("%d\n%d\n%d",max,min,sum);
return 0;
}
java代码:
public class Main {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int a=input.nextInt();
int arr[]=new int [a];
int max,min,sum=0;
for(int i=0;i<arr.length;i++){
arr[i] = input.nextInt();
}
max=arr[0];
min =arr[0];
for(int i=0;i<arr.length;i++){
if(max<=arr[i]){
max=arr[i];
}
if(min>=arr[i]){
min=arr[i];
}
sum=arr[i]+sum;
}
System.out.println(max+"\n"+min+"\n"+sum);
}
}
BASIC-5 查找整数
问题描述:
BASIC-5 查找整数
C语言代码:
#include <stdio.h>
int main(){
int a,b,i;
scanf("%d",&a);
int arr[a];
for(i=0;i<a;i++){
scanf("%d",&arr[i]);
}
scanf("%d",&b);
for(i=0;i<a;i++){
if(b==arr[i]){
printf("%d",i+1);
return 0;
}
}
printf("-1");
return 0;
}
Java代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int a=input.nextInt();
int arr[]=new int [a];
for(int i=0;i<arr.length;i++){
arr[i] = input.nextInt();
}
int b =input.nextInt();
ss(a,arr,b);
}
static int ss(int a,int[] arr,int b){
for(int i=0;i<arr.length;i++){
if(b==arr[i]){
System.out.println(i+1);
return 0;
}
}
System.out.println("-1");
return 0;
}
}
BASIC-5 杨辉三角
问题描述:
BASIC-5杨辉三角
C语言代码:
#include <stdio.h>
#define MAX 35
int main(){
int a,i,j;
scanf("%d",&a);
int arr[MAX][MAX];
for(i=0;i<a;i++){
for(j=0;j<=i;j++){
if(i==0||j==0||i==j){
arr[i][j]=1;
}else{
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
}
}
for(i=0;i<a;i++){
for(j=0;j<=i;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Java代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int i,j;
Scanner input =new Scanner(System.in);
int a=input.nextInt();
int arr[][]=new int [a][];
for(i=0;i<arr.length;i++){
arr[i]=new int[i+1];
for(j=0;j<=i;j++){
if(i==0||j==0||i==j){
arr[i][j]=1;
}else{
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
}
}
for(i=0;i<arr.length;i++){
for(j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
网友评论