1.很杂,关于逗号,小数点位数
#include<stdio.h>
int main() {
int a;
int i,m,n;
float x,y;
x = 13 / 3;
y = 13 / 3.0;
char ch;
ch = 'A' + '6' - '3';
i = ((a = 2 * 3, a * 5), a + 6);
m = 9, 8, 7;
n = (9, 8, 7);
printf("%d ; %c\n", ch,ch);
printf("i=%d a=%d\nm=%d n=%d\nx = % f y=%lf", i,a,m,n,x,y);
return 0;
}
68 ; D
i=12 a=6
m=9 n=7
x = 4.000000 y=4.333333
以下代码都用函数方法定义一个使用,,尽量
2.闰年判断
#include<stdio.h>
int main() {
int RUNNIAN(int a,int i);
int a,m,i=0;
printf("输入一个年份");
scanf_s("%d", &a);
m=RUNNIAN(a,i);
if (m != 0)
{printf("是闰年");}
else
{ printf("不是闰年"); }
return 0;
}
int RUNNIAN(int a,int i) {
if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) i = 1;
else i = 0;
return (i);
}
不用m,直接在if里判断
if (RUNNIAN(a, i))
{printf("是闰年");}
else
{ printf("不是闰年"); }
3.整除,用不来函数
#include<stdio.h>
int main() {
int a, b, i;
scanf_s(" %d %d %d", &a, &b, &i);
while(a<b) {
if(a%i==0)
printf("%5.2d", a);
a++;
}
return 0;
}
4.递加三角,奇数递增
#include<stdio.h>
int main() {
int a,i,j,k=1;//k代表现层数
scanf_s("%d", &a);
while (k <= a) {
i = j = 0;
while (i <= a - k) {
printf(" "); i++;
}
while (j < k * 2 - 1) {
printf("*"); j++;
}
printf("\n");
k++;
}
return 0;
}
如果i=j=1,则会输出偶数段,即*的个数为0,2,4。。。
5.水仙花,三位数上每个数字的三次方之和等于数本身
#include<stdio.h>
#include<math.h>
int main() {
int n;
int a, b, c;//各位数
for (n = 100; n <= 999; n++) {
a = n / 100;
b = n % 100 / 10;
c = n % 100 % 10;
if ((pow(a, 3) + pow(b, 3) + pow(c, 3)) == n) printf("%d\n", n);
}
return 0;
}
6,最小公倍数,最大公约数
注意!上面的运行不出来,下面的是对的!!!
#include<stdio.h>
int gy(int x, int y) {//辗转相除法;
int r;
if (x < y)
{ r = x; x = y; y = r; }
r = x % y;
while (r != 0) {
x = y; y = r; r = x % y;
}
return (r);
}
int gb(int x, int y) {
int gy(int x, int y);
int p;
p = x * y / gy(x,y);
return (p);
}
int main() {
int a, b, c,d;
int gy(int x, int y);
int gb(int x, int y);
printf("please enter the data you want to handle:");
scanf_s("%d,%d", &a, &b);
c = gy(a, b);
d = gb(a, b);
printf("gy=%d,gb=%d", c,b);
return 0;
}
#include<stdio.h>
int yue(int a, int b)
{
int w, t;
if (a < b)
{
t = a;
a = b;
b = t;
}
while (b != 0)
{
w = a % b;
a = b;
b = w;
}
return a;
}
int bei(int a, int b)
{
return a * b / yue(a, b);
}
int main()
{
int a, b, c, d;
scanf_s("%d %d", &a, &b);
c = yue(a, b);
d = bei(a, b);
printf("%d\n%d", c, d);
return 0;
}
7,1+1/3+1/5+......
#include<stdio.h>
int main() {
double sum=0.0,a=1.0;
int b = 1;
int n;
scanf_s("%d", &n);
while (b <= n) {
sum = sum + a / (2*b-1);
b++;
}
printf("%lf", sum);
return 0;
}
8.1+1/(1+2)+1/(1+2+3)+....
a=1,0;
b=0;sum=o;
while ....... a=a+b;
sum=sum+1.0/a;
b++;
9.从大到小排序,求和,取极差 打擂台,冒泡,选择法
我也不知道这个是什么方法。。。
#include<stdio.h>
#include<math.h>
int main() {
int a;
int m[80] = {0};
scanf_s("%d", &a);
for (int i = 0; i < a; i++) {
scanf_s("%d",& m[i]);
}
int k, j,t,max,b=0;
k = 0; t = 0;
max = m[k];
for (k; k < a; k++) {
for (j = k + 1; j < a; j++) {
max = m[k] > m[j] ? m[k] : m[j];
if (max != m[k]) {
b = m[k]; m[k] = m[j]; m[j] = b;
}
}
}
for (int i = 0; i < a; i++) {
printf("%d ", m[i]);
}
return 0;
}
for(i=0;i<a;i++)
sum=sum+m[i];
jicha=abs(m[0]-m[a]);
10.矩阵转置及其元素最大值
#include<stdio.h>
int main() {
int a[80][80] = { 0 }, b[80][80] = { 0 };
int i, j;
int m, n;
scanf_s("%d %d", &i, &j);
for (m=0; m < i; m++) {
for (n=0; n < j; n++) {
scanf_s("%d", &a[m][n]);
}
}
for (m = 0; m < i; m++) {
for (n = 0; n < j; n++) {
printf("%d ", a[m][n]);
}
printf("\n");
}
for (n = 0; n < j; n++){
for (m = 0; m < i; m++) {
printf("%d ",a[m][n]);
}
printf("\n");
}
int max,c=0,d=0;
max = a[0][0];
for (m = 0; m < i; m++) {
for (n = 0; n < j; n++) {
if (max < a[m][n]) max = a[m][n]; c = m; d = n;
}
}
printf("max=a[%d][%d]=%d", c+1, d+1, max);
return 0;
}
11.矩阵对角元素和
sum=sum+a[n][n];
12.斐波那契数列
#include<stdio.h>
int Fibon1(int n)
{
if (n == 1 || n == 2){
return 1;
}
else{
return Fibon1(n - 1) + Fibon1(n - 2);
}
}
int main()
{
int n = 0,i;
int ret = 0;
int Fibon1(int n);
scanf_s("%d", &n);
for (i=1; i <= n; i++) {
printf("ret=%-4d", Fibon1(i));
}
return 0;
}
13.杨辉三角
#include<stdio.h>
int main() {
int i = 0,m=0,n=0;
int a[80][80] = { 0 };
scanf_s("%d", &i);//矩形样的
for (m; m <= i; m++) {
a[m][0] = 1; a[m][m] = 1;
}
for (m = 2; m <= i; m++) {
for (n = 1; n < m; n++) {
a[m][n] = a[m - 1][n] + a[m - 1][n - 1];
}
}
for (m = 0; m <= i; m++) {
for (n = 0; n < m; n++)printf("%5d", a[m][n]);
printf("\n");
}
}
14.素数
这个循环范围没有缩小,按理说应该在i的一半或者是更号处,但是这样3就不见了???
#include<stdio.h>
#include<math.h>
int main() {
int a,b;
scanf_s("%d %d", &a, &b);
int i = a;
for (i; i <= b; i++) {
if (i % 2 == 0) continue;
for (int m = 2; m <= i; m++) {
if (i % m != 0) printf("%d\n", i);
break;
}
}
return 0;
}
15.猴子吃桃 猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?
(剩下的+1)*2=上一天剩下的
16.求Π/4=1-1/3+1/5-.....
i=-1;sum=1;a=1.0;
a=a*i;
c= a / (2*b-1);
sum = sum +c;
17.变换大小写
#include <stdio.h>
int main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
if(a[i]>='a'&&a[i]<='z')
a[i]-=32;
else if(a[i]>='A'&&a[i]<='Z')
a[i]+=32;
puts(a);
return 0;
}
/*运行结果:
abc23DEF
ABC23def
*/
18.指针!!!