字符大小写转换...

来源:互联网 发布:pat考试 知乎 编辑:程序博客网 时间:2024/05/09 06:43

题目1:写一个程序,要求功能:求出用1,2,5这三个数不同个数组合的和为100的组合个数。

如:100个1是一个组合,5个1加19个5是一个组合

#include <iostream>using namespace std;int func(int num){    int i,j,k,sum,count=0;    for(i=0;i<=(num/5);i++)  //i对应数组5的个数    {        for(j=0;j<=(num/2);j++)    //j对应数字2的个数        {            for(k=0;k<=num;k++)     //k对应数字1的个数            {                sum=i*5+j*2+k;                if(100==sum)                {                    count++;                    cout<<"1的个数:"<<k<<' '<<"2的个数:"<<j<<' '<<"5的个数"<<i<<'\n';                }            }        }    }    return count;}int main(){    int num=100;    cout<<func(num);    return 0;}

题目2:一个学生的信息是:姓名,学号,性别,年龄,用一个链表,把这些学生信息连在一起,给出一个age,在链表中删除学生年龄等于age的学生信息。

#include <iostream>#include <string>using namespace std;class LinkList;class Node{private:    string name;    string id;    char sex;    int age;    Node *link;public:    Node()    {        link=NULL;    }    Node(string n,string i,char s,int a)    {        name=n;        id=i;        sex=s;        age=a;        link=NULL;    }    friend class LinkList;};class LinkList{private:    Node *first;    int n;          //记录单链表长度public:    LinkList()    {        first=new Node;        n=0;    }    ~LinkList()    {        Node *p;        while(first)        {            p=first;            first=first->link;            delete p;        }    }    bool Insert(int adr,string na,string id,char se,int ag)  //adr若为0表示将新节点插在第一位    {        if(adr<0 || adr>n)        {            cout<<"Insert:越界"<<endl;            return false;        }        Node *p=first;        for(int j=0;j<adr;j++)        {            if(p)                p=p->link;            else                return false;        }        Node *q=new Node(na,id,se,ag);        q->link=p->link;        p->link=q;        n++;        return true;    }    bool Delete(int a)    {        if(!n)        {            cout<<"Delete:没有可以删除的数据项"<<endl;            return false;        }        Node *q=first->link;    //q是要删除的节点        Node *p=first;        while(q)        {            if(q->age==a)            {                p->link=q->link;                cout<<"Delete:成功删除了年龄为"<<q->age<<"的学生"<<endl;                delete q;                n--;                return true;            }            else            {                p=p->link;                q=q->link;            }        }    }    void Print()    {        if(n)        {            cout<<"Print:总共有"<<n<<"条数据"<<endl;            Node *p=first->link;            while(p)            {                cout<<"姓名"<<p->name<<'\n';                cout<<"学号"<<p->id<<'\n';                cout<<"性别"<<p->sex<<'\n';                cout<<"年龄"<<p->age<<'\n';                cout<<endl;                p=p->link;            }        }        else        {            cout<<"Print:数据项为0"<<endl;        }    }};int main(){    LinkList L1;    L1.Insert(0,"zhuzhiwen","100",'f',10);    L1.Insert(0,"gwb","101",'m',11);    L1.Print();    L1.Delete(11);    L1.Print();    return 0;}

注意:定义局部变量时不能产生重名覆盖!


题目3:实现一个函数,把一个字符串中的字符从小写转为大写

#include <iostream>#include <cctype>#include <string.h>using namespace std;void func(const char *input,char *output){    const char *p=input;    int len=strlen(input);    char *q=output;    while(*p!='\0')    {        if(isalpha(*p))        {            *q=toupper(*p);            q++;        }        p++;    }    *q='\0';}int main(){    const char *input="zh78_ hiwen";    int len=strlen(input);    char *output=new char[len+1];    func(input,output);    cout<<output<<endl;    return 0;}

【欢迎读者交流批评指正~】

0 0
原创粉丝点击