您的位置:首页 > 其它

基础练习03

2018-03-31 11:07 99 查看
1.将数组A的内容和数组B的内容交换
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int A[] = { 1, 2, 3, 4 };
int B[] = { 7, 8, 9, 10 };
int tmp = 0;
int i = 0;
int size = sizeof(A) / sizeof(A[0]);
for (i = 0; i < size; i++)
{
tmp = A[i];
A[i] = B[i];
B[i] = tmp;
}
for (i = 0; i < size; i++)
{
printf("%d ", A[i]);
}
printf("\n");
for (i = 0; i < size; i++)
{
printf("%d ", B[i]);
}
system("pause");
return 0;
}
2.计算1/1-1/2+1/3-1/4......+1/99-1/100的值。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
double sum = 0.0;
int flag = 1;
int i = 0;
for (i = 1; i <= 100; i++)
{
if (i % 2 == 0)
sum += -flag*(1.0 / i);
else
sum += flag*(1.0 / i);
}
printf("%1f\n", sum);
system("pause");
return 0;
}
3.编写程序数一下1到100的所有整数中出现多少次数字9.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
if (i % 10 == 9 || i / 10 ==9)
{
count ++;
}
}
printf("%d\n", count);
system("pause");
return 0;
}
4.在屏幕上输出以下图案:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 0;
int j = 0;
int l = 0;
scanf("%d\n", &l);
for (i = 0; i < l; i++)
{
for (j = 0; j > l-1-i; j++)
{
printf(" ");
}
for (j = 0; j < 2*i+1; j++)
{
printf("*");
}
printf("\n");
}
for (i = 0; i < l-1; i++)
{
for (j = 0; j > l-1-i; j++)
{
printf(" ");
}
for (j = 0; j < 2*(l-1-i)-1; j++)
{
printf("*");
}
printf("\n");
}
system("pause");
return 0;

}
//菱形
//#include<stdio.h>
//#include<stdlib.h>
//int main()
//{
// int line = 0;
// int i = 0;
// scanf("%d", &line);
//
// //打印上半部分
// for (i = 0; i < line; i++)
// {
// //打印空格
// int j = 0;
// for (j = 0; j < line - 1 - i; j++)
// {
// printf(" ");
// }
// for (j = 0; j < 2*i+1; j++)
// {
// printf("*");
// }
// printf("\n" );
// }
// //打印下半部分
// for (i = 0; i < line; i++)
// {
// //打印空格
// int j = 0;
// for (j = 0; j <= i; j++)
// {
// printf(" ");
// }
// for (j = 0; j < 2*(line-1-i)-1; j++)
// {
// printf("*");
// }
// printf("\n");
// }
// system("pause");
// return 0;
//}

2.求出0~999之间的所有“水仙花数”并输出。“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3 ? ,则153是一个“水仙花数”。
/*
在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数。
例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:
153 = 1^3 + 5^3 + 3^3。
370 = 3^3 + 7^3 + 0^3。
371 = 3^3 + 7^3 + 1^3。
407 = 4^3 + 0^3 + 7^3。
*/
#include<stdio.h>
#include<stdlib.h>
//方法1(只计算3位数的水仙花数)
int main()
{
int count = 0;
int i = 0;
int sum;
int a, b, c;
for (i = 100; i < 1000; i++)
{
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
sum = a*a*a + b*b*b + c*c*c;
if (i == sum)
{
printf("%d ", i);
count++;
}
}
printf("\n");
printf("count=%d\n", count);
system("pause");
return 0;
}
//方法2
int main()
{
int line = 0;
int i = 0;
for (i = 0; i <= 10000; i++)
{
//计算位数
int tmp = i;
int count = 0;
int sum=0;
while (i)
{
count++;
tmp = tmp / 10;
}
//计算和
tmp = i;
while (tmp)
{
sum += pow(tmp % 10, count);//求次方
tmp = tmp / 10;
}
if (sum == i)
{
printf("%d ", sum);
}
}
system("pause");
return 0;
}

3.
求Sn = a + aa + aaa + aaaa + aaaaa的前5项之和,其中a是一个数字,例如:2 + 22 + 222 + 2222 + 22222
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 0;
int n = 0;
int sum = 0;
int k = 0;
scanf("%d,%d\n", &a,&n);
for (int i = 0; i < n; i++)
{
k = k * 10 + a;
sum = sum + k;
}
printf("%d\n", sum);
system("pause");
return 0;
}
//方法2
//#include<stdio.h>
//#include<stdlib.h>
//#include<math.h>
//int main()
//{
// int line = 0;
// int i = 0;
// for (i = 0; i <= 10000; i++)
// {
// //计算位数
// int tmp = i;
// int count = 0;
// int sum=0;
// while (i)
// {
// count++;
// tmp = tmp / 10;
// }
// //计算和
// tmp = i;
// while (tmp)
// {
// sum += pow(tmp % 10, count);//次方
// tmp = tmp / 10;
// }
// if (sum == i)
// {
// printf("%d ", sum);
// }
// }
// system("pause");
// return 0;
//}

4.编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ch = 0;
int count = 0;
while((ch = getchar()) != EOF)
{
if (ch == '{')
{
count ++;
}
else if (ch == '}'&&count == 0)
{
printf("不匹配\n");
return 0;
}
else if (ch == '}'&& count != 0)
{
count--;
}
}
if (count == 0)
{
printf("匹配\n");
}
else
{
printf("不匹配\n");
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: