数组输出,和最高分相差不超过10都为高分,统计所有高分的和,不知道程序问题在哪?
基于new bing的编写:
#include <iostream>
using namespace std;
int main() {
int n, arr[101], maxv = 0; // n为数组大小,arr为数组,maxv表示最高分,初值为0
long long ans = 0; // ans表示所有高分的和,初值为0
cin >> n;
for (int i = 0; i < n; i++) { // 输入数组元素
cin >> arr[i];
if (arr[i] > maxv) { // 更新最高分
maxv = arr[i];
}
}
for (int i = 0; i < n; i++) { // 统计所有高分的和
if (maxv - 10 <= arr[i]) { // 判断是否为高分
ans += arr[i]; // 若是,则将其加入答案之中
}
}
cout << ans; // 输出答案
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:如果我们对康威的生命规则进行一些改变:
那么根据上面的规则我们修改代码如下:
#include<stdio.h>
#include <unistd.h>
#include<time.h>
#include<memory.h>
#include<string>
#include <cstdlib>
using namespace std;
int life[80][400];
int lifen[80][400];
int lifes[80][400];
int period[80][400];
int mm, nn,s,times=0,ry,rx;
int main(){
for (int sj = 0; sj < 80; sj++) {//初始化所有生命为死亡
for (int sm = 0; sm < 400; sm++) {
life[sj][sm] =0;
lifen[sj][sm] = 0;
lifes[sj][sm] = 0;
period[80][400];
}
}
life[40][120]=1;period[40][120]=5;lifes[40][120]=1;//再一个点初始化一个生命
srand((unsigned)time(NULL));
int m,t,e;
while(1){
if(life[ry=rand()%80][rx=rand()%400]==1){//生命会随机的发生位移
life[ry][rx]==0;
life[ry=rand()%80][rx=rand()%400]=1;period[ry][rx]=5;lifes[ry][rx]=1;
}
for(int y=1;y<79;y++)
{//开始遍历数组
for(int x=1;x<399;x++)
{
if(life[y][x]==1)
{
m=0;
m = life[y - 1][x - 1] + life[y - 1][x] + life[y - 1][x + 1] + life[y][x - 1] + life[y][x + 1] + life[y + 1][x - 1] + life[y + 1][x + 1] + life[y + 1][x];
if(m==0){
lifen[y][x]=1;
}
else if(m==1){
lifen[y][x]=1;
}
else if(m==2){
lifen[y][x]=1;
}
else {lifen[y][x]=0;period[y][x]==0;}
if(period[y][x]!=0)
period[y][x]=period[y][x]-1;//生命周期减少
else if(period[y][x]==0)lifen[y][x] = 0;
}
else if (life[y][x] == 0)
{
m = 0;
mm=(rand()%2);
if(mm==1){
m = lifes[y - 1][x - 1] + lifes[y - 1][x] + lifes[y - 1][x + 1] + lifes[y][x - 1] + lifes[y][x + 1] + lifes[y + 1][x - 1] + lifes[y + 1][x +1] + lifes[y + 1][x];
if(lifes[y - 1][x - 1]==1)lifes[y - 1][x - 1]=0;
else if(lifes[y - 1][x]==1)lifes[y - 1][x]=0;
else if(lifes[y - 1][x+1]==1)lifes[y - 1][x+1]=0;
else if(lifes[y][x-1]==1)lifes[y][x-1]=0;
else if(lifes[y][x+1]==1)lifes[y][x+1]=0;
else if(lifes[y+1][x-1]==1)lifes[y + 1][x-1]=0;
else if(lifes[y+1][x]==1)lifes[y + 1][x]=0;
else if(lifes[y+1][x+1]==1)lifes[y + 1][x+1]=0;
}
if (m != 0) {
lifen[y][x] = 1;period[y][x]=5;//新生命诞生,初始化生命周期
}
else {lifen[y][x]=0;period[y][x]==0;}
}
}
}
for(int my=0;my<80;my++)
{
for(int mx=0;mx<400;mx++)
{
life[my][mx]=lifen[my][mx];
lifes[my][mx]=lifen[my][mx];
}
}
printf("start print:%d\n",times);
for (int py = 0; py < 80; py++)
{
for (int px = 0; px < 400; px++)
{
if (life[py][px] == 0) {
printf("\033[47m*\033[0m");
}
else {
printf("\033[41m*\033[0m");
}
}
printf("\n");
}
usleep(300000);
system("clear");
times++;
}
}
最后来看看效果图吧: