algorithm test

本文介绍了一个C语言程序,该程序能够接收一系列四位数,并检查通过改变每一位数字是否可以使这些数变成某个整数的平方。程序实现了全面的检查机制,包括对千位、百位、十位和个位数的修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入一个四位数,改变其中一位,使其变为一个数的平方

#include<stdio.h>
#include<math.h>
int check(int n);


int main(void)
{
int i,line;
int num[1000];
int result[1000]; 

printf("enter number (1<=num<=1000)\n");
scanf("%d", &line);
printf("enter %d number (1000<=num<=9999)\n",line);
for (i = 0; i < line; i++)  //输入line个整数
{
int n;
scanf("%d", &n);  
num[i] = n;
}
for ( i = 0; i < line; ++i)  
result[i]=check(num[i]);
for(i=0;i<line;i++)
printf("case %d :%d\n",i+1,result[i] );


}


int check(int n)
{
int count1=0,temp,time,q,b,s,g;
q=n/1000;
b=(n-q*1000)/100;
s=(n-(q*1000+b*100))/10;
g = (n - (q * 1000 + b * 100 + s * 10));

for(temp=n,time=0;time<9-q;temp+1000,time++)//改变千位数,往上加
{
float t = (sqrt(temp));
if (t - (int)t == 0)
count1++;
temp = (temp+1000);
}
for(temp=n,time=0;time<q-1;time++)//改变千位数,往下减
{
float t= (sqrt(temp));
if(t-(int)t==0)
count1++;
temp=(temp - 1000);
}

for(temp=n,time=0;time<9-b;time++)//改变百位数,往上加
{
if(sqrt(temp)==(int)sqrt(temp))
count1++;
temp = temp + 100;
}
for(temp=n,time=0;time<b;time++)//改变百位数,往下减
{
if (sqrt(temp) == (int)sqrt(temp))
count1++;
temp = temp-100;
}
for (temp=n,time=0;time<9-s; time++)//改变十位,往上加
{
if(sqrt(temp)==(int)sqrt(temp))
count1++;
temp = temp + 10;
}
for (temp=n,time=0;time<s;  time++)//改变十位,往下减
{
if(sqrt(temp)==(int)sqrt(temp))
count1++;
temp = temp -10;


}
for (temp=n,time=0;time<9-g;temp++,time++)//改变个位,往上加
{
if(sqrt(temp)==(int)sqrt(temp))
count1++;
}
for (temp=n,time=0;time<g; temp--,time++)//改变个位,往下减
{
if(sqrt(temp)==(int)sqrt(temp))
count1++;
}


return count1;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值