C语言题目14

//实现输入任意两个若干位的整数,进行加法,计算并返回加和后的结果。
//(注意两个整数可以足够大,以至于double都会溢出。只要系统内存够用,
//此加法都要正常运行)
#include"stdio.h"
#include"stdlib.h"
typedef struct node{
  int x;
  struct node *next;
  struct node *before;
}llnode;

int charToNumber(char c)
{
  return c-48;
}

void getNumber(llnode* tail)
{
  char c;
  llnode *p;
  llnode *q;
  q = tail;
  printf("请输入一个整数, 输入完成后敲击回车:");
  while(1)
    {
      c = getchar();
      if(c == '\n')
	{
	  break;
	}
      else
	{
	  p = (llnode *)malloc(sizeof(llnode));
	  p->x = charToNumber(c);
	  p->before = NULL;
	  if(tail->x == 0)
	    {
	      tail->before = p;
	      p->next = tail;
	      tail->x = tail->x+1;
	    }
	  else
	    {
	      q = tail->before;
	      q->next = p;
	      p->before = q;
	      p->next = tail;
	      tail->before = p;
	      q = p;
	      tail->x = tail->x+1;
	    }
	}
    }
}

void addNumber(llnode* ctail, llnode* atail, llnode* btail)
{
  int i=0;
  int alength = atail->x;
  int blength = btail->x;
  llnode *pshort;
  llnode *plong;
  llnode *ps;
  llnode *pl;
  llnode *p;
  llnode *q;
  int a;
  int b;
  int nextc;
  int thisc;
  int d;
  if(alength>blength)
    {
      pshort = btail;
      plong = atail;
    }
  else
    {
      pshort = atail;
      plong = btail;
    }
  ps = pshort->before;
  pl = plong->before;
  while(i<pshort->x)
    {
      thisc = nextc;
      if(ps->x + pl->x >=10)
	{
	  nextc = 1;
	  d = (ps->x + pl->x)%10;
	}
      else
	{
	  nextc = 0;
	  d = (ps->x + pl->x);
	}
      ps = ps->before;
      pl = pl->before;
      p = (llnode *)malloc(sizeof(llnode));
      p->x = d+thisc; 
      p->before = NULL;
      if(ctail->x == 0)
	{
	  ctail->before = p;
	  p->next = ctail;
	  ctail->x = ctail->x+1; 
	  q = p;
	}
      else
	{
	  p->next = q;
	  q->before = p;
	  q = p;
	  ctail->x = ctail->x+1;
	}
      i = i+1;
    }
  while(i<plong->x)
    {
      if(i == pshort->x)
	{
	  p = (llnode *)malloc(sizeof(llnode));
      	  p->x = (pl->x)+nextc; 
      	  p->before = NULL;
      	}
      else
      	{
	  p = (llnode *)malloc(sizeof(llnode));
      	  p->x = pl->x;
	  p->before = NULL;
      	}
      p->next = q;
      q->before = p;
      q = p;
      ctail->x = ctail->x+1;
      pl = pl->before;
      i = i+1;
    }
}

void showResult(llnode* ctail)
{
  llnode *p;
  p = ctail;
  while(p->before != NULL)
    {
      p = p->before;
    }
  while(p != ctail)
    {
      printf("%d",p->x);
      p = p->next;
    }
  printf("\n");
}

int main()
{
  llnode *atail = (llnode *)malloc(sizeof(llnode));
  llnode *btail = (llnode *)malloc(sizeof(llnode));
  llnode *ctail = (llnode *)malloc(sizeof(llnode));
  llnode *p=atail;
  atail->x = 0;
  btail->x = 0;
  ctail->x = 0;
  atail->next = NULL;
  btail->next = NULL;
  ctail->next = NULL;
  getNumber(atail);
  getNumber(btail);
  addNumber(ctail, atail, btail);
  showResult(ctail);
  return 1;
}

C++语言题目4

//超市每天卖出的货物假如有egg,apple,pear三种,销售账目存储在了字典里,如下:
//account = [{"egg":[5.00, 3, "s"], 
//"apple":[2.50, 5, "s"], 
//"pear":[4.50, 2, "s"]},
//{"egg":[6.00, 3, "s"], 
//"apple":[3.00, 15, "q"], 
//"pear":[6.50, 2, "s"]},
//{"egg":[5.50, 5, "s"], 
//"apple":[3.50, 5, "s"], 
//"pear":[5.00, 6, "q"]},
//{"egg":[5.00, 3, "s"], 
//"apple":[2.50, 6, "s"], 
//"pear":[5.50, 2, "q"]},
//{"egg":[5.50, 8, "q"], 
//"apple":[3.50, 5, "s"], 
//"pear":[5.50, 3, "s"]}]
//每天的账目是列表中的一个元素,包括一个字典,字典的键是货物名称,字典的值的第一个元素是单价,
//第二个元素是数目,第三个元素,如果是s表示该账目没有任何疑问,如果是q表示该账目可能有错误。
//我们要做的是事情是统计每种货物,没有任何疑问的账目的销售总额。也就是对标志位为"s"的账目,
//按照货物名称,计算销售总额。(自己可根据使用的编程语言,确定存储结构,最后结果在屏幕打印
//egg、apple和pear分别的没有任何疑问的帐目的销售总额就可以。)
#include"iostream"
#include"string"
using namespace std;
class Account
{
private:
  string name;
  double price;
  float number;
  char flag;
public:
  Account(string goodsname, double goodsprice, float goodsnumber, char goodsflag)
  {
    name = goodsname;
    price = goodsprice;
    number = goodsnumber;
    flag = goodsflag;
  }
  double getTotalPrice()
  {
    return price*number; 
  }
  string getName()
  {
    return name;
  }
  char getFlag()
  {
    return flag;
  }
};
int main()
{
  Account accounts[15] = {
    Account("egg",5.00,3,'s'),
    Account("apple",2.50,5,'s'),
    Account("pear",4.50,2,'s'),
    Account("egg",6.00,3,'s'),
    Account("apple",3.00,15,'q'),
    Account("pear",6.50,2,'s'),
    Account("egg",5.50,5,'s'),
    Account("apple",3.50,5,'s'),
    Account("pear",5.00,6,'q'),
    Account("egg",5.00,3,'s'),
    Account("apple",2.50,6,'s'),
    Account("pear",5.50,2,'q'),
    Account("egg",5.50,8,'q'),
    Account("apple",3.50,5,'s'),
    Account("pear",5.50,3,'s')};
  string names[3] = {"egg", "apple", "pear"};
  int i=0;
  int j;
  double count=0;
  for(i=0; i<3; i++)
    {
      count = 0;
      for(j=0; j<15; j++)
	{
	  if((accounts[j].getName() == names[i]) && (accounts[j].getFlag() == 's'))
	    {
	      count = count + accounts[j].getTotalPrice();
	    }
	}
      cout<<names[i]<<"的总价是:"<<count<<endl;
    }
  return 1;
}

C++语言题目3

//采用面向对象的方法实现一个stack类,操作包括进栈,出栈,取栈顶元素,显示整个栈;
//(只要内存够用,可以无限次进栈)
#include"iostream"
using namespace std;

class Node{
private:
  int x;
  Node* next;
public:
  Node(int data)
  {
    x = data;
  }
  void setNext(Node* p)
  {
    next = p;
  }
  Node* getNext()
  {
    return next;
  }
  int setData(int number)
  {
    x = number;
  }
  int getData()
  {
    return x;
  }
  ~Node(){}
};

