好吧,所以,我现在正在为使用C语言的课程做一些功课,在本练习中,我需要对Kaprekar的Constant进行应用(在经过几次迭代之后,您总是得到6174的数字)相同的算法)。
输入是1到10000之间的数字n(不包括10000)。如果n的位数少于4位,则必须将其加0直到得到4位。必须从此数字中产生两个变量(出于明显的原因,我将其称为asc和des):des以数字降序对数字进行重新排序,而asc是以数字升序对数字进行重新排序(对于asc,必须包含前导零。然后n将取des-asc的值,并且该过程将一直重复进行直到获得数字6174。然后,程序必须输出所花费的迭代次数以及计算本身,以达到6174。
除了重新排序外,我还能做其他所有事情(如下面添加的代码所示)。我可能也可以做这些,但是由于不允许我们使用数组,所以练习又复杂了一步。请问有人有什么想法吗?
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, asc, des, k = 0;
scanf("%d", &n);
while (n != 6174) {
des = /*this is where I'm stuck */
asc = /* this is also where I'm stuck */
n = des - asc;
printf("%d, "-" %d "=" %d, des, asc, n);
k++;
}
printf(%d, k);
return 0;
}
答案 0 :(得分:0)
您的代码中有很多错误。
更改
#inlcude <stdio.h> // wrong spelling
到
#include <stdio.h>
更改
printf("%d, "-" %d "=" %d, des, asc, n); // wrong format, too many "s
收件人
printf("%d - %d = %d\n", des, asc, n);
何时编写代码,请确保您没有犯任何语法错误。
修改后的代码将起作用:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, asc, des, k = 0;
scanf("%d", &n);
while (n != 6174)
{
int arr[4] = {0, 0, 0, 0}, i = 0, j, temp;
temp = n;
// separating digits
while (temp > 0)
{
arr[i] = temp % 10;
temp = temp / 10;
i++;
}
// sorting
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
asc = arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
des = arr[0] + arr[1] * 10 + arr[2] * 100 + arr[3] * 1000;
n = des - asc;
printf("%d - %d = %d\n", des, asc, n);
k++;
}
printf("%d", k);
return 0;
}
输出:-
1369
9631 - 1369 = 8262
8622 - 2268 = 6354
6543 - 3456 = 3087
8730 - 378 = 8352
8532 - 2358 = 6174
5
答案 1 :(得分:0)
这确实是一个很好的练习,可以帮助您取得进步,但是如果不使用数组或字符串(它们也是数组的一种),那么添加前导零可能真的很复杂,甚至是绝对不可能的。像这样的代码中的字符串(也许您的老师会接受这个-:)):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SORT_ASC 0
#define SORT_DESC 1
static void purger(void);
static void clean (char *chaine);
static int getNumber(void);
static void insertionSort(char string[],int order);
int main ( int argc, char** argv )
{
int again=1;
printf("Welcome To an application of The Kaprekars's Constant!!!\n\n");
while(again){
int number=0,ascending,descending,k=0,length,u,v;
char asc[5],desc[5]={'0','0','0','0'},complete[5]={'0','0','0','0'},tmp;
while(number<1||number>9999)
{
printf("Please Enter a number between 1 and 10000? ");
number=getNumber();
}
while(number!=6174)
{
char numberinstr[5];
sprintf(numberinstr,"%d",number);
length=strlen(numberinstr);
if(length<4)
{
for(u=0;u<length;u++)
{
tmp=numberinstr[u];
complete[u+(4-length)]=tmp;
}
strcpy(numberinstr,complete);
}
insertionSort(numberinstr,SORT_ASC);
strcpy(asc,numberinstr);
for(u=3,v=0;u>=0;u--,v++){
desc[v]=asc[u];
}
sscanf(asc, "%d", &ascending);
sscanf(desc, "%d", &descending);
number=descending-ascending;
printf("%s - %s = %d\n",desc,asc,number);
k++;
}
printf("6174 reached after %d iterations\n Do you want to choose another number?\n Press:\n 1 for Yes \n 0 for No \n",k);
again=getNumber();
while(again!=0&&again!=1)
{
printf("Please Enter a valid option!!!\n");
again=getNumber();
}
}
return 0;
}
static void purger(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
{}
}
static void clean (char *chaine)
{
char *p = strchr(chaine, '\n');
if (p)
{
*p = 0;
}
else
{
purger();
}
}
static int getNumber(void){
char chaine[100];
int nombre;
int ret=0;
int i=0;
while (ret != 1) {
if(i) printf("Veuillez Entrer uniquement des chiffres: ");
fgets(chaine, sizeof chaine, stdin);
clean(chaine);
ret = sscanf(chaine, "%d", &nombre);
i++;
}
return nombre;
}
static void insertionSort(char string[],int order){
int x,c,i,n,m;
char temp=0;
char chaine[2];
char chaineMoinsUn[2];
if(order==0){
for(x=0,c=4;x<c;x++)
{
for(i=x;i>0;i--)
{
chaine[0]=string[i];
chaineMoinsUn[0]=string[i-1];
sscanf(chaine, "%d", &n);
sscanf(chaineMoinsUn, "%d", &m);
if(n<m)
{
temp=string[i];
string[i]=string[i-1];
string[i-1]=temp;
}
else
{
break;
}
}
}
}
else
{
for(x=0,c=4;x<c;x++)
{
for(i=x;i>0;i--){
chaine[0]=string[i];
chaineMoinsUn[0]=string[i-1];
sscanf(chaine, "%d", &n);
sscanf(chaineMoinsUn, "%d", &m);
if(n>m)
{
temp=string[i];
string[i]=string[i-1];
string[i-1]=temp;
}
else
{
break;
}
}
}
}
}
此代码至少要做三件事,首先运行一个无限循环以允许输入多个数字,并给出是否退出的选择,它检查用户输入以确保用户确实输入了数字,然后检查输入小于10000且大于0,最后将进行排序并进行迭代,直到达到6174。然后按预期方式打印结果,并以前导000和迭代次数进行打印。
经过深思熟虑,即使不使用字符串或数组,您也可以得到相同的结果,但是前导零将仅用于打印:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void purger(void);
static void clean (char *chaine);
static int getNumber(void);
static int intlength(int number);
static int int_asc(int number,int length);
static int int_desc(int number,int length);
int main ( int argc, char** argv )
{
int again=1;
printf("Welcome To an application of The Kaprekars's Constant!!!\n\n");
while(again){
int number=0,asc,desc,k=0,length,u,breakbefore=0;
while(number<1||number>9999)
{
printf("Please Enter a number between 1 and 10000? ");
number=getNumber();
}
while(number!=6174)
{
length=intlength(number);
u=4-length;
asc=int_asc(number,length);
desc=int_desc(number,length);
if(asc==desc&&length==4){
breakbefore=1;
printf("We can never reach 6174 with a number formed of same digit!!!");
break;
}
if(length<4){
while(u>0){
desc*=10;
u--;
}
}
number=desc-asc;
switch(intlength(asc)){
case 3:
printf("%d - 0%d = %d\n",desc,asc,number);
break;
case 2:
printf("%d - 00%d = %d\n",desc,asc,number);
break;
case 1:
printf("%d - 000%d = %d\n",desc,asc,number);
break;
default:
printf("%d - %d = %d\n",desc,asc,number);
break;
}
k++;
}
if(breakbefore==1){
printf("\n Do you want to choose another number?\n Press:\n 1 for Yes \n 0 for No \n");
}else{
printf("6174 reached after %d iterations\n Do you want to choose another number?\n Press:\n 1 for Yes \n 0 for No \n",k);
}
again=getNumber();
while(again!=0&&again!=1)
{
printf("Please Enter a valid option!!!\n");
again=getNumber();
}
}
}
static void purger(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
{}
}
static void clean (char *chaine)
{
char *p = strchr(chaine, '\n');
if (p)
{
*p = 0;
}
else
{
purger();
}
}
static int getNumber(void)
{
char chaine[100];
int nombre;
int ret=0;
int i=0;
while (ret != 1)
{
if(i) printf("Veuillez Entrer uniquement des chiffres: ");
fgets(chaine, sizeof chaine, stdin);
clean(chaine);
ret = sscanf(chaine, "%d", &nombre);
i++;
}
return nombre;
}
static int intlength(int number)
{
int i,j=0,temp;
for(i=0;i<=9;i++){
for (temp=number;temp>0;temp/=10)
{
if(temp%10==i)
{
j++;
}
}
}
return j;
}
static int int_asc(int number,int length)
{
if(length==1)
{
return number;
}
int i,j=0,temp,asc=0,u,power;
j=length-1;
while(j>0)
{
for(i=0;i<=9;i++)
{
for (temp=number;temp>0;temp/=10)
{
if(temp%10==i)
{
for(u=j,power=1;u>0;u--)
{
power*=10;
}
asc += i*power;
j--;
}
}
}
}
return asc;
}
static int int_desc(int number,int length)
{
if(length==1)
{
return number;
}
int i,j=0,temp,desc=0,u,power;
j=length-1;
while(j>0)
{
for(i=9;i>=0;i--)
{
for (temp=number;temp>0;temp/=10)
{
if(temp%10==i)
{
for(u=j,power=1;u>0;u--)
{
power*=10;
}
desc += i*power;
j--;
}
}
}
}
return desc;
}