[c++,小练习] 日常小程序_2

最近老师讲到了使用数组将输入信息进行顺序存放的思想

数组可以使得信息有序化,并且使用数组来记录状态信息,可以实现通过循环的方式来遍历状态信息



练习题 1 :

求一个数 x ,使得 x^2 的数值是一个个个位数上面的数值是均不重复的 9 个数字,即 x^2 是一个 9 位的数字。


首先,有题意可知 x 的平方是一个 9 位数的话,即可判定 x 的取值范围是 [10^4 , 32000]

根据此条件来对数值 x 进行遍历求取平方数,然后对该平方数进行判断是否是各个数字位上面的数值均是不相同的,

所以,如何判断各个位数上面的数值是不相同的呢? 条件分支判断语句实现起来十分的冗长,所以这个时候,我们

可以想到使用数组来记录 0-9 上面各个位数的状态信息。如果 d[i] = 0 表明该数字位 i 并没有被访问过,

访问之后,将其数值置为 1 。


0       1     2 3 4 5 6 7 8   9

d[0]  d[1]  ... d[8]


然后,通过对 x^2 进行%10 ,然后 /10 操作即可求取 x^2 数值上个各位数值了,源代码如下:


#include <stdio.h>
#include <cstdlib>
#include <string>

int main ( int argc , char *argv [] )
{
	long x ,xx;
	int k , d[9] ,counter = 0  ;	

	for ( x = 10000 ; x <= 32000; x++ )
	{
		//printf("analyzing number %ld \n", x ) ;
		
		xx = x*x ;
		k = 0 ;
		memset ( d , 0 , sizeof(d) ) ;

		

		while ( xx )
		{
			int temp = (int)(xx%10) ;

		//	printf("temp : %d " , temp ) ;

			if ( d[temp]== 0 )
			{
				k++ ;
				d[temp] = 1 ;
			}

			xx /= 10 ;
		} //while 

		if ( k == 9 )
		{
			printf("here are the 9 different numbers :\n ") ;

			printf("%ld square of %ld \n" , x*x , x ) ;
			counter++ ;
		}
	}

	printf("total different numbers : %d \n" , counter ) ;

	system("pause") ;
	return 0 ;

}


题目2 :

n 个小朋友围成一个圈开始做报数的游戏,报数从第一个小朋友、报数起始数值为 0,报道数字为 m 值的小朋友

应该退出圈外,且退出圈外的小朋友不参与下次的报数。每当报数的数值达到游戏最开始时圈中小朋友的数目 n

的时候,数值归 0 重新开始报数。 问题是求取圈中剩余最后一名小朋友它的编号是多少,设其编号为 i ( 0 <=i <= n-1 ).

#include <cstdio>
#include <cstdlib>
#include <string>


/**
1. int n [in] total number of friends 
2. int m [in] no. m friend should get out of the circle

return value :
the  k-th friend is the last one remains in the circle 
*/
int friendGame ( int n , int m )
{
	int *friends = new int[n];
	int k , p ;

	k = 0 ; 
	p = n ;

	memset( friends , 1 , sizeof(int) *n ) ;

	while ( p != 1  )
	{
		if (k == m )
		{
			friends[k] = 0 ;
			p-- ;
		}

		//k = (k+1)%p ;
	     k  = (k+friends[k])%n ;
/**
here , i misunderstood the meaning of the question ,
k is the number used to store the current reporting number ,
and the question is  if the number attach the value of the total number of friends
it would be the zero again , and i mistook it if the number reach the number of 
the current number of the friends  , the value of k would be zero again ,

and what i mistaken this justify condition would lead to a  dead circle :

 when the current number (not quit the circle yet) of the friends  equal the 
 m the reporting quit number , it could lead to a infinite loop .

 so here I correct the statement from k = (k+1)%p 
 into  k = (k+friends[k])%n;
 to hope it would work correctly 

 the last stupid mistake I found is that initialzing friends into value 0 

*/


	}

	delete  friends ;
	return k ;
}



int main ( int argc , char * argv [] )
{
	int n , m ;

	printf("friend number :\n") ;
	scanf("%d" , &n ) ;

	printf("number quit the circle : \n") ;
	scanf ("%d" , &m ) ;
	


	int res = friendGame( n , m ) ;

	printf("friends num : %d , quit number: %d \n" , n , m ) ;
	printf("no . %d-th (no. from 0 to %d ) remains in the circle in the end \n " , res, (n-1)) ;

	system("pause") ;
	return 0 ;
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值