最大字符串
#include<stdio.h>
#include<string.h>
#include<math.h>
char a[100][1000];
char *b[100];
int cnt;
int sort (char *x[]){
int maxu=1;
for(int i=2;i<=cnt;i++){
if(strcmp(b[maxu],b[i])<0){
maxu=i;
}
}
return maxu;
}
int main(){
while(gets(a[++cnt])){
b[cnt]=a[cnt];
// printf("%s\n",b[cnt]);
// printf("上一个%s\n",b[cnt-1]);
if(strcmp(a[cnt],"*****")==0)
break;
}
// printf("dsad");
// for(int i=1;i<=cnt;i++){
// printf("%s\n",b[i]);
// }
printf("%s",b[sort(b)]);
}
阶段字符串
#include<stdio.h>
#include<string.h>
char s[100];
int n;
int main(){
scanf("%s",s);
scanf("%d",&n);
char *p;
p=s+n-1;
printf("%s",p);
}
使用指针
#include<stdio.h>
#include<string.h>
char s[100];
int n;
int main(){
scanf("%s",s);
printf("%d",strlen(s));
}
翻转字符串
#include<stdio.h>
#include<string.h>
char s[100];
int n;
void change (char s[]){
char *p;
p=s;
int len=strlen(s);
char *q=&s[len-1];
while(p<=q){
// printf("%c",*q);
char temp;
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
}
}
int main(){
scanf("%s",s);
change(s);
//printf("sadas");
printf("%s",s);
}
猜猜我多长
#include<stdio.h>
#include<string.h>
char s[100];
int n;
int len(char s[]){
char *p=s;
int cnt=0;
while(1){
if(*(p+cnt)=='\0')
break;
else
cnt++;
}
return cnt;
}
int main(){
gets(s);
printf("%d",len(s));
}
用指针输出数组
#include<stdio.h>
#include<string.h>
char s[100];
int n;
int a[3000];
void print(int s[]){
int *p=s+1;
for(int i=0;i<10;i++){
printf("%d ",*(p+i));
}
}
int main(){
for(int i=1;i<=10;i++){
scanf("%d",&a[i]);
}
print(a);
}
字符串滞销
#include<stdio.h>
#include<string.h>
char s[100];
int n;
int a[3000];
int main()
{
gets(s);
printf("%s",s);
}
交换两个数字
#include<stdio.h>
#include<string.h>
int a,b;
void swap(int *x,int *y){
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int main(){
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("%d %d",a,b);
}
这也是
#include<stdio.h>
#include<string.h>
#include<math.h>
struct person{
char name[20];
int count;
};
struct person leader[300];
int main(){
int i,j,max,t;
char v[300];
for(j=0;j<3;j++){
scanf("%s",leader[j].name);
leader[j].count++;
}while(~scanf("%s",v)){
for(int j=0;j<3;j++){
if( strcmp(v,leader[j].name)==0){
leader[j].count++;
}
}
}
max=leader[0].count;t=0;
for(i=0;i<3;i++){
if(leader[i].count>max){
max=leader[i].count;
t=i;}
}
printf("%s",leader[t].name);
return 0;
}
输出坐标
#include<stdio.h>
#include<string.h>
#include<math.h>
struct node{
double x,y;
}a[3];
struct node l;
int main(){
for(int i=1;i<=2;i++){
scanf("%lf%lf",&a[i].x,&a[i].y);
}
l.x=a[1].x-a[2].x;
l.y=a[1].y-a[2].y;
printf("(%.1f, %.1f)",l.x,l.y);
return 0;
}
这是一个
#include<stdio.h>
#include<string.h>
#include<math.h>
struct node{
char s[100];
double a,b,c,aver;
}a[4];
void sort(struct node x[]){
for(int i=1;i<=2;i++){
double maxu =a[i].aver;
int flag=i;
for(int j=i+1;j<=3;j++){
if(a[j].aver>maxu){
maxu=a[j].aver;
flag=j;
}
}
struct node temp=a[i];
a[i]=a[flag];
a[flag]=temp;
}
}
int main(){
for(int i=1;i<=3 ;i++){
scanf("%s%lf%lf%lf",a[i].s,&a[i].a,&a[i].b,&a[i].c);
a[i].aver=(a[i].a+a[i].b+a[i].c)/3;
}
sort(a);
printf("%s %.2f\n",a[1].s,a[1].aver);
return 0;
}
输出学号
#include<stdio.h>
#include<string.h>
#include<math.h>
struct node{
char hao[300];
char name[300];
int score;
}a[5000];
int n;
void sort(struct node x[]){
for(int i=1;i<n;i++){
double maxu =a[i].score;
int flag=i;
for(int j=i+1;j<=n;j++){
if(a[j].score>maxu){
maxu=a[j].score;
flag=j;
}
}
struct node temp=a[i];
a[i]=a[flag];
a[flag]=temp;
}
}
int main(){
while(1){
scanf("%d",&n);
if(n==0)
break;
for(int i=1;i<=n ;i++){
scanf("%s%s%d",a[i].hao,&a[i].name,&a[i].score);
// a[i].aver=(a[i].a+a[i].b+a[i].c)/3;
}
sort(a);
printf("%s %s\n",a[1].hao,a[1].name);
}
return 0;
}
输出日期
#include<stdio.h>
#include<string.h>
#include<math.h>
struct node{
int x,y;
}a[5000];
int n,m,cnt;
void sort(struct node x[]){
for(int i=1;i<n;i++){
int maxu =a[i].x;
int minu=a[i].y;
int flag=i;
for(int j=i+1;j<=n;j++){
if(a[j].x>maxu||(a[j].x==maxu&&a[j].y>minu)){
maxu=a[j].x;
minu=a[j].y;
flag=j;
}
}
struct node temp=a[i];
a[i]=a[flag];
a[flag]=temp;
}
}
int main(){
while(1){
scanf("%d%d",&n,&m);
if(n==0&&m==0)
break;
a[++cnt].x=n;
a[cnt].y=m;
sort(a);
}
for(int i=cnt;i>=1;i--){
printf("%d %d\n",a[i].x,a[i].y);
}
return 0;
}
学生信息
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int n;double sum=0,tt=0;
struct node {
char name[10];
char id[10];
double data;
struct node* next;
};
struct node *creat()
{
struct node *head,*p1,*p2;
p1=p2=(struct node*) malloc(sizeof(struct node));
scanf("%s",p1->name);
while(strcmp(p1->name,"*")!=0)
{
scanf("%s%lf",p1->id,&p1->data);
n++;
if(n==1) head=p1;
else p2->next=p1;
sum+=(p1->data);
p2=p1;
p1=(struct node*) malloc(sizeof(struct node));
scanf("%s",p1->name);
}
p2->next=NULL;
return head;
}
void del(struct node *head,double x)
{
struct node *p1,*p2;
if( head==NULL) return ;
p1=head;
while(p1->next!=NULL){
p2=p1->next;
if(x==p1->next->data){
p1->next=p1->next->next;
}
if(p2->next==NULL&&x==p2->data){
p2=NULL;
break;
}
p1=p2;}
}
void print(struct node *head)
{
struct node *p;
p=head;
if(head!=NULL)
do
{
printf("%s\n",p->name);
p=p->next;
}while(p!=NULL);
}
int main()
{
struct node *head;
head=creat();
tt=sum/n;
struct node *p;
p=head;
while(p!=NULL)
{
if(p->data < tt){
del(head,p->data);
}
p=p->next;
}
print(head);
}
单向列表
#include<stdio.h>
#include<string.h>
int n;
int a[10000];
int main(){
scanf("%d",&n);
while(n--){
int flag=0;
int num=0;
while(1){
scanf("%d",&a[++num]);
if(a[num]==-1)
break;
}
for(int i=1;i<=num;i++){
if(a[i]!=-1&&a[i]%2==0){
flag=1;
printf("%d ",a[i]);
}
}
if(!flag){
printf("NULL");
}
printf("\n");
}
}
链表的交集
#include<stdio.h>
#include<string.h>
int n;
int a[10000];
int num,cnt;
int b[10000];
int c[10000];
int kl;
int main(){
while(1){
scanf("%d",&a[++num]);
if(a[num]==-1)
break;
}
while(1){
scanf("%d",&b[++cnt]);
if(b[cnt]==-1)
break;
}
int head=1;int tail=1;
while(head!=num&&tail!=cnt){
if(a[head]==b[tail]){
c[++kl]=a[head];
head++;
tail++;
}
else{
if(a[head]<b[tail])
head++;
else
tail++;
}
}
if(kl==0){
printf("NULL");
}
for(int i=1;i<=kl;i++){
printf("%d ",c[i]);
}
}
算法2-8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int Elemtype;
typedef struct Node{
Elemtype elem;
struct Node *next;
}Node, *Linklist;
Status visit(Elemtype e)
{
printf("%d", e);
return OK;
}
Status InitLinklist(Linklist *L)
{
*L = (Linklist)malloc(sizeof(Node));
if(!(*L)) return ERROR;
(*L)->next = NULL;
return OK;
}
Status ListInsert(Linklist *L, int i, Elemtype e)
{
int j = 1;
Linklist p, s;
p = *L;
while(p && j < i){
p = p->next;
++j;
}
if(!p || j > i) return ERROR;
s = (Linklist)malloc(sizeof(Node));
s->elem = e;
s->next = p->next;
p->next = s;
return OK;
}
Status ListDelete(Linklist *L, int i, Elemtype *e)
{
int j = 1;
Linklist p, q;
p = *L;
while(p->next && j < i){
p = p->next;
++j;
}
if(!(p->next) || j > i)
return ERROR;
q = p->next;
p->next = q->next;
*e = q->elem;
free(q);
return OK;
}
int ListLength(Linklist L)
{
int i;
Linklist p;
i = 0;
p = L->next;
while(p){
i++;
p = p->next;
}
return i;
}
Status ListEmpty(Linklist L)
{
if(L->next) return FALSE;
else return TRUE;
}
Status ClearList(Linklist *L)
{
Linklist p, q;
p = (*L)->next;
while(p){
q = p->next;
free(p);
p = q;
}
(*L)->next = NULL;
return OK;
}
Status GetElem(Linklist L, int i, Elemtype *e)
{
Linklist p;
int j;
j = 1;
p = L->next;
while(p && j < i){
++j;
p = p->next;
}
if(!p || j > i) return ERROR;
*e = p->elem;
return OK;
}
int ElemLocate(Linklist L, Elemtype e)
{
int i;
i = 0;
Linklist p;
p = L->next;
while(p){
i++;
if(p->elem == e)
return i;
p = p->next;
}
return 0;
}
Status ListTraverse(Linklist L)
{
Linklist p;
p = L->next;
while(p && p->next != NULL){
visit(p->elem);
printf(" ");
p = p->next;
}
visit(p->elem);
printf("\n");
return OK;
}
int main()
{
Linklist L;
InitLinklist(&L);
int n;
int nn;
int num;
int i = 1;
int j;
char str[100];
int loc;
Elemtype e;
scanf("%d", &n);
for(j = 0; j < n; j++){
scanf("%d", &num);ListInsert(&L, 1, num);
}
scanf("%d", &nn);
for(j = 0; j < nn; j++){
scanf("%s", str);
if(strcmp (str, "show") == 0){
if(ListEmpty(L)){
printf("Link list is empty\n");
continue;
}
ListTraverse(L);
}
else if(strcmp (str, "delete") == 0){
scanf("%d", &loc);
if(ListDelete(&L, loc, &e))
printf("delete OK\n");
else
printf("delete fail\n");
}
else if(strcmp (str, "get") == 0){
scanf("%d", &loc);
if(GetElem (L, loc, &e)){
printf("%d\n", e);
}
else
printf("get fail");
}
else if(strcmp (str, "insert") == 0){
scanf("%d%d", &loc, &num);
if(ListInsert (&L, loc, num))
printf("insert OK\n");
else
printf("insert fail\n");
}
}
return 0;
}