算法 — 最基础のc语言算法
发布日期:2021-06-30 19:49:58 浏览次数:2 分类:技术文章

本文共 3859 字,大约阅读时间需要 12 分钟。

1、斐波拉契数列

#include 
using namespace std;int main(){ int n1 = 1, n2 = 1, display; int count; cin>>count; cout<
<
<
<

2、回文数判断

#include 
using namespace std;int main(){ int num, rem, reserve = 0, tmp; cin>>num; tmp = num; while(tmp != 0) { reserve = reserve*10 + (rem = tmp%10); tmp = tmp/10; } if(num == reserve) { cout<<"yes"<

3、判读质数

#include 
using namespace std;int main(){ /* 2\3\5\7\11 */ int num; bool flag = false; cin>>num; if(num == 1) cout<<"1既不是质数也不是合数!"<

4、打印三角形

#include
/* * 三角型 */int main(){ int rows,i,j,space; printf("Enter number of rows: "); scanf("%d",&rows); //i = rows for(i=rows;i>=1;--i){ //rows - i for(space=0;space

5、简单计算器

# include 
int main(){ char o; float num1,num2; printf("Enter operator either + or - or * or divide : "); scanf("%c",&o); printf("Enter two operands: "); scanf("%f%f",&num1,&num2); switch(o) { case '+': printf("%.1f + %.1f = %.1f",num1, num2, num1+num2); break; case '-': printf("%.1f - %.1f = %.1f",num1, num2, num1-num2); break; case '*': printf("%.1f * %.1f = %.1f",num1, num2, num1*num2); break; case '/': printf("%.1f / %.1f = %.1f",num1, num2, num1/num2); break; default: /* If operator is other than +, -, * or /, error message is shown */ printf("Error! operator is not correct"); break; } return 0;}

6、判断一个数能不能表示成两哥质数的和

#include 
int prime(int n);int main(){ int n, i, flag=0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2; i<=n/2; ++i) { if (prime(i)!=0) { if ( prime(n-i)!=0) { printf("%d = %d + %d\n", n, i, n-i); flag=1; } } } if (flag==0) printf("%d can't be expressed as sum of two prime numbers.",n); return 0; }int prime(int n) /* Function to check prime number */{ int i, flag=1; for(i=2; i<=n/2; ++i) if(n%i==0) flag=0; return flag;}

7、递归的方式颠倒字符串

#include 
void Reverse();int main(){ printf("Enter a sentence: "); Reverse(); return 0;}void Reverse(){ char c; scanf("%c",&c); if( c != '\n') { Reverse(); printf("%c",c); }}

8、十进制 与 二进制的转换

#include 
#include
int binary_decimal(int n);int decimal_binary(int n);int main() { int n; char c; printf("Instructions:\n"); printf("1. Enter alphabet 'd' to convert binary to decimal.\n"); printf("2. Enter alphabet 'b' to convert decimal to binary.\n"); scanf("%c",&c); if (c =='d' || c == 'D') { printf("Enter a binary number: "); scanf("%d", &n); printf("%d in binary = %d in decimal", n, binary_decimal(n)); } if (c =='b' || c == 'B') { printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %d in binary", n, decimal_binary(n)); } return 0;}int decimal_binary(int n) /* Function to convert decimal to binary.*/{ int rem, i=1, binary=0; while (n!=0) { rem=n%2; n/=2; //进位 binary+=rem*i; i*=10; } return binary;}int binary_decimal(int n) /* Function to convert binary to decimal.*/{ int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal;}

9、数组转置

#include 
int main(){ int a[10][10], trans[10][10], r, c, i, j; printf("Enter rows and column of matrix: "); scanf("%d %d", &r, &c); /* Storing element of matrix entered by user in array a[][]. */ printf("\nEnter elements of matrix:\n"); for(i=0; i

转载地址:https://lipenglin.blog.csdn.net/article/details/50697028 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Hadoop - 更换节点ip 地址之后(虚拟机中的伪分布模式,学习format)
下一篇:Linux — Shell

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月15日 01时29分52秒