class Stack
{
private:
  Node *nodelast;
public:
  Stack()
  {
    nodelast = new Node(0);
    nodelast->setNext(NULL);
  }
  void pushData(int x)
  {
    Node *p = new Node(x);
    p->setNext(nodelast->getNext());
    nodelast->setNext(p);
    nodelast->setData(nodelast->getData()+1);
  }
  int popData()
  {
    int value;
    int error = 0;
    Node *p = nodelast->getNext();
    Node *q = p;
    if((p != NULL))
      {
	value = q->getData();
	p = p->getNext();
	nodelast->setNext(p);
	nodelast->setData(nodelast->getData()-1);
	delete q;
	return value;
      }
    else
      {
	throw error;
      }
  }
  int getTopData()
  {
    int value;
    int error = 0;
    Node *p = nodelast->getNext();
    if((p != NULL))
      {
	value = p->getData();
	return value;
      }
    else
      {
	throw error;
      }
  }
  void showStack()
  {
    int i;
    Node *p = nodelast;
    cout<<"Top ";
    for(i=0; i< nodelast->getData(); i++)
      {
	p = p->getNext();
	cout<< p->getData()<<" ";
      }
    cout<<"End"<<endl;
  }
};
int main()
{
  int value;
  Stack s;
  try
    {
      cout<<"出栈:"<<endl;
      s.popData();
    }
  catch(int)
    {
      cout<<"栈为空!"<<endl;
    }
  cout<<"入栈1,3,2:"<<endl;
  s.pushData(1);
  s.pushData(3);
  s.pushData(2);
  s.showStack();
  try
    {
      value = s.popData();
      cout<<"出栈:"<<value<<endl;
      s.showStack();
      value = s.getTopData();
      cout<<"当前栈顶元素:"<<value<<endl;
      s.showStack();
    }
  catch(int)
    {
      cout<<"栈为空!"<<endl;
    }
  cout<<"入栈3,4,5:"<<endl;
  s.pushData(3);
  s.pushData(4);
  s.pushData(5);
  s.showStack();
  return 1;
}

C语言题目13

//有两列数字, A{1,2,3,4,5,6,7,8,9}   B{10,11,12,13,14,15} 
//合并成一个新的数列C,要求所有的奇数排在所有的偶数前面,
//并且奇数要升序,偶数要降序,输出数列C到屏幕上

#include"stdio.h"
int getminodd(int c[], int start, int n)
{
  int k;
  int minodd = start;
  while(c[minodd]%2 == 0)
    {
      minodd = minodd+1;
      if(minodd == n)
	{
	  break;
	}
    }
  if(minodd != n)
    {
      for(k=minodd; k<n; k++)
	{
	  if((c[minodd]>c[k]) && c[k]%2 == 1)
	    {
	      minodd = k;
	    }
	}
    } 
  return minodd;
}

int getmaxeven(int c[], int start, int n)
{
  int k;
  int maxeven = start;
  while(c[maxeven]%2 != 0)
    {
      maxeven = maxeven+1;
      if(maxeven == n)
	{
	  break;
	}
    }
  if(maxeven != n)
    {
      for(k=maxeven; k<n; k++)
	{
	  if((c[maxeven]<c[k]) && c[k]%2 == 0)
	    {
	      maxeven = k;
	    }
	}
    } 
  return maxeven;
}

int main()
{
  int a[9] = {1,2,3,4,5,6,7,8,9};
  int b[6] = {10,11,12,13,14,15};
  int c[15] = {};
  int i;
  int j;
  int k=0;
  int count=0;
  int minodd=0;
  int maxeven=0;
  int swap;
  //合并
  for(i=0; i<9; i++)
    {
      c[k] = a[i];
      k = k+1;
    }
  for(j=0; j<6; j++)
    {
      c[k] = b[j];
      k = k+1;
    }
  //奇数部分选择排序
  while(1)
    {
      minodd = getminodd(c, count, 15);
      if(minodd == 15)
	{
	  break;
	}
      swap = c[count];
      c[count] = c[minodd];
      c[minodd] = swap;
      count = count+1;
      if(count >= 15)
	{
	  break;
	}
    }
  //偶数部分选择排序
  while(1)
    {
      maxeven = getmaxeven(c, count, 15);
      if(maxeven == 15)
	{
	  break;
	}
      swap = c[count];
      c[count] = c[maxeven];
      c[maxeven] = swap;
      count = count+1;
      if(count >= 15)
	{
	  break;
	}
    }
  //输出
  for(k=0; k<15; k++)
    {
      printf("%d ", c[k]); 
    }
  printf("\n");
  return 1;
}

C语言题目12

