1.輸入一串字符串,統計輸入大小寫英文字母、數字、空格、其他字符的數量
#include<stdio.h>
int main()
{
int letter,num,space,other;
letter=num=space=other=0;
char ch;
while((ch=getchar())!='\n')
{
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
letter++;
else if(ch>='0'&&ch<='9')
num++;
else if(ch==' ')
space++;
else
other++;
}
printf("letter num is %d\n",letter);
printf("number num is %d\n",num);
printf("space num is %d\n",space);
printf("other num is %d\n",other);
}
2.完全平方數:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,n,temp,flag;
for(i=1;;i++)
{
if(sqrt(i+100)==(int)sqrt(i+100))
if(sqrt(i+100+168)==(int)sqrt(i+100+168))
{ printf("%d\n",i);
break;
}
printf("f %d\n",i);
}
}
3.輸入一段英文,如( hello world chen swpu )去掉空格後輸出
#include<stdio.h>
#include<string.h>
int main()
{
char s[1000];
int des[1000];
int i;
int j=0;
int m=2;
gets(s);
for(i=0;*(s+i)!='\0';i++)
{
if(*(s+i)==' ')
des[j++]=i;
}
printf("%d\n",des[j-1]);
for(i=des[j-1]-1;i>=0;i--)
{
if(i>des[j-m])
printf("%c",*(s+i));
else
{
m++;
printf(" ");
}
}
printf("%d",strlen(s));
}
4.判斷素數和輸出小於n的所有素數
#include<iostream>
using namespace std;
int checkPrime(int m)
{
int i,j;
if(m<2)
return -1;
else
for(i=2;i<=m/2;i++)
if(m%i==0)
return -1;
return 1;
}
int outputPrime(int m)
{
int i,j;
for(i=2;i<=m;i++)
{
for(j=2;j<=i/2;j++)
if(i%j==0)
{
break;
}
if((i/2+1)==j)
cout<<i<<" ";
}
return 0;
}
int main()
{
int i,j,m;
cin>>m;
outputPrime(m);
}
5.猴子第一天摘了若干個桃子,當即吃了一半,還不解饞,又多吃了一個;第二天,吃剩下的桃子的一半,還不過癮,又多吃了一個;以後每天都吃前一天剩下的一半多一個,到第10天想再吃時,只剩下一個桃子了。問第一天共摘了多少個桃子?
#include<iostream>
using namespace std;
int main()
{ int temp=1;
int i;
for(i=1;i<10;i++)
{
temp=(temp+1)*2;
cout<<"temp="<<temp<<" ";
}
}
6.10進制轉2進制輸出
#include<iostream>
using namespace std;
int main()
{
int m,i;
int a=1;
i=0;
cin>>m;
while(m>0)
{
if(m%2==1)
a|=1<<i;
else
a&=0<<i;
++i;
m/=2;
}
for (int i=sizeof(int)*8-1;i>=0;i--)
{
int mask=1<<i;
if (mask & a)
cout<<"1";
else
cout<<"0";
if(i%8==0)
cout<<" ";
}
}
7.16進制轉10進制
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int hex_char_value(char c)
{
int m;
if(c>='0'&&c<='9')
m=c-'0';
else if(c>='a'&&c<='z')
m=(c-'a'+10);
else if(c>='A'&&c<='Z')
m=(c-'A'+10);
return m;
}
int hex_str_value(const char *hex,int len)
{
int result=0;
int i,j;
for(i=0;i<len;i++)
{
result+=pow(16,i)*(hex_char_value(hex[i]));
}
return result;
}
int main()
{
int m,n,j;
char s[1024];
gets(s);
n=strlen(s);
m=hex_str_value(s,n);
cout<<m;
}