【pta】【Basic Practice】题目(1001~1003)(存疑)

这篇博客探讨了两道Java编程题:一是解决(3n+1)问题,利用while循环和条件判断进行计算;二是实现超长数的位数相加,通过字符串转换和遍历处理。文章提供了详细的代码示例,并给出了编程小提示,包括字符串操作和数字处理的方法。

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

目录

温馨提示一则⚠:

1001、 害死人不偿命的(3n+1)思想

1002、 写出这个数(超长数型用字符串转换)(位数相加)

?1003、 我要通过!(存疑)


温馨提示一则⚠:

pta写java第一句要用——

import java.util.Scanner;

1001、 害死人不偿命的(3n+1)思想

import java.util.Scanner;
/** 
 * @author: y9
 * @Date: 2021/09/06
 */

public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int count=0;
		int n = reader.nextInt();
		while(n!=1) {
			while(n!=1 && n%2==0) {
				n/=2;
				count++;
			}
			while(n!=1 && n%2==1) {
				n=(3*n+1)/2;
				count++;
			}
		}
		System.out.print(count);
	}

}

1002、 写出这个数(超长数型用字符串转换)(位数相加)

package fun;  
import java.util.Scanner;
/** 
 * @author: y9
 * @Date: 2021/09/06
 */

public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		String ch[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
		int num[] = new int[10];
		String n=reader.nextLine();
		
		long count = 0L;
	    for (int i = 0; i < n.length(); i++) {
	      count += Long.parseLong(String.valueOf(n.charAt(i)));
	    }
		int i=0;
		while(count>0) {
			num[i]=(int) (count%10);
			count/=10;
			i++;
		}
		i--;
		while(i>0) {
			System.out.print(ch[num[i]]);
			System.out.print(" ");
			i--;
		}
		System.out.print(ch[num[0]]);
	}
}

!小tips:位数相加可用【String.charAt(i)】方式循环做。超长数型可用String存储 

?1003、 我要通过!(存疑)

package fun;  
import java.util.Scanner;

/** 
 * @author: y9
 * @Date: 2021/09/07
 */

public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		boolean y = true;
		int n = reader.nextInt();
		int count = 0;
		int i;
		String[] strs = new String[20];
		for(i=0;y&&i<n;i++) {
			strs[i]=reader.next();
		}
		for(int j=0;j<n;j++) {
			String str=strs[j];
			if(j>0) System.out.println();
			count=0;
			y=true;
		    	int p = str.indexOf('P');
		    	//System.out.println(p);
		    	int t = str.indexOf('T');
		    	//System.out.println(t);
		    	int a = str.indexOf('A');
		    	//System.out.println(a);
		    	if(p==-1||t==-1||p>t||a==-1||p==t-1) {
		    		if(!y) continue;
		    		System.out.print("NO");
		    		y=false;
		    		continue;
		    	}
		    	for(i=0;i<p;i++) {  // 开始到P之间
		    		if(!y) continue;
		    		if(str.charAt(i)!='A') {
		    			System.out.print("NO");
		    			y=false;
		    			
		    			break;
		    		}
		    		count++;
		    	}
		    	//System.out.println(count);
		    	for(i=p+1;i<t;i++) { //P到T之间
		    		if(!y) continue;
		    		if(str.charAt(i)!='A') {
		    			System.out.print("NO");
		    			y=false;
		    			break;
		    		}
		    	}
		    	if(i-p-1!=count&&i-p-2!=count&&y&&i-p-1!=1&&count!=0) { 
		    		System.out.print("NO");
		    		y=false;
		    	}
		    	for(i=t+1;i<str.length();i++) { //T之后
		    		if(str.charAt(i)!='A') {
		    			if(!y) continue;
		    			System.out.print("NO");
		    			y=false;
		    			break;
		    		}
		    	}
		    	if(i-t-1<count&&y) {
		    		System.out.print("NO");
		    		y=false;
		    		//System.out.println(i-t);
		    	}
		    	if(y) System.out.print("YES");
		    
		}
		
	}
}

!小tips1:字符串包含方法:String.contains("xxx"); 

!小tips2:字符串搜查位置方法:String.indexOf("xxx"); 

!小tips3:检查字符串是否全为数字方法:Chracter.isDigit("xxx"); 

!小tips1:字符串包含方法:String.contains("xxx");  

!小tips2:字符串搜查位置方法:String.indexOf("xxx"); 

!小tips3:检查字符串是否全为数字方法:Chracter.isDigit("xxx"); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值