仅供参考!
1.1到100求和
#include <stdio.h>
int main()
{int i,sum=0;
i=1;
while(i<=100)
{ sum=sum+i;
i++;
}
printf("%d\n",sum);
}
2.1000以内数排序(一)
#include <iostream>
using namespace std;
int main()
{
int a[1000],i,j,t,n,x;
std::cout<<"对n个数排序:"<<endl;
cin>>n;
cout<<"请输入数据"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(i=1;i<=n-1;i++)
for(j=0;j<n-i;j++)
if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
3.1000以内数排序(二)
#include <stdio.h>
int main()
{
int a[1000],i,j,t;
//scanf("输入五个数据:");
//for(i=0;i<5;i++) scanf("%d",&a[i]);//输入数据
printf("请输入这些数字,中间用空格隔开 :\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=1;i<4;i++)//控制趟数 i=0时候 没有值 要从1开始 n-2趟
{
for(j=0;j<5-i;j++)
if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}//比较大小并掉换
}
printf("这些数字由小到大的顺序是:\n");//输出数据
for(i=0;i<5;i++) printf("%d\n",a[i]) ;
}
4.求n的阶层(递归方法)
#include <stdio.h>
float jieceng(int n)
{ float y=0;
if(n<0) printf("Error!");
else if(n==0||n==1)
y=1;
else y=n*jieceng(n-1);
return (y);
}
int main()
{
int n;float x;
printf("输入n:");
scanf("%d",&n);
x=jieceng(n);
printf("%d!=%-10.0f\n",n,x);
return 0;
}
5.输入两个数比较大小排序输出
# include <stdio.h>
int main()
{
int x,y,t;
scanf("%d%d",&x,&y);
if(x>y)
{t=x;x=y;y=t;}
printf("%d,%d\n",x,y);
return 0;
}
6.输入两个数比较大小后输出Max
#include <stdio.h>
int main()
{int x,y,max;
scanf("%d%d",&x,&y);
if(x>y)
max=x;
else
max=y;
printf("max=%d\n",max);
return 0;
}
/*调用函数*/
#include <stdio.h>
int max(int a,int b){
int z;
if(a<b) z=b;
else z=a;
return(z);
}
int main() {
int a,b,x;
scanf("%d,%d",&a,&b);
x=max(a,b);
printf("max=%d\n",x);
return 0;
}
7.输入成绩后输出成绩等级
#include <stdio.h>
int main()
{ int score;
scanf("%d",&score);
if(score<0||score>100)
printf("Error");
else
switch(score/10)
{ case 10:
case 9:printf("Score grade is A\n");break;
case 8:printf("Score grade is B\n");break;
case 7:printf("Score grade is C\n");break;
case 6:printf("Score grade is D\n");break;
default:printf("Score grade is E\n");break;
}
return 0;
}
#include <stdio.h>
int main()
{int score;
scanf("%d",&score);
printf("你的等级:");
if(score<0||score>100)
printf("Error");
else
switch(score/10)
{case 10:printf("A\n");break;
case 9:printf("A\n");break;
case 8:printf("B\n");break;
case 7:printf("C\n");break;
case 6:printf("D\n");break;
case 5:printf("E\n");break;
case 4:
case 3:
case 2:
case 1:
default:printf("输入你的成绩");break;
}return 0;
}
8.插入数据后仍未有序数列
#include <stdio.h>
int main()
{ int i,a[5],x;
printf("从小到大输入五个数据 :\n");
for(i=0;i<5;i++) scanf("%d",&a[i]);
printf("请输入一个要插入的整数:\n");
scanf("%d",&x);
for(i=4;i>=1;i--)
{
if(x<a[i]) {a[i+1]=a[i]; }
else break;
}
a[i+1]=x;
printf("这些数字的顺序是:\n");//输出数据
for(i=0;i<6;i++) printf("%d\n",a[i]);
}
9.大小写转换
#include <stdio.h>
int main()
{char ch;
ch=getchar();
if(ch>='A'&&ch<='Z')
ch=ch+32;
printf("%cn",ch);
return 0;
}
10.分段函数
#include <stdio.h>
int main(){
int x,y;
printf("输入x的值:");
scanf("%d",&x);
if(x<1){
y=x;
printf("x=%d,y=%d\n",x,y);
}
else if(1<=x<10) {
y=2*x-1;
printf("x=%d,y=%d\n",x,y);
}
else {
y=3*x-11;
printf("x=%d,y=%d\n",x,y);}
}
11.输入数字并输出所有位和
#include <iostream>
using namespace std;
int add_num(int n);//声明功能函数
int main() {
int n;
cin >> n;
cout << add_num(n);
return 0;
}
int add_num(int n) {
if (n < 10) return n;
else return add_num(n / 10) + n % 10;
}
#include <stdio.h>
int f(int n){
if (n < 10)
return n;
else
return f(n / 10) + n % 10;
}
int main (){
int n,sum;
printf("输入数据n:");
scanf("%d",&n);
sum=f(n);
printf("%d\n",sum);
}
12.角谷定理
角谷定理:输入一个自然数,若为偶数,则把它除以2,若为奇数,则把它乘以3加1。经过如此有限次运算后,总可以得到自然数值1。求经过多少次可得到自然数1?
#include <stdio.h>
#include<iostream>
using namespace std;
int Kakutani(int n) {
int count = 0;
cout << n<<" "; //输出所有数字r
count++; //计数
//如果n=1 退出
if (n == 1) {
return count;
}
else {
if (n % 2 == 0) { //若是偶数
count += Kakutani(n / 2);
return count;
}
else {
count += Kakutani(n * 3 + 1);
return count;
}
}
}
int main() {
int n = 0;
cout << "请输入数字:";
cin >> n;
cout<<endl<<"step="<<Kakutani(n)<<endl;
system("pause");
return 0;
}
|