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;
}
转载请注明原文地址: https://ju.6miu.com/read-33810.html