1、水仙花数:
首先介绍一下何为水仙花数:水仙花数也被称为超完全数字不变数、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数,水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:153=1^3 + 5^3 + 3^3,所以153就是水仙花数)请输入1000以内的所有水仙花数。
#include <stdio.h>
void main()
{
int x,y,z,k=100;
while(k/1000==0)
{
x=k/100;
y=k/10%10;
z=k%10;
if(k==z*z*z+y*y*y+x*x*x)
printf("%d\n",k);
++k;}
}
2、1-1/2+1/3-…+1/99+1/100求 的值
#include <stdio.h>
void main()
{
float s=0,i;
int sign=1;
for(i=1;i<=100;++i)
{s=s+(sign/i);
sign=-sign;}
printf("s=%f\n",s);
}
3、求 的值
#include <stdio.h>
void main()
{
int s=0,a,b=0,n;
scanf("%d",&n);
for(a=1;a<=n;++a)
{b+=a;
s+=b;}
printf("s=%d",s);
}
4、
回文排列
解释:给定一个字符串,编写一个函数判定其是否为某个回文串的排列之一。
回文串是指正反两个方向都一样的单词或短语。排列是指字母的重新排列。
回文串不一定是字典当中的单词。
输入:“tactcoa”
输出:true(排列有"tacocat"、“atcocta”,等等)
#include<stdio.h>
int main()
{
char he[100];
char a;
int i=0,flag=1;
while((a=getchar())!='\n')
{
he[i]=a;
i++;
}
int n=i;
for(i=0;i<n/2;i++)
{
printf("%c\t%c\n",he[i],he[n-1-i]);
if(he[i]!=he[n-1-i])
{
printf("no");break;
}
}
if(flag==1)
{
printf("yes");
}
return 0;
}
或者
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(){
char s[] = "tactcoa";
int ascii[128] = { 0 };
int oddSum = 0;
for (int i = 0; i<strlen(s); i++)
{
ascii[s[i]]++;
}
for (int i = 0; i<128; i++)
{
if (ascii[i] % 2 != 0)
{
oddSum++;
}
if (oddSum>1)
{
printf("false");
system("pause");
return 0;
}
}
printf("true");
system("pause");
return 0;
}
5,
实现函数: Pow(x, n)
实现库函数 pow(x, n) 方法,即计算 x 的 n 次幂函数。
示例1:
输入x=2,n=3;
输出:8;
示例2:
输入x=3,n=4;
输出:81;
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,result;
printf("输入两个正整数,以逗号隔开:");
scanf("%d,%d",&a,&b);
result = pow(a,b);
printf("result = %d\n",result);
return 0;
}
或者
#include<stdio.h>
double my_pow(double num, double n)
{
if (n < 0)
return 1.0 / my_pow(num, -n);
else if (n == 0)
return 1.0;
else if (n > 0)
return num * my_pow(num, n - 1);
}
int main()
{
printf("%lf\n", my_pow(2,-2));
return 0;
}
1、
算法思想
S1:遍历输入的括号序列,如果是左括号,进入S2,如果是右括号,进入S3
S2:如果当前遍历到左括号,则入栈
S3:如果当前遍历到右括号,则出栈一个元素,看其是否与当前的右括号组成一对,如果不是,则匹配失败。或者在出栈过程中发生异常(从空栈中出栈),也匹配失败
S4:若能顺利遍历完成,检查栈中是否还有剩余元素,如果有,则匹配失败;如果没有,则匹配成功。
#define ElemType char
#define MaxSize 50
#include<stdio.h>
typedef struct {
ElemType data[MaxSize];
int top;
}SqStack;
bool StackEmpty(SqStack S){
if (S.top == -1) //栈空 return true;
else
return false; //栈不空}
bool Pop(SqStack& S, ElemType& x){
if (S.top == -1) //栈空 不能执行出栈操作 return false;
x = S.data[S.top]; //先出栈 指针再减1 S.top--;
return true;}
bool Push(SqStack& S, ElemType x){
if (S.top == MaxSize - 1) //栈满 不能执行入栈操作 return false;
S.top++; //指针先加1,再入栈 S.data[S.top] = x;
return true;}
bool GetPop(SqStack S, ElemType& x){
if (S.top == -1) //栈空报错 return false;
x = S.data[S.top]; //用x存储栈顶元素 return true;}
void initStack(SqStack& S){
S.top = -1; //初始化栈顶指针}
int main(){
SqStack S;
initStack(S);
char sequence[] = { '[','(',')',']','[',']' };
char* p = sequence;
while (*p == '\0')
{
if (*p == '(' || *p=='[')
{
Push(S, *p);
p++;
}
else
{
char getpop;
GetPop(S,getpop);
if ((getpop=='('&&*p==')')||(getpop == '[' && *p == ']')) //判断是否匹配 {
char pop;
Pop(S, pop);
p++;
}
else
{
printf("该序列不合法!");
return 0;
}
}
}
if (StackEmpty(S)) //判断最后栈是否为空 printf("该序列合法!");
else
printf("该序列不合法!");
return 0;}