很多初二的朋友们最近是不是都学到统计那一个单元了(反正我们学到那儿了)
所以我来分享一个好东西
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define ll long long
#define rll register long long
using namespace std;
char s[10000005];
ll arr[10000005],cnt;
inline void gotoxy(rll x,rll y){
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
inline void color(rll a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
inline void zhushi(){
color(3);
gotoxy(35,8);
cout<<"注:";
gotoxy(35,10);
cout<<"由于C++精度问题,请勿输入小数。";
gotoxy(35,12);
cout<<"如果您实在需要计算小数,出门左转打开计算器。";
gotoxy(35,14);
cout<<"本程序所有结果均按保留两位小数处理。";
gotoxy(35,16);
cout<<"当计算完成后:";
gotoxy(37,18);
cout<<"按下'\\'键清屏重新输入。";
gotoxy(37,20);
cout<<"双击'`'键退出程序。";
gotoxy(37,22);
cout<<"(这两个符号均为英文符号)";
gotoxy(0,0);
}
inline void shuru(){
printf("请在一行内输入您的数据,用空格分开:\n");
rll i=0,tmp,len;
bool flag;
gets(s);len=strlen(s);
while(i<len){
if(s[i]==' ')++i;
flag=0;
if(s[i]=='-')flag=1,++i;
if('0'<=s[i]&&s[i]<='9'){
tmp=0;
while(s[i]!=' '&&i<len){
tmp=tmp*10+s[i++]-'0';
}
arr[++cnt]=flag?-tmp:tmp;
}
}
}
struct Node{
ll num,id;
}kl[10005];
inline bool cmp(Node a,Node b){
return a.id>b.id;
}
inline void tongji(){
bool flag;
rll i,ans,crt=0;
double num=0,lop;
sort(arr+1,arr+1+cnt);
ans=arr[1];
kl[++crt].num=arr[1];kl[crt].id=1;
for(i=2;i<=cnt;i++){
ans+=arr[i];
if(arr[i]==kl[crt].num)kl[crt].id++;
else {
kl[++crt].num=arr[i];
kl[crt].id=1;
}
}
sort(kl+1,kl+1+crt,cmp);
lop=ans*1.0/cnt;
printf("平均数:%.2f\n",lop);
printf("中位数:%.2f\n",cnt%2==1?arr[cnt/2+1]*1.0:(arr[cnt/2]+arr[cnt/2+1])*1.0/2);
printf("众数:");
flag=0;
for(i=2;i<=crt;i++){
if(kl[i].id!=kl[1].id)flag=1;
}
if(flag)for(i=1;i<=crt;i++){
if(kl[i].id==kl[1].id)printf("%.2f ",kl[i].num*1.0);
}else printf("无");
printf("\n");
printf("极差:%.2f\n",arr[cnt]*1.0-arr[1]);
printf("方差:");
for(i=1;i<=cnt;i++)num+=(arr[i]*1.0-lop)*(arr[i]*1.0-lop);
lop=num/cnt;
printf("%.2f\n",lop);
printf("标准差:%.2f",sqrt(lop));
}
int main(){
char ch;
while(true){
zhushi();
shuru();
tongji();
while(true){
ch=getch();
if(ch=='\\'){
system("cls");
break;
}
else if(ch=='`'){
return 0;
}
}
}
}
本人花了一上午写出来的qwq