北理2017
作者:互联网
#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
//1.让输入10个整数然后求出平均值之后与10个整数比较然后输出最接近平均数的整数
int main()
{
int m = 10;
int a[10];
int res = 0;
for(int i = 0; i<m;i++)
{
scanf("%d",&a[i]);
res+=a[i];
}
double avg = res/10.0;
double min = abs(avg-a[0]);
int flag = 0;
for(int i = 0;i<m;i++)
{
double tmp = abs(avg - a[i]);
if(min > tmp)
{
min = tmp;
flag = i;
}
}
printf("%lf\n",avg);
printf("%d",a[flag]);
return 0;
}
//2.2001-2016中的素数
int main2()
{
for(int i = 2001; i<=2016; i++)
{
int flag = 0;
for(int j = 2;j<i;j++)
{
if(i % j == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("%d ",i);
}
}
return 0;
}
//3.求n项之和
//1 2 4 7 11 16 21
int main3()
{
int n;
int res = 0;
int x = 1;
scanf("%d",&n);
for(int i = 1;i<=n;i++)
{
res+=x;
x+=i;
}
printf("%d",res);
return 0;
}
//4.递归方法输入n求2n!
int main4()
{
int n;
scanf("%d",&n);
int res = 2*n;
long long s = 1;
for(int i = 1;i<=res;i++)
{
s = s*i;
}
printf("%d",s);
return 0;
}
//建立一个双向循环链表,可以输入若干个整数,然后让你正向输出,逆向输出
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node*pre;
struct node*next;
}linkNode,*linkList;
//链表初始化
linkList initLinkList()
{
linkList head=(linkList)malloc(sizeof(linkNode));
if(!head)
{
printf("动态内存分配失败!\n");
exit(0);
}
head->next=head;
head->pre=head;
printf("链表初始化成功!\n");
return head;
}
linkList createNode(int val)
{
linkList head=(linkList)malloc(sizeof(linkNode));
head->data = val;
head->pre = nullptr;
head->next = nullptr;
return head;
}
void insertLinkListByNext(linkList l,int x)
{
linkList newNode = createNode(x);
linkList lastNode = l;
while(lastNode->next != l)
{
lastNode = lastNode->next;
}
l->pre = newNode;
newNode->next = l;
lastNode->next = newNode;
newNode->pre = lastNode;
}
//正序输出
void printList(linkList l)
{
linkList p = l->next;
while(p != l)
{
printf("%d",p->data);
p = p->next;
}
printf("\n");
}
//逆序输出
void printList_reverse(linkList l)
{
linkList p = l->pre;
while(p != l)
{
printf("%d",p->data);
p = p->pre;
}
printf("\n");
}
int main5()
{
linkList head = initLinkList();
insertLinkListByNext(head,1);
insertLinkListByNext(head,2);
insertLinkListByNext(head,4);
insertLinkListByNext(head,3);
insertLinkListByNext(head,5);
printList(head);
printList_reverse(head);
return 0;
}
标签:pre,北理,head,int,next,linkList,printf,2017 来源: https://www.cnblogs.com/akmfwei/p/13725500.html