c++基础题(随时补充)

来源:互联网 发布:tk顶级域名注册官网 编辑:程序博客网 时间:2024/05/22 02:17
统计行数、字数和字符数
#include<iostream>
using namespace std;
int main()
{
int ch;
int nline=0,nword=0,nch=0;
int isword=0;
cout<<"输入一段文本,无空行,以输入ctrl+z,enter结束:"<<endl;
do{
ch=cin.get();
if(ch=='\n') nline++;
if(ch!=' '&&ch!='\n'&&ch!='\t'&&ch!=EOF){
if(isword==0) nword++;
nch++;
isword=1;
}
else isword=0;
}while(ch!=EOF);
cout<<"行数:"<<nline<<endl;
cout<<"字符数:"<<nch<<endl;
cout<<"单词数:"<<nword<<endl;
system("pause");
return 0;
}


兔子问题
#include<iostream>
#include<iomanip>
using namespace std;
const int m=30;
int main()
{
int n,x0=0,x1=1,x2;
cout<<setw(15)<<x0<<setw(15)<<x1;
for(n=3;n<=m;n++){
x2=x0+x1;
cout<<setw(15)<<x2;
if(n%5==0) cout<<endl;
x0=x1;x1=x2;
}
system("pause");
return 0;
}




数字倒置
#include<iostream>
using namespace std;
int main()
{
int i,num,subscript=0;
int digit[9];//最多9位
cin>>num;
do{
digit[subscript]=num%10;
num=num/10;
subscript++;
}while(num>0);
for(i=0;i<subscript;i++)
{
num=num*10+digit[i];
}
cout<<num<<endl;
system("pause");
return 0;
}




#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<setw(3)<<'*';
for(i=1;i<=10;i++) cout<<setw(4)<<i;
cout<<endl;
for(i=1;i<=10;i++){
cout<<setw(3)<<i;
for(j=1;j<=i;j++)
cout<<setw(4)<<i*j;
cout<<endl;
}
system("pause");
return 0;
}




递归:阶层
#include<iostream>
using namespace std;
int fan(int n){
int y;
cout<<n<<'\t';
if(n==0||n==1)
y=1;
else
y=n*fan(n-1);
cout<<y<<'\t';
return y;
}
int main(){
int n=4;
cout<<'\n'<<n<<"!="<<fan(n)<<endl;
system("pause");
return 0;
}

//移盘子
#include<iostream>
using namespace std;
void move(char,char);
void hanoi(int,char,char,char);
int main(){
int n;
cout<<"输入盘子数:"<<endl;
cin>>n;
hanoi(n,'A','B','C');
cout<<endl;
system("pause");
return 0;
}
void hanoi(int n,char source,char temp,char target){
if(n==1)
move(source,target);
else{
hanoi(n-1,source,target,temp);
move(source,target);
hanoi(n-1,temp,source,target);
}


}
void move(char source,char target){
cout<<source<<"->"<<target<<'\t';
}



字符串语句逆序
#include<iostream>
#include<string>
using namespace std;
int main()
{
   string s1;
   getline(cin,s1);
   string s2(s1);
   for (int i= 0; i <s1.size(); ++i)
   s2[i]=s1[s1.size()-i-1];
   cout<<s2<<endl;
   system("pause");
   return 0;
}




百钱买百鸡
#include<iostream>  
#include <iomanip>
using namespace std;  
int main()  
{  
    int x,y,z;  
    for(x=0;x<=20;x++)  
        for(y=0;y<=33;y++)  
            {  
z=100-x-y;
                if(5*x+3*y+z/3==100 && z%3==0)  
                    cout<<x<<" "<<y<<" "<<z<<endl;  
            }  
system("pause");
            return 0;  
}  


//写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。

#include <iostream>
#include <string.h>
using namespace std;
int main(){
char str[1000];
    char sh[1000];
    char* result;
    int n = 0;
    cin.getline(str, 1000);
    cin.getline(sh, 1000);
for(int i=0;i<1000;i++){     //将所有大写转换为小写
        str[i] = tolower(str[i]);
sh[i] = tolower(sh[i]);
}
    for (result = strstr(str, sh);result&&++n;)
        (result = strstr(result+1,sh));
    cout << n << endl;
system("pause"); 
    return 0;
}