//有两个集合, A{1,3,6,2,9}  B{1,4,6,9,10}  输出这两个集合的交集和并集到屏幕上;
#include"stdio.h"
int main()
{
  int a[5] = {1,3,6,2,9};
  int b[5] = {1,4,6,9,10};
  int i;
  int j;
  //输出交集
  printf("两个集合的交集:");
  for(i=0; i<5; i++)
    {
      for(j=0; j<5; j++)
	{
	  if(a[i] == b[j])
	    {
	      printf("%d ", a[i]);
	      break;
	    }
	}
    }
  printf("\n");
  //输出并集
  printf("两个集合的并集:");
  for(i=0; i<5; i++)
    {
      for(j=0; j<5; j++)
	{
	  if(a[i] == b[j])
	    {
	      a[i] = '#';
	    }
	}
    }
  for(i=0; i<5; i++)
    {
      if(a[i]!='#')
	{
	  printf("%d ", a[i]);
	}
    }
  for(j=0; j<5; j++)
    {
      printf("%d ", b[j]);
    }
  printf("\n");
  return 1;
}

C语言题目11

//输入一串字符串,以回车结束,统计其中的英文字符、数字字符、空格和其他字符数目;
#include<stdio.h>
int main()
{
  char c;
  int chars = 0; 
  int numbers = 0;
  int spaces = 0;
  int others = 0;
  printf("请输入一个字符串,输入完成后敲击回车:");
  while(1)
    {
      c = getchar();
      if(c=='\n')
	{
	  break;
	}
      else
	{
	  if(c>='0' && c<='9')
	    {
	      numbers++;
	    }
	  else if((c>='a' && c<='z')||(c>='A' && c<='Z'))
	    {
	      chars++;
	    }
	  else if(c == ' ')
	    {
	      spaces++;
	    }
	  else
	    {
	      others++;
	    }
	}
    }
  printf("char=%d\tnumber=%d\tspace=%d\tother=%d\n", chars, numbers, spaces, others);
  return 0;
}

C语言题目10

//求两个数字的最大公约数和最小公倍数
#include"stdio.h"
int main()
{
  int min;
  int max;
  int a;
  int b;
  int swap;
  int remain = 1;
  printf("请输入数字a:");
  scanf("%d", &max);
  printf("请输入数字b:");
  scanf("%d", &min);
  a = max;
  b = min;
  if(min > max)
    {
      swap = min;
      min = max;
      max = swap;
    }
  while(remain!=0)
    {
      remain = max%min;
      max = min;
      min = remain;
    }
  printf("最大公约数为:%d\n", max);
  printf("最小公倍数为:%d\n", a*b/max);
  return 1;
}

C语言题目9

//打印100以内的素数
#include"stdio.h"
int main()
{
  int i;
  int j;
  int flag = 1;
  for(i=2; i<100; i++)
    {
      flag = 1;
      for(j=i-1; j>1; j--)
	{
	  if(i%j == 0)
	    {
	      flag = 0;
	      break;
	    }
	}
      if(flag == 1)
	{
	  printf("%d ", i);
	}
    }
  printf("\n");
  return 1;
}

C语言题目8

//有两个集合A={a,b,f,d,e} B={g,b,h,d,e,e} 合并为一个集合C, C中的元素是递增排列的,并且无重复元素。
//这道题也可以拼接两个集合到一个集合,然后排序消重。

#include"stdio.h"
int getmin(char k[], int n)
{
  int min = -1;
  int i;
  for(i=0; i<n; i++)
    {
      if(k[i] != '#')
	{
	  min = i;
	}
    }
  if(min != -1)
    {
      for(i=0; i<5; i++)
	{
	  if((k[i] == '#') || i == min)
	    {
	      continue;
	    }
	  if(k[i] < k[min])
	    {
	      min = i;
	    }
	  else if(k[i] == k[min])
	    {
	      k[i] = '#';
	    }
	}
    }
  return min;
}

