第四章 数组
4 AcWing 743 数组的行
a, b, c = int(input()),input(),[[float(x) for x in input().split()] for i in range(12)]
print('%.1lf'%(sum(c[a]) if b=='S' else sum(c[a])/12))
#分项定义语句
#基础数据读取与转换 列表类型使用及简写
5 AcWing 745 数组的右上半部分
a, b ,c= input(),[[float(y) for y in input().split()] for x in range(12)],0.
for i in range (12):
for j in range(12):
if (j>i):
c+=b[i][j];
# c = sum([sum([i[x][y] for y in range(x+1,12)]) for x in range(12)])//该种写法也可以
print("%.1lf"%(c if a=='S' else c/((1+11)/2*11)))
6 AcWing 数组的左上半部分
a, b, ans = input(),[[float(x) for x in input().split()] for x in range(12)], 0.0
ans = sum([sum([b[x][y] for y in range(0,11-x)]) for x in range(12)])
print("%.1lf"%(ans if (a=='S') else ans/66))
7 AcWing 数组的上方区域
a, b, ans = input(),[[float(y) for y in input().split()] for i in range(12)], 0.
for i in range(12):
for j in range(12):
if(j>i and i+j <11):
ans += b[i][j]
# ans = sum([sum([b[i][j] for j in range(i+1,11-i)]) for i in range (5)])
# print("%.1lf"%(ans if a=='S' else ans/30))
print("%.1lf"%(ans if a=='S' else ans/30))
8 AcWing 数组的左方区域
a, b, c = input(), [[float(y) for y in input().split()] for i in range(12)] ,0.
#c = sum([sum([b[i][j] for i in range(j+1, 11-j)]) for j in range(5)])
for i in range(12):
for j in range(12):
if(i>j and i+j <11):
c+=b[i][j]
print("%.1f"%(c if a=='S'else c/30))
9 763.平方矩阵
#include<iostream>
#include<algorithm>
using namespace std;
bool minmap(int a, int b)
{
return a < b;
}
int main()
{
int n;
while(cin>>n){
if(n==0) break;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
// int d=min(min(i+1,j+1),min(n-i,n-j));
int d = min({i+1,j+1,n-i,n-j});//min()函数用法
cout<<d<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
n=int(input())
while(n):
for i in range(n):
for j in range(n):
print("%d "%(min(i+1, j+1,n-i,n-j)),end='')
print()
print()
n = int(input())
10 740数组变换
#方法1 注意两种读取方式数据类型 方法2更优
a = [[int(y) for y in input().split()] for i in range(20)]
b = list(reversed(a))
for i in range(20):
print("N[%d] = %d"%(i,b[i][0]))
#方法2
a = list(reversed([int(input()) for x in range(20)]))
for i in range(20):
print("N[%d] = %d"%(i,a[i]))
11 741 斐波那契数列
#include<iostream>
using namespace std;
const int N = 100;
int main()
{
int n;
long int a[N]={0,1};
for(int i=2;i<N;i++)
{
a[i]=a[i-1]+a[i-2];
}
cin>>n;
while(n--)
{
int i;
cin>>i;
printf("Fib(%d) = %ld\n",i,a[i]);
}
return 0;
}
# 第一种方法
n = int(input())
b = [0,1]
for i in range(2,62):
b.append(b[i-1]+b[i-2])
for i in range(n):
a = int(input())
print("Fib(%d) = %d"%(a,b[a]))
# 第二种方法
for x in range(int(input())):
a, d = int(input()), 5**0.5
print("Fib(%d) = %d"%(a,(1/d)*(((1+d)/2)**a-((1-d)/2)**a)))
//出现问题
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double c = sqrt(5);
int n;
cin>>n;
while(n--)
{
int a;
cin>>a;
//cout<<(pow((1.0+c)/2.0,a)-pow((1.0-c)/2.0,a))*1.0/c<<endl;
printf("Fib(%d) = %ld\n",a,int((pow((1.0+c)/2.0,a)-pow((1.0-c)/2.0,a))*1.0/c));
//printf("Fib(%d) = %ld\n",a,(1.0/c)*((pow((1.0+c)/2.0,a)-pow((1.0-c)/2.0,a))));
}
}
//正常
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double c = sqrt(5);
int n;
cin>>n;
while(n--)
{
int a;
cin>>a;
long long int x =(pow((1.0+c)/2.0,a)-pow((1.0-c)/2.0,a))*1.0/c;
//cout<<(pow((1.0+c)/2.0,a)-pow((1.0-c)/2.0,a))*1.0/c<<endl;
printf("Fib(%d) = %ld\n",a,x);
//printf("Fib(%d) = %ld\n",a,(1.0/c)*((pow((1.0+c)/2.0,a)-pow((1.0-c)/2.0,a))));
}
}
742 最小数和它的位置
- 如何读取数据 并将单行数据转换成整数类型
- Python中 min 函数 index 函数的用法
n = input()
a = [int(x) for x in input().split()]
print("Minimum value: %d\nPosition: %d"%(min(a),a.index(min(a))))
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> a;
int n,b;
cin>>n;
for(int i =0;i<n;i++){
cin>>b;
a.push_back(b);
}
int min = *min_element(a.begin(),a.end());
int index = min_element(a.begin(),a.end())-a.begin();
printf("Minimum value: %d\nPosition: %d",min,index);
return 0;
}
数组中的列
#include<iostream>
using namespace std;
const int N=12;
double a[N][N],sum=0;
int main()
{
int n;
char c;
cin>>n>>c;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++){
cin>>a[i][j];
}
}
for(int j=0;j<N;j++){
sum+=a[j][n];
}
if(c=='S')printf("%.1lf",sum);
else printf("%.1lf",sum/12);
return 0;
}
a,b,c,d=int(input()),input(),[[float(y) for y in input().split()] for i in range(12)],0.
x= sum([c[i][a] for i in range(12)])
print("%.1f"%(x if b=='S' else x/12))
数组的右下半部分
a,b=input(),[[float(y) for y in input().split()] for i in range(12)]
ans = sum([sum([b[i][j] for j in range(12-i,12)]) for i in range(12)])#(12-i,12)
print("%.1f"%(ans if a=='S' else ans/66))
数组的左下半部分
a,b =input(),[[float(y) for y in input().split()] for i in range(12)]
c = sum([sum([b[i][j] for j in range(i)]) for i in range(12)])
print("%.1lf" %(c if a=='S' else c/66))
数组的下方区域
数组的右方区域
平方矩阵2
a=int(input())
while(a):
#print(a)
a=int(input())
for i in range(a):
for j in range(a):
print("%d"%(abs(i-j)+1),end=' ')
print()
print()
## 方法二 注意比较
while 1:
n=int(input())
if not n:
break
ans=[[0 for i in range(n)] for i in range(n)]
i,j,num=0,0,1
while i<n:
ans[i][j]=num
num+=1
j+=1
if j==n:
j=i+1
i+=1
num=1
for i in range(1,n):
for j in range(i):
ans[i][j]=ans[j][i]
for i in range(n):
print(*ans[i],sep=" ")
print()
//方法一
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n,n!=0){
int a[n][n]={0};
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
a[i][j]=j-i+1;
}
for(int j=0;j<i;j++){
a[i][j]=a[j][i];
}
}
// for(int i=1;i<n;i++){
// for(int j=0;j<i;j++){
// a[i][j]=8;
// }
// }
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
}
//方法二
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n,n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<abs(i-j)+1<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
平方矩阵3
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
int n,x;
while(cin>>n,n)
{
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
x = pow(2,i+j);
cout<<x<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
756 蛇形矩阵