输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string ch;
int blank=0,eng=0,num=0,qita=0;
getline(cin,ch);
for(int i=0;i!=ch.size();i++){
if(ch[i]==' ') blank++;
else if(ch[i]>='0'&&ch[i]<='9') num++;
else if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')) eng++;
else qita++;
}
cout<<eng<<endl;
cout<<blank<<endl;
cout<<num<<endl;
cout<<qita<<endl;
system("pause");
return 0;
}




找出给定字符串中大写字符(即'A'-'Z')的个数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int count=0;
getline(cin,str);
for(int i=0;i!=str.size();i++){
if((str[i]>='A'&&str[i]<='Z')) count++;
}
cout<<count<<endl;
system("pause");
return 0;
}




#include<iostream>
using namespace std;
void main()
{
   int n,i=0,j=0;
   int a[10000];
   cin>>n;
   while(n)
   {
    a[j]=n%2;
    n/=2;
    j++;
   }
   for(n=j-1;n>=0;n--){
 
  if(a[n]==1) ++i;
   }
   cout<<i<<endl;
}




记负均正
#include<iostream>
#include <iomanip>
using namespace std;
void main()
{
   int n,num=0,neg=0,pos=0;
   float num1=0.0;
   int a[10000];
   cin>>n;
   for(int i=0;i<n;i++){
  cin>>a[i];
  if(a[i]<0) neg++;
  else num+=a[i],num1+=a[i],pos++;
   }
   cout<<neg<<" ";
   if(num%pos==0) cout<<num/pos<<endl;
   else cout<<fixed<<setprecision(1)<<num1/pos<<endl;
   system("pause");
}


质数因子从小到大排列
#include<iostream>
using namespace std;
int prime(int y)
{
 int i;
 if(y==1) return 0;
 if(y==2) return 1;
 for(i=2;i<y;i++)
 {
        if(y%i==0)
           return 0;
 }
  return 1;
}
void main()
{
long a;
int i;
cin>>a;
for(i=1;i<=a;i++)
{
   if(a%i==0&&prime(i)==1)
   {
     a=a/i;
     cout<<i<<" "; 
i--;
   }
   else
     continue;
 }
 cout<<endl;
 system("pause");
}




字符串合并
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string a,b;
cin>>a;
cin>>b;
string c=a+b; 
cout<<c<<endl;
system("pause");
return 0;
}


字符串合并:合并后分别取出其奇偶位的字符,先按顺序排序,然后将每个字符转换为16进制数并取反,以十六进制形式输出
#include <iostream>  
#include <string>  
#include <cctype>  
  
using namespace std;  
  
void sort( string& word, int old );  
void ProcessString( string& word );  
  
int main( void )  
{  
    string word1, word2;  
    cin >> word1 >> word2;  
    word1.append( word2 );  
    sort( word1, 0 );  
    sort( word1, 1 );  
    ProcessString( word1 );  
  
    cout << word1 << endl;  
  
    system("pause");
    return 0;  
}  
  
void sort( string& word, int old )  
{  
    char tmp;  
    int nMax;  
    for( nMax = old; nMax < word.length(); nMax += 2 )  
        ;  
    nMax -= 2;  
  
    for( int i = nMax; i > old; i -= 2 ){  
        bool flag = false;  
        for( int j = old; j < i; j += 2 ){  
            if( word[j] > word[j + 2] ){  
                tmp = word[j];  
                word[j] = word[j + 2];  
                word[j + 2] = tmp;  
                flag = true;  
            }  
        }  
        if( !flag )  
            break;  
    }  
}  
  
void ProcessString( string& word )  
{  
    char ch1, ch2, bit, bFlag;  
  
    for( string::iterator it = word.begin();   
        it != word.end(); ++it ){  
        ch2 = 0;  
        bit = 1;  
        bFlag = 1;  
        if( isdigit( *it ) ){  
            ch1 = *it - '0';  
        }else if( *it >= 'a' && *it <= 'f' ){  
            ch1 = *it - 'a' + 10;  
        }else if( *it >= 'A' && *it <= 'F' ){  
            ch1 = *it - 'A' + 10;  
        }else{  
            bFlag = 0;  
        }  
  
        if( bFlag ){  
            ch2 |= ( ch1 & ( 1 << 0 ) ) << 3;  
            ch2 |= ( ch1 & ( 1 << 1 ) ) << 1;  
            ch2 |= ( ch1 & ( 1 << 2 ) ) >> 1;  
            ch2 |= ( ch1 & ( 1 << 3 ) ) >> 3;  
            if( ch2 < 10 ){  
                *it = '0' + ch2;  
            }else{  
                *it = 'A' + ch2 - 10;  
            }  
        }  
    }  
}  