int main()
{
  char a[5] = {'a','b','f','d','e'};
  char b[6] = {'g','b','h','d','e','e'};
  char c[12] = {};
  int i = 0;
  int j = 0;
  int mina;
  int minb;
  int k = 0;
  int flaga = 0;
  int flagb = 0;
  while(1)
    {
      mina = getmin(a,5);
      minb = getmin(b,6);
      if((mina != -1) && (minb != -1))
	{
	  if(a[mina] < b[minb])
	    {
	      c[k] = a[mina];
	      a[mina] = '#';
	      k = k+1;
	    }
	  else if(a[mina] == b[minb])
	    {
	      c[k] = a[mina];
	      a[mina] = '#';
	      b[minb] = '#';
	      k = k+1;
	    }
	  else
	    {
	      c[k] = b[minb];
	      b[minb] = '#';
	      k = k+1;
	    }
	}
      else if((mina == -1) && (minb != -1))
	{
	  c[k] = b[minb];
	  b[minb] = '#';
	  k = k+1;
	}
      else if((mina != -1) && (minb == -1))
	{
	  c[k] = a[mina];
	  a[mina] = '#';
	  k = k+1;
	}
      else
	{
	  c[k] = '#';
	  break;
	}
    }
  i = 0;
  while(i<12)
    {
      if(c[i] == '#')
	{
	  printf("\n");	 
	  break;
	}
      else
	{
	  printf("%c ", c[i]);
	  i = i+1;
	}
    }
}

C语言题目7

//已知n=0, f(n)=0; n=1, f(n)=1; n>=2, f(n)=[f(n-1)+1]*[f(n-2)-1], 求f(50)。
//注意:double也只能存储到f(18)。这道题应该不会是要考大整数乘法,所以题目出的有问题。
//这道题也可以用递归,但是递归运行速度比较慢,因为有大量重复计算,所以可以按照我的方法用链表。
//如果真的想要求出f(50),可以用链表存储两个计算因子和计算结果,转化成大整数加法,来求大整数乘法。
//把乘法转化成加法计算,比如3×4 = 4+4+4
//以下是前20个数字的运行结果
//0:0.000000
//1:1.000000
//2:-2.000000
//3:-0.000000
//4:-3.000000
//5:2.000000
//6:-12.000000
//7:-11.000000
//8:130.000000
//9:-1572.000000
//10:-202659.000000
//11:318781034.000000
//12:-64604164553100.000000
//13:-20594582312338883346432.000000
//14:1330495784608724577563601617989664768.000000
//15:-27401004952344282189274505435890321300250290209030873808896.000000
//16:-36456921583136852485661222029923476042013772777779791392989314785761343035841829994866675810304.000000
//17:9989562888467599682490178454054145458467604867235829190066412851063708189685362766757643907501070026206
//55223767942258990500160700908909006358607921414144.000000
//18:-364188710874677328890688166883341655997440487840343568582579911102615655287833051471441667841754329590
//03497968188602575038253510532501707167150463676880377107100124867120157208280951966764835855418677107978
//345178504768013589268641567045778554224640.000000
//19:-inf
//20:inf


#include"stdio.h"
#include"stdlib.h"

typedef struct node{
  double x;
  struct node *next;
}llnode;

void generate(llnode *head, int n)
{
  llnode *p;
  llnode *m;
  double fn2 = 0;
  double fn1 = 0;
  int i = 0;
  if(n==1)
    {
      m = (llnode *)malloc(sizeof(llnode));
      m->x = 1;
      m->next = NULL;
      head->next = m;
    }
  else
    {
      p = head;
      while(i<n-2)
	{
	  p = p->next;
	  i = i+1;
	}
      fn2 = p->x;
      p = p->next;
      fn1 = p->x;
      m = (llnode *)malloc(sizeof(llnode));
      m->x = (fn1+1)*(fn2-1);
      m->next = NULL;
      p->next = m;
    }
}

double f(llnode* head, int n)
{
  int i=0;
  llnode *p = head;
  while(i<n)
    {
      p = p->next;
      i = i+1;
    }
  return p->x;
}

int main()
{
  llnode *head;
  int n = 0;
  int last = 20;//要求的最大结果
  double result=0;
  head = (llnode *)malloc(sizeof(llnode));
  head->x = 0;
  head->next = NULL;
  for(n=1; n<=last; n++)
    {
      generate(head, n);
    } 
  for(n=0; n<=last; n++)
    {
      result = f(head, n);
      printf("%d:", n);
      printf("%lf\n", result);
    }
  return 1;
}