c语言算法

来源:互联网 发布:java开发小游戏 编辑:程序博客网 时间:2024/06/14 15:48

¨  统计从1到100的和

¨  统计从键盘输入字符的个数

¨  请按要求实现以下菜单

  

**中心 学员成绩管理系统

            

       1    输入学员成绩      

 

              2    学员成绩修改

             

              3    学员成绩统计

             

              4    退出

   如果选择了1-4 请输出对应菜单的文字内容, 输入其他的字符,请输出:"输入错误,请输入1-4数字键!"

¨  输入一串字符串,统计0-9的个数

¨  若一个口袋中放有12个球,其中有3个红的。3个白的和6个黒的,问从中任取8个共有多少种不同的颜色搭配?

¨  如果一个数恰好等于它的因子之和,则称该数为“完全数,求出1000以内的完全数

¨  打印1000 ~ 2000 之间的闰年。(能被4整除而不能被100整除或者能被400整除的为闰年)

¨  输入一个5位数,判断它是不是回文数。(回文数,如:12321)(不限定位数呢?)

¨  求具有abcd=(ab+cd)*(ab+cd)性质的四位数

¨  某人岁数的3次方是四位数,4次方是六位数,且知此人岁数的3次方和4次方用遍了0~9十个数字。编写一个程序求此人的岁数。

¨  有一个四位数,千位上的数字和百位上的数字都被擦掉了,已知十位上的数字是1,个位上的数字是2,又知道这个数如果减去7就能被7整除,减去8就能被8整除,减去9就能被9整除。编写一个程序求这个四位数。


按题目顺序做的。


#include "stdafx.h"
#include<cstdlib>


int _tmain(int argc, _TCHAR* argv[])
{
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;  
printf("1-100之和:%d\n",sum );
system("pause");

return 0;
}


#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char str[100];
gets_s(str);
printf("字符串长度是: %d\n",strlen(str));
system("pause");


return 0;
}


#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("=======**中心 学员成绩管理系统======\n");
printf("=======**                     ======\n");
printf("=======**  1    输入学员成绩  ======\n");
printf("=======**  2    学员成绩修改  ======\n");
printf("=======**  3    学员成绩统计  ======\n");
printf("=======**  3    学员成绩统计  ======\n");
printf("=======**  4    退出          ======\n");
printf("=======** ==========================\n");
int num;
while (1){
scanf_s("%d",&num);
if (num==1)printf("你输入的是 1    输入学员成绩\n");
    else if (num == 2)printf("你输入的是 2    学员成绩修改\n");
    else if (num == 3)printf("你输入的是 3    学员成绩统计\n");
else if (num == 4)
{
printf("你输入的是 4    退出\n");
break;
}
else
{
printf("输入错误,请输入1-4数字键!\n");
break;
}
}
system("pause");


return 0;
}




#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char str[100];
int count[11] = {0};
gets_s(str);
int i;
for (i = 0; i < strlen(str); i++)
{

//printf("str[i] =   %d    \n",str[i]-'0');
if (str[i] - '0' == 0){ count[0]++; //printf("str[i] =  %c  \n", str[i]);
}
if (str[i] - '0' == 1){ count[1]++;  }
if (str[i] - '0' == 2){ count[2]++;  }
if (str[i] - '0' == 3){ count[3]++;  }
if (str[i] - '0' == 4){ count[4]++;  }
if (str[i] - '0' == 5){ count[5]++;  }
if (str[i] - '0' == 6){ count[6]++;  }
if (str[i] - '0' == 7){ count[7]++;  }
if (str[i] - '0' == 8){ count[8]++;  }
if (str[i] - '0' == 9){ count[9]++;  }

}
for (i = 0; i < 10; i++)
    printf("%d 的个数:  %d  \n", i,count[i]);




system("pause");


return 0;
}




#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
int red, black, whiet;
int count = 0;

for (red = 0; red < 4; red++)
{
for (black = 0; black < 7; black++)
{
for (whiet = 0; whiet < 4; whiet++)
{
if (red + black + whiet == 8)
{
count++;
printf("red=%d,black=%d,whiet=%d\n", red, black, whiet);
}
}


}


}
printf("共有: %d\n", count);
system("pause");


return 0;
}




#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
int i, n, m;
for (i = 1; i<1000; i++)
{
m = 0;
for ( n = 1; n <= i / 2; n++)
{
if (i%n == 0)
{
m += n;
if (i == m)
{

printf(" num=%d \n",i);
break;
}
}



}

}
system("pause");


return 0;
}


#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
int i;
for (i = 1000; i<=2000; )
{
if (i % 400 == 0)
{
printf("%d 是闰年\n",i);
}


else if ( (i%4==0) &&(i%100!=0) ) 
printf("%d 是闰年\n", i);
i += 4;


}
system("pause");


return 0;
}




#include "stdafx.h"
#include<cstdlib>
#include<string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char str[100];
int i, j;
gets_s(str);
int mark = strlen(str)%2;//判断奇数偶数


if (mark == 0) j = strlen(str) / 2 ;//找中间数,从中间开始2边找
else j = strlen(str) / 2 + 1;
int flag = 1;
for (i = strlen(str) / 2 -1; i >= 0; i--)
{
if (str[i] == str[j])j++;
else {
printf("不是回文数\n"); 
flag=0;//只要有一个不符合,就不会往下执行。
break;
}
}
if (flag==1)printf("是回文数\n");
system("pause");


return 0;
}


#include "stdafx.h"
#include<cstdlib>
int main()
{
int n, a, b;
for (n = 1000; n<10000; n++) //四位数N的取值范围1000~9999
{
a = n / 100; //截取N的前两位数存于a
b = n % 100; //截取N的后两位存于b
if ((a + b)*(a + b) == n) //判断N是否为符合题目所规定的性质的四位数
printf("%d \t", n);
}


system("pause");


return 0;
}


#include "stdafx.h"
#include<cstdlib>
int main()
{
int n, a, b,num;
int i;
for (n = 1; n<10; n++)
{
for (i = 0; i < 10; i++)
{
num = n * 1000 + i * 100 + 12;
if ((num-7) % 7 == 0)
{
if ((num-8) % 8 == 0)
{
if ((num-9) % 9 == 0)
{
printf("%d \n", num);
break;
}
}
}


}




}


system("pause");


return 0;
}








#include "stdafx.h"
#include<cstdlib>
int main()
{
int n,i,j;
int a, b;
int count[10];
int  stra[4] = {0};
int  strb[6] = { 0 };
for (n = 18; n<25; n++) 
{
a = n*n*n;
b = n*n*n*n*n;

int ii = 3;//int数字化W为字符数组
while (a)
{
stra[ii] = (int)a % 10;
a = a / 10;
ii--;
}


int jj = 5;
while (b)
{
strb[jj] = (int)b % 10;
b = b / 10;
jj--;
}

int mark = 1;//假设每一轮开始都是对的,
for (i = 0; i < 6; i++)
{
for (j = 0; i < 4; j++)
{
if (strb[i] == stra[j])//一旦发现重复的立即退出
{ mark = 0; break; }
}
if (mark == 0)break;//重复的,退出,开始下一轮
}
if (mark == 1)//找到 了退出
{
printf("%d\n", n); 
break;
}




}


system("pause");


return 0;
}

原创粉丝点击