字符串排序,字符串长度不变,从a到z,大小写按原始顺序,数字与其它字符位置不变
#include<iostream>
#include <stdio.h>  
#include <string>   
using namespace std;
int sort_string(char* str)  
{  
    char SwapChar = '0';  
    int length = 0;  
    int i , j , k;  
    if (NULL == str)  
    {  
        return -1;  
    }  
    length = (int)strlen(str);  
    for(i = 0; i < length; i++)  
    {  
        for(j = 0; j < length-1-i; j++)  
        {  
            if(toupper(str[j]) < 'A' || toupper(str[j]) > 'Z')  
            {  
                continue;  
            }  
            k = j + 1;  
            while((toupper(str[k]) < 'A' || toupper(str[k]) > 'Z') && k < length)  
            {  
                k++;  
            }  
            if(k == length)  
            {  
                continue;  
            }  
            if(toupper(str[j]) > toupper(str[k]))  
            {  
                SwapChar = str[j];  
                str[j] = str[k];  
                str[k] = SwapChar;  
            }  
        }  
    }  
cout<<str;
    return 0;  
}  
int main(){
char s[10000];
cin.getline(s,10000);
sort_string(s);
system("pause");
return 0;
}


最大公约数
#include<iostream>
using namespace std;
int main()
{
int a,b,i=1;
cin>>a>>b;
while(i>=1)
{
if(i>=a&&i>=b&&i%a==0&&i%b==0)
{
cout<<i<<endl;
break;


}
else ++i;
}
system("pause");
return 0;



统计字符串中字符个数,转化为大写
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a[1000],s1;
int count=0;
string s;
getline(cin,s);
strcpy(a,s.c_str());//字符串复制,c_str返回当前字符串首字符地址。
     cin>>s1;
for(int i=0;i<s.size();++i)
{
if(toupper(s1)==toupper(a[i]))//转换为大写
count++;
}
cout<<count<<endl;
system("pause");
    return 0;
}


只出现一次的最靠前的字母
#include <iostream>
using namespace std;
void  f(char* s)
{
 int cnt,i,l;
 char* s1;
 l=strlen(s);
 for(i=0;i<l;i++)
 {
  s1=s;
  cnt=0;
  while(*s1)
  {
   if(*s1==s[i])
   {
    cnt++;
    if (cnt>1)
     break;
   }
   s1++;
  }
  if(cnt==1)
  {
     cout<<s[i]<<endl;
     return;
  }
 }
 cout<<"."<<endl;
}
int main()
{
char a[1000];
cin.get(a,1000);
f(a);
system("pause");
 return 0;
}


#include<iostream>
#include<string>  
using namespace std;
char getUniqueCharacter(char*s)
{     char order[256]={0};
      int counter[256]={0};
 int i=0;
 char temp;
 char*p=s;
 while(*p)
   {
     temp=*p;//p指向的字符赋给temp
 if(counter[temp]==0)//如果temp没出现过,把第i个次序位置留给temp
 {
 order[i++]=temp; }
 counter[temp]++;//temp出现的次数加1
 p++;//继续往下搜索
      }


 int j;
 for(j=0;j<256;j++)
 if(order[j]!=0)//如果次序j的值不为0
{
 if(counter[order[j]]==1)//第j个值出现了一次
 {
 temp=order[j];
 break;   //把这个值赋给temp
 }
 else
 temp='.';
}
 return temp;
}


int main()
{
char s[1000];
cin.getline(s,1000);
char result= getUniqueCharacter(s);
cout<<result<<endl;
system("pause");
return 0;
}


