NOJ 11-20题 循环
#include <stdio.h>
#include <math.h>
int main()
{
double a,eps = 1,b,c;
scanf("%lf", &a);
b = a;
while(eps > 1e-5){
c = (1 / 2.0) * (b + a / b);
eps = abs(c - b);
b = c;
}
printf("%.5lf", c);
}
#include <stdio.h>
int main()
{
int n,a;
scanf("%d", &n);
if(n == 1){
printf("1*1*1=1=1");
return 0;
}
int sum = 0;
for (int i = 1; i < n * n * n;i = i + 2){
sum = 0;
for (int j = 0; j < n;j++){
sum = sum + i + 2 * j;
}
if(sum == n * n * n){
a = i;
break;
}
}
printf("%d*%d*%d=%d=", n, n, n, sum);
for (int i = 0; i < n - 1;i++){
printf("%d+", a + i * 2);
}
printf("%d", a + (n - 1) * 2);
return 0;
}
#include <stdio.h>
int main()
{
char i, j, k;
for (i = 'X'; i <= 'Z'; i++)
{
for (j = 'X'; j <= 'Z'; j++)
{
if (i != j)
{
for (k = 'X'; k <= 'Z'; k++)
{
if (i != k && j != k)
{
if (i != 'X' && k != 'X' && k != 'Z')
{
printf("A=%c\nB=%c\nC=%c", i, j, k);
}
}
}
}
}
}
}
#include <stdio.h>
#include <math.h>
int main()
{
int n;
double a = 0;
scanf("%d", &n);
for (int i = 1; i <= n;i++){
a += (pow(-1, i - 1)) * pow(2, i) / ((pow(-1, i) + pow(2, i)) * (pow(-1, i + 1) + pow(2, i + 1)));
}
printf("%lf", a);
}
#include <stdio.h>
#include <math.h>
int main()
{
double max = 0;
int a1, a2, a3, a4, a5;
for (int i = 0; i < 3; i++) //8年
{
for (int j = 0; j <= (20 - i * 8) / 5; j++) //5年
{
for (int k = 0; k <= (20 - i * 8 - j * 5)/3;k++) //3年
{
for (int m = 0; m <= (20 - i * 8 - j * 5 - 3 * k)/2;m++) // 2年
{
for (int n = 0; n <= 20 - i * 8 - j * 5 - 3 * k - 2 * m; n++) // 1年
{
double money = 2000.0 * pow((1 + 0.0063 * 12), n) * pow((1 + 2 * 0.0063 * 12), m) * pow((1 + 3 * 0.0069 * 12), k) * pow((1 + 5 * 0.0075 * 12), j) * pow((1 + 8 * 0.0084 * 12), i);
if(money > max){
a1 = i;
a2 = j;
a3 = k;
a4 = m;
a5 = n;
max = money;
}
}
}
}
}
}
printf("%d %d %d %d %d\n", a1, a2, a3, a4, a5);
printf("%.2f", max);
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c = 1;
scanf("%lf%lf", &a, &b);
while(abs(2 * c * c * c - 4 * c * c + 3 * c - 6) > 1e-5){
c = (b + a) / 2;
if (2 * c * c * c - 4 * c * c + 3 * c - 6 > 0){
b = c;
}
else if (2 * c * c * c - 4 * c * c + 3 * c - 6 < 0){
a = c;
}
}
printf("%.2lf", c);
}
#include <stdio.h>
#include <math.h>
int main()
{
double a;
scanf("%lf", &a);
int b = trunc(a);
int count = 0;
while(b != 0){
b /= 10;
count++;
}
printf("%d", count);
}
#include <stdio.h>
#include <math.h>
int main()
{
int s = 2;
int a1 = 1, a2 = 1 ,a3 = 2;
int count = 2;
int f1 = 0,f2 = 0,f3 = 0;
while(s < 10000){
a3 = a1 + 2 * a2;
a1 = a2;
a2 = a3;
s += a3;
count++;
if(s >= 100 && f1 == 0){
printf("%d\n", count - 1);
f1 = 1;
}
if(s >= 1000 && f2 == 0){
printf("%d\n", count - 1);
f2 = 1;
}
if(s >= 10000 && f3 == 0){
printf("%d", count - 1);
f3 = 1;
}
}
}
#include <stdio.h>
int main()
{
int last = 1;
int first = 1;
int all = 1;
for (int i = 0; i < 4;i++){
first = last;
for (int j = 0; j < 4;j++)
{
all = first * 5 + 1;
if(all % 4 != 0){
last++;
i = 0;
break;
}
first = all / 4;
}
}
printf("%d %d",first * 5 + 1,last * 4);
}
#include <stdio.h>
int main()
{
int a, b;
int ans = 1;
scanf("%d%d", &a, &b);
for (int i = 0; i < b;i++){
ans = (ans * a) % 1000;
}
printf("%d", ans);
}