//字符串通配符 
#include<iostream>
using namespace std;
bool PathernMatch(char *pat,char *str)
{
 char *s=NULL;
 char *p=NULL;
 bool star=false;
 bool bBreak=false;
 do 
 {
    bBreak=false;
    for(s=str,p=pat;*s;++s,++p)
    {
     switch(*p)
     {
     case '?':
      break;
     case '*':
      star=true; //出现*匹配符
      str=s;
      pat=p;
      if(!*++pat)
       return true;
      bBreak=true; //退出循环
      break;
     default:
      if(*s!=*p)
      {
       if(!star) 
        return false;
       str++;
       bBreak=true;
      }
      break;
     }
     if(bBreak) //退出循环 重新开始循环
      break;
    }
    if(bBreak==false)
    {
    if(*p=='*')
     ++p;
    return (!*p);
    }
 } while(true);
}
int main()
{
   char a[100];
   char b[100];
   cin.getline(a,100);
   cin.getline(b,100);
   int p=PathernMatch(a,b);
   if(p==0) cout<<"false"<<endl;
   else cout<<"true"<<endl;
   system("pause");
   return 0; 
}


放苹果
#include<iostream> 
using namespace std; 
int f(int m, int n) 

if(n==1||m==0) 
return 1; 
if(n>m) 
return f(m, m); 
return f(m,n-1) + f(m-n, n); 



int main(int argc, char * argv[]) 

int m, n; 
cin>>m>>n; 
cout<<f(m, n)<<endl; 
system("pause");
return 0; 
}


在字符串中找出连续最长的数字串
#include <iostream>
using namespace std;
void func(const char *str)
{
    int len = 0;
    int max_len = 0;
    const char *max_pos = str;
     
    for (;;)
    {
        if ('0' <= *str && *str <= '9')
        {
            ++len;
        }
        else
        {
            if (len > max_len)
            {
                max_len = len;
                max_pos = str - len;
            }
            len = 0;
            if (0 == *str)
            {
                break;
            }
        }
        ++str;
    }
    if(max_len==0) cout<<"0"<<endl;
else
{
    while('0' <= *max_pos && *max_pos <= '9')
    {
       cout<<*max_pos;
        ++max_pos;
    }   
cout<<","<<max_len<<endl;
}
}
int main()
{
char s[100];
cin.getline(s,100);
func(s);
system("pause");
return 0;
}



四舍五入取整
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
double a;
int b;
cin>>a;
int p=(int)a;
if(a-p<0.5)
b=floor(a);
else
b=ceil(a);
cout<<b<<endl;
system("pause");
return 0;
}


明明的随机数,“去重”和“排序”相结合
#include<iostream>
#define MAXN 100 
using namespace std;
int main()
{
    int num[MAXN],n,count=0;
    int found,t,i,j,x;
    
       cin>>n;
    for(i=0;i<n;i++){
        cin>>x;
        found=0;
        for(j=0;j<count;j++)
            if(x==num[j]){
                found=1;
                break;
            }
       if(!found){
           num[count]=x;
           count++;
       }
    }
    
    for(i=0;i<count;i++)
        for(j=1;j<count;j++)
            if(num[j-1]>num[j]){
                t=num[j];
                num[j]=num[j-1];
                num[j-1]=t;
            }
    for(i=0;i<count;i++)
        cout<<num[i]<<endl;
system("pause");
    return 0;
}


合法ip
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
bool IsIP(char *ip)
{
char *temp = ip;
int nlength =strlen(temp);
if (nlength<7 || nlength>15)
return false;
int nCount = 0;
int nFlag = 0;
while(*temp != '\0')
{
if(*temp == '.')
{
nCount++;
temp++;
}
if(*temp<= '9' && *temp >= '0')
temp++;
else
 return false;
}


char s1[5],s2[5],s3[5],s4[5]; /*存放IP段*/
nFlag = sscanf(ip,"%[^.].%[^.].%[^.].%[^.]",s1,s2,s3,s4);
if(nCount != 3 || nFlag != 4)
return false;
int ip_1,ip_2,ip_3,ip_4;
ip_1 = atoi(s1);
ip_2 = atoi(s2);
ip_3 = atoi(s3);
ip_4 = atoi(s4);


int len_s1,len_s2,len_s3,len_s4;
len_s1=strlen(s1);
len_s2=strlen(s2);
len_s3=strlen(s3); 
len_s4=strlen(s4); 
     if( (0 <= ip_1 && ip_1 <= 255) && (0 <= ip_2 && ip_2 <= 255) && (0 <= ip_3 && ip_3 <= 255) && (0 <= ip_4 && ip_4 <= 255))
     {
if (!((s1[0]=='0' && len_s1>1) || (s2[0]=='0' && len_s2>1) || (s3[0]=='0' && len_s3>1) || (s4[0]=='0' && len_s4>1))) //确保每段ip不是以0开头
          return true;
}
return false;
}


int main()
{
char s[100];
cin.getline(s,100);
int p=IsIP(s);
if(p==0) cout<<"NO"<<endl;
else  cout<<"YES"<<endl;
system("pause");
return 0;
}




#include<iostream>
using namespace std;
int main()
{
int a,b,i=1;
cin>>a;
b=a*a-a+1;


for (i =0; i< a-1; ++i) 
{
    cout <<b+2*i<<"+";
}
cout<<b+2*(a-1)<<endl;
system("pause");
return 0;
}


#include<iostream>
using namespace std;
int main() 

int a,sum=2,x1=2; //x1初始化为0
cin>>a;
if(a<=0)
return -1;
else
{    for(int i=1;i<=a-1;i++)// 去分号
      { x1+=3;
        sum+=x1;
}
      cout<<sum<<endl;
}
system("pause");
return 0;
}




#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
char s[100];
char temp;
cin.getline(s,100);
int a=strlen(s);
for(int i=0;i<a;i++)
{
for(int j=i+1;j<=a;j++)
{
if(s[i]>s[j])
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
cout<<temp;
}
cout<<endl;
system("pause");
return 0;
}


统计大写
#include<iostream>
#include <string>
using namespace std;
int main()
{
string str;
int count=0;
    getline(cin,str);
if(str.size()==0)
        count=0;
else
{
for(int i=0;i<=str.size();i++)
{
if(str[i]>='A'&&str[i]<='Z')
count++;
else 
continue;
}
}
cout<<count<<endl;
system("pause");
return 0;
}




图像整理
#include <iostream>
#include <string>
using namespace std;


int main()
{
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
for(int j=i;j<s.size();j++)
{
if(s[j]<s[i])
{
char chari=s[i];
s[i]=s[j];
s[j]=chari;
}
}
}
cout<<s<<endl;
system("pause");
return 0;


}


位运算,统计二进制1的个数
#include <iostream>
using namespace std;
int main(){    
int i,count=0;
cin >> i;
for (int j = 0; j < 32;j++)
{
if (i&1)
{
count++;
}
i >>= 1;
}
cout << count<<endl;
system("pause");
}


DNA序列
#include <iostream>
#include <string>
using namespace std;
void FindGC(string &s,int n)
{

double k=0;
string rst;
for(int i=0;i<s.size()-n+1;i++)
{
string a = s.substr(i, n);//返回字符串的一部分,substr(string,start,length)
int g=0;
int c=0;
for(int j=0;j<a.size();j++)
{

if(a[j]=='G')
{
g++;
}
if(a[j]=='C')
{
c++;
}
if(g!=0&&c!=0)
{
if(k<((double)(g+c))/n)
{
k=((double)(g+c))/n;
rst=a;
}
}
}
}
cout<<rst;

    return;
}
 
int main()
{
    string s;
    cin>>s;
int n;
cin>>n;
    FindGC(s,n);
    cout<<endl;
    system("pause");
    return 0;
}




挑7(30000以内)
#include<iostream>
using namespace std;
int main()
{
int a,count=0;
cin>>a;
for(int i=1;i<=a;i++)
{
if(i%7==0)
count++;
else if(i%10==7)
count++;
else if((i/10)%10==7)count++;   
        else if((i/100)%10==7)count++;  
        else if((i/1000)%10==7)count++; 
}
cout<<count<<endl;
system("pause");
return 0;
}


杨辉三角变形,指定行偶数位置
#include<iostream>
using namespace std;
int main()
{
int x,y;
cin>>x;
    if (x <= 2) // 1、2行都为-1
    {
        cout<<"-1"<<endl;;
    }
    else if (x & 1) // 奇数行为 2
    {
cout<<"2"<<endl;

    }
else
{
y=((x >> 1) & 1) + 3;
       cout<<y<<endl;
}
system("pause");
return 0;
}
0 0
原创粉丝点击